-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSubclassWnd.cpp
449 lines (395 loc) · 11.9 KB
/
SubclassWnd.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/** @file SubclassWnd.cpp
*
* Implementation file for CSubclassWnd.
*
* @author William E. Kempf
* @version 2.0
*
* Copyright © 2000 NEWare Software.
*
* This code may be used in compiled form in any way you desire (including
* commercial use). The code may be redistributed unmodified by any means providing
* it is not sold for profit without the authors written consent, and providing that
* this notice and the authors name and all copyright notices remains intact. However,
* this file and the accompanying source code may not be hosted on a website or bulletin
* board without the authors written permission.
*
* <b><i>This software is provided "as is" without express or implied warranty. Use it
* at your own risk!</i></b>
*/
#include "stdafx.h"
#include "Debug.h"
#include "SubclassWnd.h"
namespace
{
/*
* Sends a message to all child controls of a specified window.
*
* @param message [in] Specifies the message id.
* @param wParam [in] Specifies additional message-specific information.
* @param lParam [in] Specifies additional message-specific information.
* @param bDeep [in] Specifies whether or not the message should be sent
* recursively to all children.
*/
void SendMessageToDescendantsImp(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam, BOOL bDeep)
{
// walk through HWNDs to avoid creating temporary CWnd objects
// unless we need to call this function recursively
for (HWND hWndChild = ::GetTopWindow(hWnd); hWndChild != NULL;
hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
{
// send message with Windows SendMessage API
::SendMessage(hWndChild, message, wParam, lParam);
if (bDeep && ::GetTopWindow(hWndChild) != NULL)
{
// send to child windows after parent
SendMessageToDescendantsImp(hWndChild, message, wParam, lParam,
bDeep);
}
}
}
};
/*
* Message used to unsubclass CSubclassWnd objects.
*/
const UINT WM_UNSUBCLASS = RegisterWindowMessage("{207D9568-FF3C-11D3-A469-000629B2F855}");
/*
* Message used to reflect messages back to child controls.
*/
const UINT WM_REFLECT = RegisterWindowMessage("{9688DAE5-3736-11D4-A48C-000629B2F855}");
/*
* Message used to get the reflector plugin from a window if one exists.
*/
const UINT WM_GETREFLECTOR = RegisterWindowMessage("{9688DAE6-3736-11D4-A48C-000629B2F855}");
/*
* Specifies the reflected message information. Used by WM_REFLECT.
*/
struct MSGREFLECTSTRUCT
{
UINT message;
WPARAM wParam;
LPARAM lParam;
BOOL bHandled;
};
/*
* Message handler used to reflect messages from a parent window to a child window.
*/
class CMessageReflector : public CSubclassWnd
{
private:
CMessageReflector(HWND hWnd);
public:
static CMessageReflector* FromHandle(HWND hWnd);
virtual BOOL ProcessWindowMessage(UINT message, WPARAM wParam, LPARAM lParam,
LRESULT& lResult);
virtual void OnFinalMessage();
};
/*
* Constructor. Subclasses the specified window.
*
* @param hWnd [in] Handle to the window to be subclassed.
*/
CMessageReflector::CMessageReflector(HWND hWnd)
{
ASSERT(::IsWindow(hWnd));
SubclassWindow(hWnd, FALSE);
}
/*
* This method retrieves a pointer to a CMessageReflector object from a handle to a
* window. If the specified window has not yet been subclassed by a CMessageReflector
* instance then a new instance is created on the heap.
*
* @param hWnd [in] Handle to the window to retrieve the CMessageReflector from.
*
* @return A pointer to the message reflector associated with the window.
*/
CMessageReflector* CMessageReflector::FromHandle(HWND hWnd)
{
CMessageReflector* pReflector =
(CMessageReflector*)::SendMessage(hWnd, WM_GETREFLECTOR, 0, 0);
if (!pReflector)
pReflector = new CMessageReflector(hWnd);
return pReflector;
}
/*
* Called to handle window messages. Derived classes can override this
* method to handle any window messages sent to the subclassed window.
* Normally the programmer won't code this directly, but will instead use
* the BEGIN_MSG_DISPATCH/END_MSG_DISPATCH macros.
*
* @param message [in] Specifies the message id.
* @param wParam [in] Specifies additional message-specific information.
* @param lParam [in] Specifies additional message-specific information.
* @param lResult [in] A reference to the result value to be returned to
* the caller.
*
* @return TRUE if the message was handled, otherwise FALSE.
*/
BOOL CMessageReflector::ProcessWindowMessage(UINT message, WPARAM wParam, LPARAM lParam,
LRESULT& lResult)
{
if (message == WM_GETREFLECTOR)
{
lResult = (LRESULT)this;
return TRUE;
}
HWND hWndChild = NULL;
switch (message)
{
case WM_COMMAND:
if (lParam != NULL) // not from a menu
hWndChild = (HWND)lParam;
break;
case WM_NOTIFY:
hWndChild = ((LPNMHDR)lParam)->hwndFrom;
break;
case WM_PARENTNOTIFY:
switch (LOWORD(wParam))
{
case WM_CREATE:
case WM_DESTROY:
hWndChild = (HWND)lParam;
break;
default:
hWndChild = GetDlgItem(GetHandle(), HIWORD(wParam));
break;
}
break;
case WM_DRAWITEM:
if (wParam) // not from a menu
hWndChild = ((LPDRAWITEMSTRUCT)lParam)->hwndItem;
break;
case WM_MEASUREITEM:
if (wParam) // not from a menu
hWndChild = GetDlgItem(GetHandle(), ((LPMEASUREITEMSTRUCT)lParam)->CtlID);
break;
case WM_COMPAREITEM:
if (wParam) // not from a menu
hWndChild = GetDlgItem(GetHandle(), ((LPCOMPAREITEMSTRUCT)lParam)->CtlID);
break;
case WM_DELETEITEM:
if (wParam) // not from a menu
hWndChild = GetDlgItem(GetHandle(), ((LPDELETEITEMSTRUCT)lParam)->CtlID);
break;
case WM_VKEYTOITEM:
case WM_CHARTOITEM:
case WM_HSCROLL:
case WM_VSCROLL:
hWndChild = (HWND)lParam;
break;
case WM_CTLCOLORBTN:
case WM_CTLCOLORDLG:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORMSGBOX:
case WM_CTLCOLORSCROLLBAR:
case WM_CTLCOLORSTATIC:
hWndChild = (HWND)lParam;
break;
default:
break;
}
if (hWndChild == NULL)
return FALSE;
ASSERT(::IsWindow(hWndChild));
MSGREFLECTSTRUCT rs;
rs.message = OCM__BASE + message;
rs.wParam = wParam;
rs.lParam = lParam;
rs.bHandled = FALSE;
lResult = ::SendMessage(hWndChild, WM_REFLECT, 0, (LPARAM)&rs);
return rs.bHandled;
}
/*
* Called after the last message has been dispatched to the window.
*/
void CMessageReflector::OnFinalMessage()
{
delete this;
}
/**
* Default contructor.
*/
CSubclassWnd::CSubclassWnd()
: m_hWnd(0), m_pfnSuperWindowProc(::DefWindowProc), m_pCurrentMsg(0)
{
}
/**
* Destructor.
*/
CSubclassWnd::~CSubclassWnd()
{
if (m_hWnd != NULL)
UnsubclassWindow();
}
/**
* Called to subclass the specified window. If the @a bReflect
* parameter is non-zero then the parent window will be subclassed to
* support message reflection.
*
* @param hWnd [in] Specifies the handle of the window to be subclassed.
* @param bReflect [in] Specifies whether or not the parent should
* reflect messages back to the window.
*
* @return If the window was subclassed, the return value is non-zero. If the
* window was not subclassed, the return value is zero.
*/
BOOL CSubclassWnd::SubclassWindow(HWND hWnd, BOOL bReflect)
{
if (m_hWnd != NULL || !::IsWindow(hWnd))
return FALSE;
m_thunk.Init(WindowProc, this);
WNDPROC pProc = (WNDPROC)&(m_thunk.thunk);
WNDPROC pfnWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC, (LONG)pProc);
if (pfnWndProc == NULL)
return FALSE;
m_pfnSuperWindowProc = pfnWndProc;
m_hWnd = hWnd;
if (bReflect && (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) == WS_CHILD)
{
HWND hWndParent = GetParent(hWnd);
ASSERT(::IsWindow(hWndParent));
CMessageReflector::FromHandle(hWndParent);
}
return TRUE;
}
/**
* Called to unsubclass the specified window.
*
* @return The handle to the window previously subclassed.
*/
HWND CSubclassWnd::UnsubclassWindow()
{
return (HWND)SendMessage(WM_UNSUBCLASS, 0, (LPARAM)this);
}
/**
* Implementation detail. This is the actual WindowProc installed by
* the thunk.
*
* @param hWnd [in] Handle to the window.
* @param message [in] Specifies the message id.
* @param wParam [in] Specifies additional message-specific information.
* @param lParam [in] Specifies additional message-specific information.
*
* @return The return value is the result of the message processing and
* depends on the message sent.
*/
LRESULT CALLBACK CSubclassWnd::WindowProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
CSubclassWnd* pThis = (CSubclassWnd*)hWnd;
// set a ptr to this message and save the old value
MSG msg = { pThis->m_hWnd, message, wParam, lParam, 0, { 0, 0 } };
// check to see if this is a reflected message and adjust accordingly
MSGREFLECTSTRUCT* rs = 0;
if (message == WM_REFLECT)
{
rs = (MSGREFLECTSTRUCT*)lParam;
ASSERT(rs);
msg.message = rs->message;
msg.wParam = rs->wParam;
msg.lParam = rs->lParam;
}
// save the message information and set the new current
const MSG* pOldMsg = pThis->m_pCurrentMsg;
pThis->m_pCurrentMsg = &msg;
// pass to the message map to process
LRESULT lRes;
BOOL bRet = pThis->ProcessWindowMessage(msg.message, msg.wParam, msg.lParam, lRes);
// if this is a reflected message let parent know if it was handled
if (rs)
rs->bHandled = bRet;
// restore saved value for the current message
pThis->m_pCurrentMsg = pOldMsg;
// do the default processing if message was not handled
// note that we need to use the original values, so that reflected
// messages can properly set the 'bHandled' flag.
if (!bRet)
lRes = pThis->DefWindowProc(message, wParam, lParam);
if (message == WM_NCDESTROY)
{
// unsubclass, if needed
pThis->UnsubclassWindow();
// clean up after window is destroyed
pThis->OnFinalMessage();
}
return lRes;
}
/**
* Sends a message to all child windows of the subclassed window.
*
* @param message [in] Specifies the message id.
* @param wParam [in] Specifies additional message-specific information.
* @param lParam [in] Specifies additional message-specific information.
* @param bDeep [in] Specifies whether or not the message should be sent
* recursively to all children.
*/
void CSubclassWnd::SendMessageToDescendants(UINT message, WPARAM wParam, LPARAM lParam,
BOOL bDeep)
{
SendMessageToDescendantsImp(GetHandle(), message, wParam, lParam, bDeep);
}
/**
* Called after the last message has been dispatched to the window.
*/
void CSubclassWnd::OnFinalMessage()
{
}
/**
* Called to handle window messages. Derived classes can override this
* method to handle any window messages sent to the subclassed window.
* Normally the programmer won't code this directly, but will instead use
* the #BEGIN_MSG_DISPATCH/#END_MSG_DISPATCH macros.
*
* @param message [in] Specifies the message id.
* @param wParam [in] Specifies additional message-specific information.
* @param lParam [in] Specifies additional message-specific information.
* @param lResult [out] A reference to the result value to be returned to
* the caller.
*
* @return If the message was handled, the return value is non-zero. If the
* message was not handled, the return value is zero.
*/
BOOL CSubclassWnd::ProcessWindowMessage(UINT message, WPARAM wParam, LPARAM lParam,
LRESULT& lResult)
{
if (message == WM_UNSUBCLASS)
{
if ((CSubclassWnd*)lParam == this)
{
if (m_hWnd == NULL)
lResult = NULL;
else
{
if (wParam)
{
CSubclassWnd* pPrevious = (CSubclassWnd*)wParam;
ASSERT(pPrevious->m_pfnSuperWindowProc == (WNDPROC)&(m_thunk.thunk));
pPrevious->m_pfnSuperWindowProc = m_pfnSuperWindowProc;
lResult = (LRESULT)m_hWnd;
m_pfnSuperWindowProc = ::DefWindowProc;
m_hWnd = NULL;
}
else
{
WNDPROC pOurProc = (WNDPROC)&(m_thunk.thunk);
WNDPROC pActiveProc = (WNDPROC)::GetWindowLong(m_hWnd, GWL_WNDPROC);
ASSERT(pOurProc == pActiveProc);
if (!::SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)m_pfnSuperWindowProc))
lResult = NULL;
else
{
m_pfnSuperWindowProc = ::DefWindowProc;
lResult = (LRESULT)m_hWnd;
m_hWnd = NULL;
}
}
}
}
else
lResult = DefWindowProc(message, (WPARAM)this, lParam);
return TRUE;
}
return FALSE;
}