-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
81 lines (74 loc) · 2.31 KB
/
Window.cpp
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
#include "Window.h"
void Window::Register()
{
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = Window::WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hinst_;
wndclassex.hIcon = reinterpret_cast<HICON>(LoadImage(hinst_, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE));
wndclassex.hIconSm = reinterpret_cast<HICON>(LoadImage(hinst_, MAKEINTRESOURCE(IDI_SMALL), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
wndclassex.hCursor = manualcursor_ ? NULL : LoadCursor(NULL, IDC_ARROW);
wndclassex.hbrBackground = dontfillbackground_ ? NULL : backgroundbrush_ != nullptr ? backgroundbrush_ : GetSysColorBrush(COLOR_BTNFACE);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = ClassName();
WinRegisterClass(&wndclassex);
}
LRESULT CALLBACK Window::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Window* self{ nullptr };
if (uMsg == WM_NCCREATE)
{
const auto lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
self = reinterpret_cast<Window*>(lpcs->lpCreateParams);
self->hwnd_ = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(self));
}
else
{
self = reinterpret_cast<Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}
if (self)
{
return self->HandleMessage(uMsg, wParam, lParam);
}
else
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
LRESULT Window::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lres{};
switch (uMsg)
{
case WM_NCDESTROY:
lres = DefWindowProc(hwnd_, uMsg, wParam, lParam);
SetWindowLongPtr(hwnd_, GWLP_USERDATA, 0);
delete this;
return lres;
case WM_PAINT:
OnPaint();
return 0;
case WM_PRINTCLIENT:
OnPrintClient(reinterpret_cast<HDC>(wParam));
return 0;
}
return DefWindowProc(hwnd_, uMsg, wParam, lParam);
}
void Window::OnPaint()
{
PAINTSTRUCT ps;
BeginPaint(hwnd_, &ps);
PaintContent(&ps);
EndPaint(hwnd_, &ps);
}
void Window::OnPrintClient(HDC hdc)
{
PAINTSTRUCT ps;
ps.hdc = hdc;
GetClientRect(hwnd_, &ps.rcPaint);
PaintContent(&ps);
}