-
Notifications
You must be signed in to change notification settings - Fork 1
/
amsWnd.cpp
443 lines (350 loc) · 13.9 KB
/
amsWnd.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
// amsWnd.h : implementation of the CAMSWnd class
// Created by: Alvaro Mendez - 04/09/2002
#include <stdafx.h>
#include "amsWnd.h"
///////////////////////////////////////////////////////////////////////////////
// CAMSWnd
// Saves the given window's position and size to the app's profile.
void CAMSWnd::Save(CWnd* pWnd, LPCTSTR szWindowName /*= NULL*/)
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
// Set registry section
CString strSection = szWindowName;
if (strSection.IsEmpty())
pWnd->GetWindowText(strSection);
if (strSection.Find('\\') < 0)
strSection = _T("Windows\\") + strSection;
WINDOWPLACEMENT wp;
pWnd->GetWindowPlacement(&wp);
pWnd->GetWindowRect(&wp.rcNormalPosition); // MODIF_KOCH
// Save everything
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(strSection, _T("X"), wp.rcNormalPosition.left);
pApp->WriteProfileInt(strSection, _T("Y"), wp.rcNormalPosition.top);
pApp->WriteProfileInt(strSection, _T("Width"), wp.rcNormalPosition.right - wp.rcNormalPosition.left);
pApp->WriteProfileInt(strSection, _T("Height"), wp.rcNormalPosition.bottom - wp.rcNormalPosition.top);
pApp->WriteProfileInt(strSection, _T("State"), wp.showCmd);
}
// Restores and sets the given window's position and/or size from the app's profile
void CAMSWnd::Restore(CWnd* pWnd, LPCTSTR szWindowName /*= NULL*/, unsigned uFlags /*= CAMSWnd::Position*/)
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
// Set registry section
CString strSection = szWindowName;
if (strSection.IsEmpty())
pWnd->GetWindowText(strSection);
if (strSection.Find('\\') < 0)
strSection = _T("Windows\\") + strSection;
unsigned uSWPflags = SWP_NOZORDER | ((uFlags & NoRedraw) ? SWP_NOREDRAW : 0);
const int nInvalidValue = -1717171717;
CWinApp* pApp = AfxGetApp();
// Get position from registry
int nX = pApp->GetProfileInt(strSection, _T("X"), nInvalidValue);
int nY = pApp->GetProfileInt(strSection, _T("Y"), nInvalidValue);
if (!(uFlags & CAMSWnd::Position) || (nX == nInvalidValue && nY == nInvalidValue))
uSWPflags |= SWP_NOMOVE;
// Get size from registry
int nWidth = pApp->GetProfileInt(strSection, _T("Width"), nInvalidValue);
int nHeight = pApp->GetProfileInt(strSection, _T("Height"), nInvalidValue);
if (!(uFlags & CAMSWnd::Size) || (nWidth == nInvalidValue && nHeight == nInvalidValue))
uSWPflags |= SWP_NOSIZE;
// Set it!
pWnd->SetWindowPos(NULL, nX, nY, nWidth, nHeight, uSWPflags);
int nState = pApp->GetProfileInt(strSection, _T("State"), nInvalidValue);
if ((uFlags & CAMSWnd::State) && (nState != nInvalidValue))
{
WINDOWPLACEMENT wp;
pWnd->GetWindowPlacement(&wp);
// If the state was relevant, refresh the window with it
wp.showCmd = nState;
pWnd->SetWindowPlacement(&wp);
}
}
// Changes the position and/or size of the given pWnd window by nPixels,
// The uFlags parameter determines what is being changed.
void CAMSWnd::ChangeBy(CWnd* pWnd, int nPixels, unsigned uFlags)
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
ASSERT(uFlags);
if (nPixels == 0)
return;
// Get window rectangle
CRect rectWindow;
pWnd->GetWindowRect(rectWindow);
// Change position
if (uFlags & CAMSWnd::Left)
rectWindow.left += nPixels;
if (uFlags & CAMSWnd::Right)
rectWindow.right += nPixels;
if (uFlags & CAMSWnd::Top)
rectWindow.top += nPixels;
if (uFlags & CAMSWnd::Bottom)
rectWindow.bottom += nPixels;
// Change size
if (uFlags & CAMSWnd::Width)
rectWindow.right += nPixels;
if (uFlags & CAMSWnd::Height)
rectWindow.bottom += nPixels;
// Modify coordinates if it's a child window
CWnd* pWndParent = pWnd->GetParent();
if (pWndParent && pWndParent->IsChild(pWnd))
pWndParent->ScreenToClient(rectWindow);
// Do it!
pWnd->MoveWindow(rectWindow, !(uFlags & NoRedraw));
}
// Aligns the given uCtrlToAlign based on the current position and/or size of the uCtrlToAlignAgainst. Additionally the aligned
// control may be placed by nOffset pixels away from uCtrlToAlignAgainst.
// The uFlags may be any combination of Left, Right, Top, Bottom, Width, or Height.
void CAMSWnd::AlignControl(CWnd* pWnd, unsigned uCtrlToAlign, unsigned uCtrlToAlignAgainst, unsigned uFlags, int nOffset /*= 0*/)
{
ASSERT(pWnd);
ASSERT(uFlags);
// Get each control's window
CWnd* pWndToAlign= pWnd->GetDlgItem(uCtrlToAlign);
CWnd* pWndToAlignAgainst = pWnd->GetDlgItem(uCtrlToAlignAgainst);
ASSERT(pWndToAlign);
ASSERT(pWndToAlignAgainst);
// Get each control's dimensions
CRect rectToAlign, rectToAlignAgainst;
pWndToAlign->GetWindowRect(rectToAlign);
pWndToAlignAgainst->GetWindowRect(rectToAlignAgainst);
unsigned uNoRedraw = (uFlags & CAMSWnd::NoRedraw);
// Change each item individually
if (uFlags & CAMSWnd::Left)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.left - rectToAlign.left) + nOffset, CAMSWnd::Left | uNoRedraw);
if (uFlags & CAMSWnd::Top)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.top - rectToAlign.top) + nOffset, CAMSWnd::Top | uNoRedraw);
if (uFlags & CAMSWnd::Right)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.right - rectToAlign.right) + nOffset, CAMSWnd::Right | uNoRedraw);
if (uFlags & CAMSWnd::Bottom)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.bottom - rectToAlign.bottom) + nOffset, CAMSWnd::Bottom | uNoRedraw);
pWndToAlign->GetWindowRect(rectToAlign);
if (uFlags & CAMSWnd::Width)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.Width() - rectToAlign.Width()) + nOffset, CAMSWnd::Width | uNoRedraw);
if (uFlags & CAMSWnd::Height)
CAMSWnd::ChangeBy(pWndToAlign, (rectToAlignAgainst.Height() - rectToAlign.Height()) + nOffset, CAMSWnd::Height | uNoRedraw);
}
// Lines up the given set of sequentially numbered controls horizontally or vertically by the given number of pixels (nDistance).
// As a result every control ends up looking exactly like the first one but is separated by nDistance pixels below or to the right of it.
void CAMSWnd::OrganizeSequentialControls(CWnd* pWnd, unsigned uFirstCtrl, unsigned uLastCtrl, int nDistance, unsigned uFlags /*= CAMSWnd::Y*/)
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
ASSERT(nDistance > 0);
// Get the first control's rectangle
CRect rectRef, rectCtrl;
pWnd->GetDlgItem(uFirstCtrl)->GetWindowRect(rectRef);
pWnd->ScreenToClient(rectRef);
long& nIncrement = ((uFlags & CAMSWnd::Y)? rectCtrl.top : rectCtrl.left);
BOOL bRepaint = !(uFlags & CAMSWnd::NoRedraw);
// Loop thorugh every subsequent control and align it on the same column
for (UINT iCtrl = uFirstCtrl + 1; iCtrl <= uLastCtrl; iCtrl++)
{
rectCtrl.left = rectRef.left;
rectCtrl.top = rectRef.top;
nIncrement += (nDistance * (iCtrl - uFirstCtrl));
rectCtrl.right = rectCtrl.left + rectRef.Width();
rectCtrl.bottom = rectCtrl.top + rectRef.Height();
pWnd->GetDlgItem(iCtrl)->MoveWindow(rectCtrl, bRepaint);
}
}
// Returns the number of pixels it takes to display the given text on the window's device context.
// If pFont is not NULL, it is temporarily selected into the window's device context.
int CAMSWnd::GetTextExtent(CWnd* pWnd, const CString& strText, CFont* pFont /*= NULL*/)
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
int nExtent = 0;
int nLen = strText.GetLength();
if (nLen)
{
TEXTMETRIC tm;
CDC* pDC = pWnd->GetDC();
CFont* pFontOld = pDC->SelectObject(pFont ? pFont : pWnd->GetFont());
pDC->GetTextMetrics(&tm);
CSize size = pDC->GetTextExtent(strText, nLen);
nExtent = size.cx + tm.tmAveCharWidth;
pDC->SelectObject(pFontOld);
pWnd->ReleaseDC(pDC);
}
return nExtent;
}
///////////////////////////////////////////////////////////////////////////////
// CAMSWnd::FocusHolder
// Version 1: Constructs the object with a pointer to the window whose focus will be set later.
CAMSWnd::FocusHolder::FocusHolder(CWnd* pWnd) :
m_pWnd(pWnd)
{
}
// Version 2: Constructs the object with a reference to the window whose focus will be set later.
CAMSWnd::FocusHolder::FocusHolder(CWnd& wnd) :
m_pWnd(&wnd)
{
}
// Destroys the object and sets the focus to the window passed in construction.
CAMSWnd::FocusHolder::~FocusHolder()
{
ASSERT(m_pWnd);
if (::IsWindow(m_pWnd->m_hWnd))
m_pWnd->SetFocus();
}
///////////////////////////////////////////////////////////////////////////////
// CAMSWnd::Disabler
// Version 1: Constructs the object with a pointer to the window to disable temporarily.
CAMSWnd::Disabler::Disabler(CWnd* pWnd) :
m_pWnd(pWnd)
{
ASSERT(m_pWnd);
m_pWnd->EnableWindow(FALSE);
}
// Version 2: Constructs the object with a reference to the window to disable temporarily.
CAMSWnd::Disabler::Disabler(CWnd& wnd) :
m_pWnd(&wnd)
{
m_pWnd->EnableWindow(FALSE);
}
// Destroys the object and reenables the window.
CAMSWnd::Disabler::~Disabler()
{
if (::IsWindow(m_pWnd->m_hWnd))
m_pWnd->EnableWindow(TRUE);
}
///////////////////////////////////////////////////////////////////////////////
// CAMSWnd::Hook
// Map of windows which have been hooked and their corresponding Hook-derived objects.
CMap<HWND, HWND&, CAMSWnd::Hook*, CAMSWnd::Hook*&> CAMSWnd::Hook::m_mapHookedWindows;
// Constructs the object using the given pWnd object pointer.
CAMSWnd::Hook::Hook(CWnd* pWnd /*= NULL*/) :
m_hWnd(NULL),
m_pWndProcOld(NULL)
{
if (pWnd)
Open(pWnd);
}
// Destroys the object by closing the hook first.
CAMSWnd::Hook::~Hook()
{
Close();
}
// Opens the hook for the given window by attaching self's HookedWindowProc to it
BOOL CAMSWnd::Hook::Open(CWnd* pWnd)
{
ASSERT(pWnd);
m_hWnd = pWnd->GetSafeHwnd();
ASSERT(::IsWindow(m_hWnd)); // window must already exist
if (!(m_pWndProcOld = (WNDPROC)::SetWindowLong(m_hWnd, GWL_WNDPROC, (DWORD)Hook::HookedWindowProc)))
return FALSE;
m_mapHookedWindows[m_hWnd] = this;
return TRUE;
}
// Closes the hook and restores the old window procedure
void CAMSWnd::Hook::Close()
{
if (!IsOpen())
return;
if (::IsWindow(m_hWnd))
::SetWindowLong(m_hWnd, GWL_WNDPROC, (DWORD)m_pWndProcOld);
m_mapHookedWindows.RemoveKey(m_hWnd);
m_hWnd = NULL;
m_pWndProcOld = NULL;
}
// Returns true if the hook has been opened
BOOL CAMSWnd::Hook::IsOpen() const
{
return (m_hWnd && m_pWndProcOld);
}
// Returns a temporary CWnd pointer for the window which has been hooked
CWnd* CAMSWnd::Hook::GetWindow() const
{
return CWnd::FromHandle(m_hWnd);
}
// Returns the handle of the window being hooked (if any)
HWND CAMSWnd::Hook::GetHwnd() const
{
return m_hWnd;
}
// The window procedure through which the messages will be routed
LRESULT CALLBACK CAMSWnd::Hook::HookedWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
#ifdef _USRDLL
// If this is a DLL, need to set up MFC state
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
// Set up MFC message state just in case anyone wants it
// This is just like AfxCallWindowProc, but we can't use that because
// a CAMSWnd::Hook is not a CWnd.
//
MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
MSG oldMsg = curMsg; // save for nesting
curMsg.hwnd = hWnd;
curMsg.message = message;
curMsg.wParam = wParam;
curMsg.lParam = lParam;
// Get hook object for this window from the map
Hook* pHook = NULL;
VERIFY(m_mapHookedWindows.Lookup(hWnd, pHook));
// pass to msg hook
LRESULT result = pHook->WindowProc(message, wParam, lParam);
curMsg = oldMsg; // pop state
return result;
}
LRESULT CAMSWnd::Hook::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
return ::CallWindowProc(m_pWndProcOld, m_hWnd, message, wParam, lParam);
}
LRESULT CAMSWnd::Hook::Default()
{
// MFC stores current MSG in thread state
MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
// Note: must explicitly call CAMSWnd::Hook::WindowProc to avoid infinite
// recursion on virtual function
return Hook::WindowProc(curMsg.message, curMsg.wParam, curMsg.lParam);
}
///////////////////////////////////////////////////////////////////////////////
// CAMSWnd::PlacementHook
// Constructs the object with no flags set
CAMSWnd::PlacementHook::PlacementHook() :
m_uFlags(0)
{
}
// Hooks the given window and sets the flag to restore both the position and size
// when the window is shown.
void CAMSWnd::PlacementHook::SetLastPositionAndSize(CWnd* pWnd, LPCTSTR szWindowName /*= NULL*/)
{
Open(pWnd, szWindowName);
m_uFlags = Both;
}
// Hooks the given window and sets the flag to restore the position when the window is shown.
void CAMSWnd::PlacementHook::SetLastPosition(CWnd* pWnd, LPCTSTR szWindowName /*= NULL*/)
{
Open(pWnd, szWindowName);
m_uFlags = Position;
}
// Hooks the given window and sets the flag to restore the size when the window is shown.
void CAMSWnd::PlacementHook::SetLastSize(CWnd* pWnd, LPCTSTR szWindowName /*= NULL*/)
{
Open(pWnd, szWindowName);
m_uFlags = Size;
}
// Hooks the given window and stores the window name to use for saving/restoring in the app's profile.
void CAMSWnd::PlacementHook::Open(CWnd* pWnd, LPCTSTR szWindowName)
{
VERIFY(Hook::Open(pWnd));
if (szWindowName && *szWindowName)
m_strWindowName = szWindowName;
else
pWnd->GetWindowText(m_strWindowName);
}
// Handles the window's show/destroy messages to restore/save the window's position and/or size.
LRESULT CAMSWnd::PlacementHook::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SHOWWINDOW:
if (wParam)
CAMSWnd::Restore(GetWindow(), m_strWindowName, m_uFlags);
break;
case WM_DESTROY:
CAMSWnd::Save(GetWindow(), m_strWindowName);
break;
}
return Hook::WindowProc(message, wParam, lParam);
}