__zero__

win32_ex

Oct 21st, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #pragma once
  2. #ifndef UNICODE
  3. #define UNICODE
  4. #endif
  5. #include <windows.h>
  6.  
  7. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMSG, WPARAM wParam, LPARAM lParam);
  8. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCommandLine, int nShowCmd)
  9. {
  10.     const wchar_t CLASS_NAME[] = L"class";
  11.     WNDCLASS wc = { };
  12.  
  13.     wc.lpfnWndProc = WindowProc;
  14.     wc.hInstance = hInstance;
  15.     wc.lpszClassName = CLASS_NAME;
  16.  
  17.     RegisterClass(&wc);
  18.  
  19.     HWND hWnd = CreateWindowEx(
  20.         0,
  21.         CLASS_NAME,
  22.         L"Window Title",
  23.         WS_OVERLAPPEDWINDOW,
  24.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  25.         NULL,
  26.         NULL,
  27.         hInstance,
  28.         NULL
  29.     );
  30.     HWND testBtn = CreateWindow
  31.     (
  32.         L"BUTTON",
  33.         L"TEXT",
  34.         WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
  35.         10,         // x position
  36.         10,         // y position
  37.         100,        // Button width
  38.         20,        // Button height,
  39.         hWnd,
  40.         NULL,       // No menu.
  41.         (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  42.         NULL);      // Pointer not needed.
  43.  
  44.     if (hWnd == NULL)
  45.     {
  46.         return 0;
  47.  
  48.         // Termina el programa, es decir, no se ha creado la ventana principal.
  49.     }
  50.  
  51.     ShowWindow(hWnd, nShowCmd);
  52.  
  53.     MSG msg = { };
  54.     //ZeroMemory(&msg, sizeof(msg));
  55.  
  56.     while (GetMessage(&msg, NULL, 0, 0))
  57.     {
  58.         TranslateMessage(&msg);
  59.         DispatchMessage(&msg);
  60.     }
  61.  
  62.     return 0;
  63. }
  64.  
  65. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMSG, WPARAM wParam, LPARAM lParam)
  66. {
  67.     RECT rect;
  68.     switch (uMSG) {
  69.     case WM_DESTROY:
  70.         PostQuitMessage(0); // Hace que GetMessage devuelva 0, por lo que el bucle es terminado, y la ventana se cierra
  71.         return 0;
  72.     case WM_PAINT:
  73.     {
  74.         PAINTSTRUCT ps;
  75.         HDC hdc = BeginPaint(hWnd, &ps);
  76.         FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW));
  77.         GetClientRect(hWnd, &rect);
  78.         DrawText(hdc, L"Hola mundo", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  79.         EndPaint(hWnd, &ps);
  80.     }
  81.     return 0;
  82.     case WM_CLOSE: // Aparece si se usa alt f4, o el botón de la esquina de cerrar
  83.     {
  84.         if (MessageBoxW(hWnd, L"Text", L"Caption", MB_OKCANCEL) == IDOK)
  85.         {
  86.             DestroyWindow(hWnd);
  87.         }
  88.     }
  89.         return 0; // Esto sucede si el usuario cancela o cierra el dialogo, el procedimiento se cierra
  90.     }
  91.  
  92.     return DefWindowProc(hWnd, uMSG, wParam, lParam);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment