Advertisement
kijato

SimpleWindowForm (c++)

May 19th, 2020
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. /*
  2.     http://www.winprog.org/tutorial/simple_window.html
  3.     +
  4.     https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
  5.     https://www.win7dll.info/user32_dll.html
  6. */
  7.  
  8. #include <windows.h>
  9.  
  10. const char g_szClassName[] = "myWindowClass";
  11.  
  12. // Step 4: the Window Procedure
  13. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  14. {
  15.     switch(msg)
  16.     {
  17.  
  18.    
  19.         case WM_LBUTTONDOWN:    // <- http://www.winprog.org/tutorial/window_click.html
  20.         {
  21.             char szFileName[MAX_PATH];
  22.             HINSTANCE hInstance = GetModuleHandle(NULL);
  23.  
  24.             GetModuleFileName(hInstance, szFileName, MAX_PATH);
  25.             MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
  26.         }
  27.         break;                  // <-
  28.  
  29.  
  30.         case WM_CLOSE:
  31.             DestroyWindow(hwnd);
  32.         break;
  33.         case WM_DESTROY:
  34.             PostQuitMessage(0);
  35.         break;
  36.         default:
  37.             return DefWindowProc(hwnd, msg, wParam, lParam);
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  43.     LPSTR lpCmdLine, int nCmdShow)
  44. {
  45.     WNDCLASSEX wc;
  46.     HWND hwnd;
  47.     MSG Msg;
  48.  
  49.     //Step 1: Registering the Window Class
  50.     wc.cbSize        = sizeof(WNDCLASSEX);
  51.     wc.style         = 0;
  52.     wc.lpfnWndProc   = WndProc;
  53.     wc.cbClsExtra    = 0;
  54.     wc.cbWndExtra    = 0;
  55.     wc.hInstance     = hInstance;
  56.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  57.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  58.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  59.     wc.lpszMenuName  = NULL;
  60.     wc.lpszClassName = g_szClassName;
  61.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  62.  
  63.     if(!RegisterClassEx(&wc))
  64.     {
  65.         MessageBox(NULL, "Window Registration Failed!", "Error!",
  66.             MB_ICONEXCLAMATION | MB_OK);
  67.         return 0;
  68.     }
  69.  
  70.     // Step 2: Creating the Window
  71.     hwnd = CreateWindowEx(
  72.         WS_EX_CLIENTEDGE,
  73.         g_szClassName,
  74.         "The title of my window",
  75.         WS_OVERLAPPEDWINDOW,
  76.         CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
  77.         NULL, NULL, hInstance, NULL);
  78.  
  79.     if(hwnd == NULL)
  80.     {
  81.         MessageBox(NULL, "Window Creation Failed!", "Error!",
  82.             MB_ICONEXCLAMATION | MB_OK);
  83.         return 0;
  84.     }
  85.  
  86.     ShowWindow(hwnd, nCmdShow);
  87.     UpdateWindow(hwnd);
  88.  
  89.     // Step 3: The Message Loop
  90.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  91.     {
  92.         TranslateMessage(&Msg);
  93.         DispatchMessage(&Msg);
  94.     }
  95.     return Msg.wParam;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement