-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZWindow.cpp
204 lines (181 loc) · 4.02 KB
/
ZWindow.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "ZWindow.h"
#include "ZGlobal.h"
#include "ZUIMgr.h"
#include "ZControl.h"
#include <tchar.h>
#include <atldef.h> // for ATLASSERT
#define Z_WINDOW_CLASS_NAME _T("ZUI Window Class")
#define Z_WINDOW_NAME _T("ZUI Window")
CZWindow::CZWindow(void)
: m_hWnd(NULL)
, m_pZUIMgr(NULL)
, m_pRootCtrl(NULL)
{
}
CZWindow::~CZWindow(void)
{
DetachZUIMgr();
}
CZWindow::operator HWND() const
{
return m_hWnd;
}
VOID CZWindow::AttachZUIMgr(CZUIMgr* pMgr)
{
ATLASSERT(pMgr != NULL);
m_pZUIMgr = pMgr;
ATLASSERT(NULL == m_pRootCtrl);
m_pRootCtrl = new CZControl();
if(m_pRootCtrl != NULL)
{
m_pRootCtrl->AttachZUIMgr(pMgr);
RECT rc = {0};
::GetClientRect(m_hWnd, &rc);
m_pRootCtrl->SetSize(rc);
}
}
CZUIMgr* CZWindow::DetachZUIMgr()
{
if(m_pRootCtrl != NULL)
{
m_pRootCtrl->DetachZUIMgr();
delete m_pRootCtrl;
m_pRootCtrl = NULL;
}
CZUIMgr* pMgr = m_pZUIMgr;
m_pZUIMgr = NULL;
return pMgr;
}
CZControl* CZWindow::GetRootControl()
{
return m_pRootCtrl;
}
BOOL CZWindow::RegisterWindowClass()
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = GetClassStyle();
wcex.lpfnWndProc = CZWindow::WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetInstance();
wcex.hIcon = GetClassIcon(FALSE);
wcex.hCursor = GetClassCursor();
wcex.hbrBackground = GetClassBackground();
wcex.lpszMenuName = GetClassMenuName();
wcex.lpszClassName = GetWindowClassName();
wcex.hIconSm = GetClassIcon(TRUE);
ATOM ret = ::RegisterClassEx(&wcex);
return ret != 0;
}
UINT CZWindow::GetClassStyle()
{
return CS_HREDRAW | CS_VREDRAW;
}
HICON CZWindow::GetClassIcon(BOOL bSmall)
{
return NULL;
}
HCURSOR CZWindow::GetClassCursor()
{
return ::LoadCursor(NULL, IDC_ARROW);
}
HBRUSH CZWindow::GetClassBackground()
{
return (HBRUSH)(COLOR_WINDOW + 1);
}
LPCTSTR CZWindow::GetClassMenuName()
{
return NULL;
}
LPCTSTR CZWindow::GetWindowClassName()
{
return Z_WINDOW_CLASS_NAME;
}
//#ifdef CreateWindow
//#pragma push_macro("CreateWindow")
//#undef CreateWindow
//HWND CZWindow::CreateWindow(DWORD dwStyle,
HWND CZWindow::CreateZWindow(DWORD dwStyle,
DWORD dwExStyle,
RECT& rc,
LPCTSTR lpWindowName,
HWND hWndParent,
HMENU hMenu)
//#pragma pop_macro("CreateWindow")
//#endif
{
if(NULL == lpWindowName)
{
lpWindowName = Z_WINDOW_NAME;
}
::CreateWindowEx(
dwExStyle,
GetWindowClassName(),
lpWindowName,
dwStyle,
rc.left,
rc.top,
GetRectWidth(rc),
GetRectHeight(rc),
hWndParent,
hMenu,
GetInstance(),
this);
return m_hWnd; // m_hWnd在WM_NCCREATE中已正确设置
}
VOID CZWindow::ShowWindow(BOOL bShow /* = TRUE */, BOOL bFocus /* = TRUE */)
{
ATLASSERT(::IsWindow(m_hWnd));
::ShowWindow(m_hWnd, bShow ? (bFocus ? SW_NORMAL : SW_SHOWNOACTIVATE) : SW_HIDE);
}
#pragma warning(push)
#pragma warning(disable:4244)
#pragma warning(disable:4312)
LRESULT CZWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CZWindow* pThis = NULL;
LRESULT lRes = -1;
BOOL bHandled = FALSE;
switch(uMsg)
{
case WM_NCCREATE:
{
LPCREATESTRUCT lpCreateStruct = (LPCREATESTRUCT)lParam;
pThis = (CZWindow*)(lpCreateStruct->lpCreateParams);
ATLASSERT(pThis != NULL);
pThis->m_hWnd = hWnd; // m_hWnd需要在此设置, 否则HandleMessage中使用m_hWnd不正确.
::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pThis);
}
break;
default:
{
// TODO: 处理WM_NCDESTROY消息
pThis = (CZWindow*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
}
break;
}
// 真窗口先处理, 非DUI的消息在此处理
// WM_GETMINMAXINFO在WM_NCCREATE之前, pThis有可能为NULL
if(pThis != NULL)
{
lRes = pThis->HandleMessage(uMsg, wParam, lParam, bHandled);
// DUI消息在此处理
if(!bHandled && pThis->m_pZUIMgr != NULL)
{
lRes = pThis->m_pZUIMgr->HandleMessage(uMsg, wParam, lParam, bHandled);
}
}
// 最后的默认处理
if(!bHandled)
{
lRes = ::DefWindowProc(hWnd, uMsg, wParam, lParam); // 使用hWnd而非m_hWnd, 因为m_hWnd可能已经置空.
}
return lRes;
}
#pragma warning(pop)
LRESULT CZWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
bHandled = FALSE;
return 0;
}