Advertisement
Combreal

windowsTemplate.cpp

Apr 2nd, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3.  
  4. #define ID_BUTTON 1
  5.  
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7. HANDLE hwndButton;
  8.  
  9. void CenterWindow(HWND);
  10. LPWSTR stringToLPWSTR(const std::string& instr);
  11.  
  12. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  13. {
  14.         MSG msg;    
  15.         WNDCLASSW wc = {0};
  16.         wc.lpszClassName = L"test";
  17.         wc.hInstance = hInstance;
  18.         wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  19.         wc.lpfnWndProc = WndProc;
  20.         wc.hCursor = LoadCursor(0, IDC_ARROW);
  21.         RegisterClassW(&wc);
  22.         CreateWindowW(wc.lpszClassName, L"Test", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 260, 134, 288, 104, 0, 0, hInstance, 0);  
  23.         while(GetMessage(&msg, NULL, 0, 0))
  24.         {
  25.             TranslateMessage(&msg);
  26.             DispatchMessage(&msg);
  27.         }
  28.         return (int) msg.wParam;
  29. }
  30.  
  31. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33.     switch(msg)
  34.     {
  35.     case WM_KEYDOWN:
  36.         if (wParam == VK_ESCAPE) {
  37.             int ret = MessageBoxW(hwnd, L"Woot?",
  38.                 L"Message", MB_OKCANCEL);                            
  39.             if (ret == IDOK) {
  40.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  41.             }
  42.         }    
  43.         break;
  44.     case WM_CREATE:
  45.         CenterWindow(hwnd);
  46.         hwndButton=CreateWindowW(L"Button", L"Click", WS_VISIBLE|WS_CHILD, 50, 9, 61, 21, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  47.         break;  
  48.     case WM_COMMAND:
  49.         if(LOWORD(wParam)==ID_BUTTON)
  50.         {
  51.  
  52.         }
  53.         break;
  54.     case WM_DESTROY:  
  55.         PostQuitMessage(0);
  56.         break;
  57.     }
  58.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  59. }
  60.  
  61. void CenterWindow(HWND hwnd)
  62. {
  63.     RECT rc = {0};
  64.     GetWindowRect(hwnd, &rc);
  65.     int win_w = rc.right - rc.left;
  66.     int win_h = rc.bottom - rc.top;
  67.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  68.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  69.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
  70. }
  71.  
  72. LPWSTR stringToLPWSTR(const std::string& instr)
  73. {
  74.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  75.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  76.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  77.     widestr[bufferlen] = 0;
  78.     return widestr;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement