Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Window.h"
- Window::Window()
- {
- }
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg, WPARAM wparam, LPARAM lparam)
- {
- //GetWindowLong(hwnd,)
- switch (msg)
- {
- case WM_CREATE:
- {
- // Event fired when the window is created
- // collected here..
- Window* window = (Window*)((LPCREATESTRUCT)lparam)->lpCreateParams;
- // .. and then stored for later lookup
- SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
- window->setHWND(hwnd);
- window->onCreate();
- break;
- }
- case WM_SETFOCUS:
- {
- // Event fired when the window get focus
- Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- window->onFocus();
- break;
- }
- case WM_KILLFOCUS:
- {
- // Event fired when the window lost focus
- Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
- window->onKillFocus();
- break;
- }
- case WM_DESTROY:
- {
- // Event fired when the window is destroyed
- Window* window =(Window*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
- window->onDestroy();
- ::PostQuitMessage(0);
- break;
- }
- default:
- return ::DefWindowProc(hwnd, msg, wparam, lparam);
- }
- return NULL;
- }
- bool Window::init()
- {
- //Setting up WNDCLASSEX object
- WNDCLASSEX wc;
- wc.cbClsExtra = NULL;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.cbWndExtra = NULL;
- wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wc.hInstance = NULL;
- wc.lpszClassName = "MyWindowClass";
- wc.lpszMenuName = "";
- wc.style = NULL;
- wc.lpfnWndProc = &WndProc;
- if (!::RegisterClassEx(&wc)) // If the registration of class will fail, the function will return false
- return false;
- //Creation of the window
- m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MyWindowClass", "DirectX Application",
- WS_CAPTION|WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
- NULL, NULL, NULL, this);
- //if the creation fail return false
- if (!m_hwnd)
- return false;
- //show up the window
- ::ShowWindow(m_hwnd, SW_SHOW);
- ::UpdateWindow(m_hwnd);
- //set this flag to true to indicate that the window is initialized and running
- m_is_run = true;
- return true;
- }
- bool Window::broadcast()
- {
- MSG msg;
- this->onUpdate();
- while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- Sleep(1);
- return true;
- }
- bool Window::release()
- {
- //Destroy the window
- if (!::DestroyWindow(m_hwnd))
- return false;
- return true;
- }
- bool Window::isRun()
- {
- return m_is_run;
- }
- RECT Window::getClientWindowRect()
- {
- RECT rc;
- ::GetClientRect(this->m_hwnd, &rc);
- return rc;
- }
- void Window::setHWND(HWND hwnd)
- {
- this->m_hwnd = hwnd;
- }
- void Window::onCreate()
- {
- }
- void Window::onUpdate()
- {
- }
- void Window::onDestroy()
- {
- m_is_run = false;
- }
- void Window::onFocus()
- {
- }
- void Window::onKillFocus()
- {
- }
- Window::~Window()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement