This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplewnd.hpp
167 lines (135 loc) · 4.79 KB
/
simplewnd.hpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef RAYTRACR_SIMPLEWND_HPP
#define RAYTRACR_SIMPLEWND_HPP
class CSimpleWindow
{
private:
typedef CSimpleWindow CSelf;
protected:
const char* const WINDOWCLASSNAME;
const HINSTANCE m_hInstance;
const INT m_nClientW;
const INT m_nClientH;
const DWORD m_dwStyle;
public:
CSimpleWindow(const HINSTANCE hInstance, const INT nClientW, const INT nClientH, const DWORD dwStyle)
: m_hInstance(hInstance)
, m_nClientW(nClientW)
, m_nClientH(nClientH)
, m_dwStyle(dwStyle)
, WINDOWCLASSNAME("CSimpleWindowClass")
{
}
virtual BOOL IsAcceleratorMessage(HWND hWnd, LPMSG lpMsg)
{
return FALSE;
}
virtual VOID WndProcOnCommand(HWND hWnd, INT nId, HWND hWndCtl, UINT uCodeNotify)
{
switch(nId)
{
case IDCANCEL:
DestroyWindow(hWnd);
break;
}
}
virtual BOOL WndProcOnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
RECT rcWnd, rcWA;
if(!SystemParametersInfo(SPI_GETWORKAREA, sizeof(rcWA), &rcWA, 0))
{
return FALSE;
}
SetRect(&rcWnd, 0, 0, m_nClientW, m_nClientH);
AdjustWindowRectEx(&rcWnd, GetWindowStyle(hWnd), FALSE, GetWindowExStyle(hWnd));
OffsetRect(&rcWnd, -rcWnd.left+(rcWA.right-rcWA.left-rcWnd.right+rcWnd.left)/2, -rcWnd.top+(rcWA.bottom-rcWA.top-rcWnd.bottom+rcWnd.top)/2);
MoveWindow(hWnd, rcWnd.left, rcWnd.top, rcWnd.right-rcWnd.left, rcWnd.bottom-rcWnd.top, TRUE);
return TRUE;
}
virtual VOID WndProcOnDestroy(HWND hWnd)
{
PostQuitMessage(EXIT_SUCCESS);
}
virtual UINT WndProcOnNCHitTest(HWND hWnd, int nX, int nY)
{
return FORWARD_WM_NCHITTEST(hWnd, nX, nY, DefWindowProc);
}
virtual VOID WndProcOnPaint(HWND hWnd)
{
FORWARD_WM_PAINT(hWnd, DefWindowProc);
}
virtual VOID WndProcOnTimer(HWND hWnd, UINT uId)
{
FORWARD_WM_TIMER(hWnd, uId, DefWindowProc);
}
virtual VOID WndProcOnWindowPosChanged(HWND hWnd, CONST WINDOWPOS* lpWP)
{
FORWARD_WM_WINDOWPOSCHANGED(hWnd, lpWP, DefWindowProc);
}
virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_COMMAND, WndProcOnCommand);
HANDLE_MSG(hWnd, WM_CREATE, WndProcOnCreate);
HANDLE_MSG(hWnd, WM_DESTROY, WndProcOnDestroy);
HANDLE_MSG(hWnd, WM_NCHITTEST, WndProcOnNCHitTest);
HANDLE_MSG(hWnd, WM_PAINT, WndProcOnPaint);
HANDLE_MSG(hWnd, WM_TIMER, WndProcOnTimer);
HANDLE_MSG(hWnd, WM_WINDOWPOSCHANGED, WndProcOnWindowPosChanged);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
static LRESULT CALLBACK WndProcCB(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CSelf* lpThis = (CSelf*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if(lpThis!=NULL)
{
return lpThis->WndProc(hWnd, uMsg, wParam, lParam);
}
else
if(uMsg==WM_NCCREATE)
{
LPCREATESTRUCT lpCreateStruct = (LPCREATESTRUCT)lParam;
if(lpCreateStruct->lpCreateParams!=NULL)
{
lpThis = static_cast< CSelf* >(lpCreateStruct->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)lpThis);
return lpThis->WndProc(hWnd, uMsg, wParam, lParam);
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
public:
virtual operator int()
{
WNDCLASSEX Wc = { sizeof(Wc) };
Wc.lpfnWndProc = &CSelf::WndProcCB;
Wc.hInstance = m_hInstance;
Wc.lpszClassName = WINDOWCLASSNAME;
Wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Wc.hCursor = LoadCursor(NULL, IDC_ARROW);
Wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(RegisterClassEx(&Wc))
{
HWND hWnd = CreateWindowEx(0, WINDOWCLASSNAME, "", m_dwStyle, 0, 0, GetSystemMetrics(SM_CXMINTRACK), GetSystemMetrics(SM_CYMINTRACK), NULL, NULL, m_hInstance, this);
if(hWnd!=NULL)
{
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0)>0)
{
if(!IsAcceleratorMessage(hWnd, &Msg) && !IsDialogMessage(hWnd, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
if(Msg.message==WM_QUIT)
{
return (int)Msg.wParam;
}
}
}
return EXIT_FAILURE;
}
};
#endif /* RAYTRACR_SIMPLEWND_HPP */