Advertisement
DSHFJGHLKG

Untitled

Dec 15th, 2019
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include "Window.h"
  2.  
  3. Window::Window()
  4. {
  5.    
  6. }
  7.  
  8. LRESULT CALLBACK WndProc(HWND hwnd,UINT msg, WPARAM wparam, LPARAM lparam)
  9. {
  10.     //GetWindowLong(hwnd,)
  11.     switch (msg)
  12.     {
  13.     case WM_CREATE:
  14.     {
  15.         // Event fired when the window is created
  16.         // collected here..
  17.         Window* window = (Window*)((LPCREATESTRUCT)lparam)->lpCreateParams;
  18.         // .. and then stored for later lookup
  19.         SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
  20.         window->setHWND(hwnd);
  21.         window->onCreate();
  22.         break;
  23.     }
  24.     case WM_SETFOCUS:
  25.     {
  26.         // Event fired when the window get focus
  27.         Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  28.         window->onFocus();
  29.         break;
  30.     }
  31.     case WM_KILLFOCUS:
  32.     {
  33.         // Event fired when the window lost focus
  34.         Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  35.         window->onKillFocus();
  36.         break;
  37.     }
  38.     case WM_DESTROY:
  39.     {
  40.         // Event fired when the window is destroyed
  41.         Window* window =(Window*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  42.         window->onDestroy();
  43.         ::PostQuitMessage(0);
  44.         break;
  45.     }
  46.  
  47.  
  48.     default:
  49.         return ::DefWindowProc(hwnd, msg, wparam, lparam);
  50.     }
  51.  
  52.     return NULL;
  53. }
  54.  
  55.  
  56. bool Window::init()
  57. {
  58.     //Setting up WNDCLASSEX object
  59.     WNDCLASSEX wc;
  60.     wc.cbClsExtra = NULL;
  61.     wc.cbSize = sizeof(WNDCLASSEX);
  62.     wc.cbWndExtra = NULL;
  63.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  64.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  65.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  66.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  67.     wc.hInstance = NULL;
  68.     wc.lpszClassName = "MyWindowClass";
  69.     wc.lpszMenuName = "";
  70.     wc.style = NULL;
  71.     wc.lpfnWndProc = &WndProc;
  72.  
  73.     if (!::RegisterClassEx(&wc)) // If the registration of class will fail, the function will return false
  74.         return false;
  75.  
  76.     //Creation of the window
  77.     m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MyWindowClass", "DirectX Application",
  78.         WS_CAPTION|WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
  79.         NULL, NULL, NULL, this);
  80.  
  81.     //if the creation fail return false
  82.     if (!m_hwnd)
  83.         return false;
  84.  
  85.     //show up the window
  86.     ::ShowWindow(m_hwnd, SW_SHOW);
  87.     ::UpdateWindow(m_hwnd);
  88.  
  89.     //set this flag to true to indicate that the window is initialized and running
  90.     m_is_run = true;
  91.  
  92.     return true;
  93. }
  94.  
  95. bool Window::broadcast()
  96. {
  97.     MSG msg;
  98.  
  99.     this->onUpdate();
  100.  
  101.     while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
  102.     {
  103.         TranslateMessage(&msg);
  104.         DispatchMessage(&msg);
  105.     }
  106.  
  107.     Sleep(1);
  108.  
  109.     return true;
  110. }
  111.  
  112.  
  113. bool Window::release()
  114. {
  115.     //Destroy the window
  116.     if (!::DestroyWindow(m_hwnd))
  117.         return false;
  118.  
  119.     return true;
  120. }
  121.  
  122. bool Window::isRun()
  123. {
  124.     return m_is_run;
  125. }
  126.  
  127. RECT Window::getClientWindowRect()
  128. {
  129.     RECT rc;
  130.     ::GetClientRect(this->m_hwnd, &rc);
  131.     return rc;
  132. }
  133.  
  134. void Window::setHWND(HWND hwnd)
  135. {
  136.     this->m_hwnd = hwnd;
  137. }
  138.  
  139. void Window::onCreate()
  140. {
  141. }
  142.  
  143. void Window::onUpdate()
  144. {
  145. }
  146.  
  147. void Window::onDestroy()
  148. {
  149.     m_is_run = false;
  150. }
  151.  
  152. void Window::onFocus()
  153. {
  154. }
  155.  
  156. void Window::onKillFocus()
  157. {
  158. }
  159.  
  160. Window::~Window()
  161. {
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement