Guest User

Win32 Window

a guest
Jul 18th, 2016
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <string>
  3.  
  4. LRESULT CALLBACK window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
  5. {
  6.     if (message == WM_CLOSE || (message == WM_KEYDOWN && wparam == VK_ESCAPE)) {
  7.         PostQuitMessage(0);
  8.         return 0;
  9.     }
  10.  
  11.     return DefWindowProc(hwnd, message, wparam, lparam);
  12. }
  13.  
  14. int main()
  15. {
  16.     std::string window_title = "AppName";
  17.     std::string window_class = window_title + "Class";
  18.  
  19.     WNDCLASS wc = {};
  20.     wc.lpfnWndProc = window_proc;
  21.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  22.     wc.lpszClassName = window_class.c_str();
  23.     RegisterClass(&wc);
  24.  
  25.     DWORD dwStyle = WS_SYSMENU | WS_CAPTION | WS_VISIBLE;
  26.  
  27.     RECT rc = { 0, 0, 512, 512 };
  28.     AdjustWindowRect(&rc, dwStyle, FALSE);
  29.  
  30.     HWND hwnd = CreateWindow(window_class.c_str(), window_title.c_str(), dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, NULL, NULL);
  31.     if (!hwnd) {
  32.         UnregisterClass(window_class.c_str(), NULL);
  33.         return 1;
  34.     }
  35.  
  36.     MSG msg = {};
  37.     while (msg.message != WM_QUIT) {
  38.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  39.             TranslateMessage(&msg);
  40.             DispatchMessage(&msg);
  41.         }
  42.     }
  43.  
  44.     DestroyWindow(hwnd);
  45.     UnregisterClass(window_class.c_str(), NULL);
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment