whiplk

[App] - WinAPI.Exemplo

Jul 7th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1.    
  2.  
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include <iostream>
  6. using namespace std;
  7.      
  8. const char g_szClassName[] = "handle1";
  9. HWND Button1; /* HANDLE de nossos objetos */
  10. HWND NewContact; // HANDLE do Window Contatc
  11. HWND Nome, Numero, Add; // HANDLE do Window Contatc
  12. #define intButton1   1002 /*IDs de nossos objetos*/
  13. #define intEdit1   1003 /*IDs de nossos objetos*/
  14. #define intEdit2   1004 /*IDs de nossos objetos*/
  15. #define intAdd1   1005 /*IDs de nossos objetos*/
  16. // Step 4: the Window Procedure
  17. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  18. {
  19.     switch(msg)
  20.     {
  21.         case WM_COMMAND:
  22.         {      
  23.                 if(LOWORD(wParam) == intButton1)
  24.                 {
  25.                     //MessageBox(NULL, "Você acabou de clicar em um botão", "Informação!", MB_ICONINFORMATION | MB_OK);
  26.                     ShowWindow(NewContact, SW_SHOW);
  27.                     UpdateWindow(NewContact);
  28.                     ShowWindow(Nome, SW_SHOW);
  29.                     UpdateWindow(Nome);
  30.                     ShowWindow(Numero, SW_SHOW);
  31.                     UpdateWindow(Numero);
  32.                     ShowWindow(Add, SW_SHOW);
  33.                     UpdateWindow(Add);
  34.                      
  35.                 }
  36.                  
  37.                 if(LOWORD(wParam) == intAdd1)
  38.                 {
  39.                     char GNome[128], GNumero[30];
  40.                     GetWindowText(Nome, (LPWSTR)GNome, 128);
  41.                     GetWindowText(Numero, (LPWSTR)GNumero, 30);
  42.                     cout << "Você Adicionou um Novo Contato:" << endl;
  43.                     cout << "Nome" << GNome << endl;
  44.                     cout << "Numero" << GNumero << endl;
  45.                     //DestroyWindow(NewContact);
  46.                     //DestroyWindow(Nome);
  47.                     //DestroyWindow(Numero);
  48.                     //DestroyWindow(Add);    
  49.                 }
  50.                 break;          
  51.         }            
  52.         case WM_CLOSE:
  53.             DestroyWindow(hwnd);
  54.         break;
  55.         case WM_DESTROY:
  56.             PostQuitMessage(0);
  57.         break;
  58.         default:
  59.             return DefWindowProc(hwnd, msg, wParam, lParam);
  60.     }
  61.     return 0;
  62. }
  63. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  64.     LPSTR lpCmdLine, int nCmdShow)
  65. {
  66.     WNDCLASSEX wc;
  67.     HWND hwnd;
  68.     MSG Msg;
  69.     //Step 1: Registering the Window Class
  70.     wc.cbSize        = sizeof(WNDCLASSEX);
  71.     wc.style         = 0;
  72.     wc.lpfnWndProc   = WndProc;
  73.     wc.cbClsExtra    = 0;
  74.     wc.cbWndExtra    = 0;
  75.     wc.hInstance     = hInstance;
  76.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  77.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  78.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  79.     wc.lpszMenuName  = NULL;
  80.     wc.lpszClassName = (LPWSTR)g_szClassName;
  81.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  82.  
  83.     if(!RegisterClassEx(&wc))
  84.     {
  85.         MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"),
  86.             MB_ICONEXCLAMATION | MB_OK);
  87.         return 0;
  88.     }
  89.     // Step 2: Creating the Window
  90.     hwnd = CreateWindowEx(
  91.         WS_EX_CLIENTEDGE,
  92.         (LPCWSTR)g_szClassName,
  93.         _T("Agenda Virtual"),
  94.         WS_OVERLAPPEDWINDOW,
  95.         CW_USEDEFAULT, CW_USEDEFAULT, 440, 220,
  96.         NULL, NULL, hInstance, NULL);
  97.     if(hwnd == NULL)
  98.     {
  99.         MessageBox(NULL, _T("Falha na criação da GUI!"), _T("Error!"),
  100.             MB_ICONEXCLAMATION | MB_OK);
  101.         return 0;
  102.     }
  103.     NewContact = CreateWindowEx(
  104.                     WS_EX_CLIENTEDGE,
  105.                     (LPCWSTR)g_szClassName,
  106.                     _T("Adicionar Contato"),
  107.                     WS_OVERLAPPEDWINDOW,
  108.                     CW_USEDEFAULT, CW_USEDEFAULT, 200, 120,
  109.                     NULL, NULL, hInstance, NULL);
  110.     Button1 = CreateWindowEx(0, _T("BUTTON"), _T("Adicionar Contato"),  WS_CHILD + WS_VISIBLE + WS_TABSTOP, 50, 50, 120, 20, hwnd, (HMENU)intButton1, hInstance, 0); /*Nosso button */
  111.     Nome = CreateWindowEx(0, _T("EDIT"), _T("Nome"),  WS_CHILD + WS_VISIBLE + WS_TABSTOP, 20, 10, 120, 20, NewContact, (HMENU)intEdit1, hInstance, 0);
  112.     Numero = CreateWindowEx(0, _T("EDIT"), _T("Numero"),  WS_CHILD + WS_VISIBLE + WS_TABSTOP, 20, 30, 120, 20, NewContact, (HMENU)intEdit2, hInstance, 0);
  113.     Add = CreateWindowEx(0, _T("BUTTON"), _T("Adicionar Contato"),  WS_CHILD + WS_VISIBLE + WS_TABSTOP, 20, 50, 120, 20, NewContact, (HMENU)intAdd1, hInstance, 0); /*Nosso button */
  114.     ShowWindow(Button1, nCmdShow);
  115.     ShowWindow(hwnd, nCmdShow);
  116.     UpdateWindow(hwnd);
  117.     UpdateWindow(Button1);
  118.        
  119.     // Step 3: The Message Loop
  120.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  121.     {
  122.         TranslateMessage(&Msg);
  123.         DispatchMessage(&Msg);
  124.     }
  125.     return Msg.wParam;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment