1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include "window.h"
namespace Engine3D
{
Window Window::window_;
LRESULT CALLBACK WndProc(HWND _hwnd, UINT _msg, WPARAM _wParam, LPARAM _lParam)
{
switch(_msg)
{
case WM_CLOSE: DestroyWindow(_hwnd);
break;
case WM_DESTROY: PostQuitMessage(0);
break;
default: return DefWindowProc(_hwnd, _msg, _wParam, _lParam);
}
return 0;
}
Window::Window()
{
ZeroMemory(&handle_, sizeof(HWND));
ZeroMemory(&wc_, sizeof(WNDCLASSEX));
ZeroMemory(&message_, sizeof(MSG));
wc_.style = CS_HREDRAW | CS_VREDRAW;
wc_.lpfnWndProc = WndProc;
wc_.cbClsExtra = 0L;
wc_.cbWndExtra = 0L;
wc_.hInstance = GetModuleHandle(NULL);
wc_.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc_.hCursor = LoadCursor(NULL, IDC_ARROW);
wc_.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
wc_.lpszMenuName = NULL;
wc_.lpszClassName = "window";
if(!RegisterClass(&wc_))
{
MessageBox(NULL, "Unable To Register Window.", "ERROR", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return;
}
//CreateWindow() sets the Window area size, we want to set the
//Client area size, this is done by using AdjustWindowRectEx().
RECT wr = {0, 0, 800, 600}; // set the size
AdjustWindowRectEx(&wr, WS_OVERLAPPEDWINDOW, false, WS_EX_CLIENTEDGE); // adjust the size
handle_ = CreateWindowEx( WS_EX_CLIENTEDGE,
"window",
"Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
wr.right - wr.left, //height
wr.bottom - wr.top, //width
NULL,
NULL,
wc_.hInstance,
0);
if(!handle_)
{
MessageBox(NULL, "Error Creating Window.", "ERROR", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return;
}
ShowWindow(handle_, SW_SHOW); //make window visible
UpdateWindow(handle_); //draw the window
}
Window::~Window()
{
UnregisterClass(wc_.lpszClassName, wc_.hInstance); //unregister window
DestroyWindow(window_.handle_); //destroy the window
}
void Window::Update()
{
//loop through windows messages
while(PeekMessage(&window_.message_, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&window_.message_);
DispatchMessage(&window_.message_);
}
}
void Window::SetWindowTitle(std::string _title)
{
SetWindowText(window_.handle_, _title.c_str());
}
void Window::SetDimensions(UINT _width, UINT _height)
{
//Client Rectangle - Area the programmer can draw on
//Window Rectangle - Area including the menu, title bar, borders, ect.
//Get current client and window rectangle
RECT rcClient, rcWindow;
GetClientRect(window_.handle_, &rcClient);
GetWindowRect(window_.handle_, &rcWindow);
//adjust width and height accordingly
int width = (rcWindow.right - rcWindow.left) - rcClient.right + _width;
int height = (rcWindow.bottom - rcWindow.top) - rcClient.bottom + _height;
//resize the client area
MoveWindow(window_.handle_, rcWindow.left, rcWindow.top, width, height, true);
}
}
|