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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
// Released: 5th AUgust 2011
//
// David Nash
// email: dnash@bigpond.net.au
// url: https://sourceforge.net/projects/win32-framework
//
//
// Copyright (c) 2005-2011 David Nash
//
// Permission is hereby granted, free of charge, to
// any person obtaining a copy of this software and
// associated documentation files (the "Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
//
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// mdi.h
// Declaration of the CMDIChild and CMDIFrame classes
// The classes defined here add MDI frames support to Win32++. MDI
// (Multiple Document Interface) frames host one or more child windows. The
// child windows hosted by a MDI frame can be different types. For example,
// some MDI child windows could be used to edit text, while others could be
// used to display a bitmap. Four classes are defined here to support MDI
// frames:
// 1) CMDIFrame. This class inherits from CFrame, and adds the functionality
// required by MDI frames. It keeps track of the MDI children created and
// destroyed, and adjusts the menu when a MDI child is activated. Use the
// AddMDIChild function to add MDI child windows to the MDI frame. Inherit
// from CMDIFrame to create your own MDI frame.
//
// 2) CMDIChild: All MDI child windows (ie. CWnd classes) should inherit from
// this class. Each MDI child type can have a different frame menu.
// Use the MDIFrame generic application as the starting point for your own MDI
// frame applications.
// Refer to the MDIDemo sample for an example on how to use these classes to
// create a MDI frame application with different types of MDI child windows.
#ifndef _WIN32XX_MDI_H_
#define _WIN32XX_MDI_H_
#include "frame.h"
#include <vector>
namespace Win32xx
{
class CMDIChild;
class CMDIFrame;
typedef Shared_Ptr<CMDIChild> MDIChildPtr;
/////////////////////////////////////
// Declaration of the CMDIChild class
//
class CMDIChild : public CWnd
{
friend class CMDIFrame;
public:
CMDIChild();
virtual ~CMDIChild();
// These are the functions you might wish to override
virtual HWND Create(CWnd* pParent = NULL);
virtual void RecalcLayout();
// These functions aren't virtual, and shouldn't be overridden
void SetHandles(HMENU MenuName, HACCEL AccelName);
CMDIFrame* GetMDIFrame() const;
CWnd* GetView() const {return m_pView;}
void SetView(CWnd& pwndView);
void MDIActivate() const;
void MDIDestroy() const;
void MDIMaximize() const;
void MDIRestore() const;
protected:
// Its unlikely you would need to override these functions
virtual LRESULT FinalWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
virtual void OnCreate();
virtual LRESULT WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam);
private:
CMDIChild(const CMDIChild&); // Disable copy construction
CMDIChild& operator = (const CMDIChild&); // Disable assignment operator
CWnd* m_pView; // pointer to the View CWnd object
HMENU m_hChildMenu;
HACCEL m_hChildAccel;
};
/////////////////////////////////////
// Declaration of the CMDIFrame class
//
class CMDIFrame : public CFrame
{
friend class CMDIChild; // CMDIChild uses m_hOrigMenu
typedef Shared_Ptr<CMDIChild> MDIChildPtr;
public:
class CMDIClient : public CWnd // a nested class within CMDIFrame
{
public:
CMDIClient() {}
virtual ~CMDIClient() {}
virtual HWND Create(CWnd* pParent = NULL);
virtual LRESULT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
CMDIFrame* GetMDIFrame() const { return (CMDIFrame*)GetParent(); }
private:
CMDIClient(const CMDIClient&); // Disable copy construction
CMDIClient& operator = (const CMDIClient&); // Disable assignment operator
};
CMDIFrame();
virtual ~CMDIFrame() {}
virtual CMDIChild* AddMDIChild(MDIChildPtr pMDIChild);
virtual CMDIClient& GetMDIClient() const { return (CMDIClient&)m_MDIClient; }
virtual BOOL IsMDIFrame() const { return TRUE; }
virtual void RemoveMDIChild(HWND hWnd);
virtual BOOL RemoveAllMDIChildren();
virtual void UpdateCheckMarks();
// These functions aren't virtual, so don't override them
std::vector <MDIChildPtr>& GetAllMDIChildren() {return m_vMDIChild;}
CMDIChild* GetActiveMDIChild() const;
BOOL IsMDIChildMaxed() const;
void MDICascade(int nType = 0) const;
void MDIIconArrange() const;
void MDIMaximize() const;
void MDINext() const;
void MDIPrev() const;
void MDIRestore() const;
void MDITile(int nType = 0) const;
void SetActiveMDIChild(CMDIChild* pChild);
protected:
// These are the functions you might wish to override
virtual void OnClose();
virtual void OnViewStatusBar();
virtual void OnViewToolBar();
virtual void OnWindowPosChanged();
virtual void RecalcLayout();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual LRESULT WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam);
private:
CMDIFrame(const CMDIFrame&); // Disable copy construction
CMDIFrame& operator = (const CMDIFrame&); // Disable assignment operator
void AppendMDIMenu(HMENU hMenuWindow);
LRESULT FinalWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
void UpdateFrameMenu(HMENU hMenu);
CMDIClient m_MDIClient;
std::vector <MDIChildPtr> m_vMDIChild;
HWND m_hActiveMDIChild;
};
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace Win32xx
{
/////////////////////////////////////
// Definitions for the CMDIFrame class
//
inline CMDIFrame::CMDIFrame() : m_hActiveMDIChild(NULL)
{
SetView(GetMDIClient());
}
inline CMDIChild* CMDIFrame::AddMDIChild(MDIChildPtr pMDIChild)
{
assert(NULL != pMDIChild.get()); // Cannot add Null MDI Child
m_vMDIChild.push_back(pMDIChild);
pMDIChild->Create(GetView());
return pMDIChild.get();
}
inline void CMDIFrame::AppendMDIMenu(HMENU hMenuWindow)
{
// Adds the additional menu items the the "Window" submenu when
// MDI child windows are created
if (!IsMenu(hMenuWindow))
return;
// Delete previously appended items
int nItems = ::GetMenuItemCount(hMenuWindow);
UINT uLastID = ::GetMenuItemID(hMenuWindow, --nItems);
if ((uLastID >= IDW_FIRSTCHILD) && (uLastID < IDW_FIRSTCHILD + 10))
{
while ((uLastID >= IDW_FIRSTCHILD) && (uLastID < IDW_FIRSTCHILD + 10))
{
::DeleteMenu(hMenuWindow, nItems, MF_BYPOSITION);
uLastID = ::GetMenuItemID(hMenuWindow, --nItems);
}
//delete the separator too
::DeleteMenu(hMenuWindow, nItems, MF_BYPOSITION);
}
int nWindow = 0;
// Allocate an iterator for our MDIChild vector
std::vector <MDIChildPtr>::iterator v;
for (v = GetAllMDIChildren().begin(); v < GetAllMDIChildren().end(); ++v)
{
if ((*v)->GetWindowLongPtr(GWL_STYLE) & WS_VISIBLE) // IsWindowVisible is unreliable here
{
// Add Separator
if (0 == nWindow)
::AppendMenu(hMenuWindow, MF_SEPARATOR, 0, NULL);
// Add a menu entry for each MDI child (up to 9)
if (nWindow < 9)
{
tString tsMenuItem ( (*v)->GetWindowText() );
if (tsMenuItem.length() > MAX_MENU_STRING -10)
{
// Truncate the string if its too long
tsMenuItem.erase(tsMenuItem.length() - MAX_MENU_STRING +10);
tsMenuItem += _T(" ...");
}
TCHAR szMenuString[MAX_MENU_STRING+1];
wsprintf(szMenuString, _T("&%d %s"), nWindow+1, tsMenuItem.c_str());
::AppendMenu(hMenuWindow, MF_STRING, IDW_FIRSTCHILD + nWindow, szMenuString);
if (GetActiveMDIChild() == (*v).get())
::CheckMenuItem(hMenuWindow, IDW_FIRSTCHILD+nWindow, MF_CHECKED);
++nWindow;
}
else if (9 == nWindow)
// For the 10th MDI child, add this menu item and return
{
::AppendMenu(hMenuWindow, MF_STRING, IDW_FIRSTCHILD + nWindow, _T("&Windows..."));
return;
}
}
}
}
inline LRESULT CMDIFrame::FinalWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return ::DefFrameProc(m_hWnd, GetMDIClient(), uMsg, wParam, lParam);
}
inline CMDIChild* CMDIFrame::GetActiveMDIChild() const
{
return (CMDIChild*)FromHandle(m_hActiveMDIChild);
}
inline BOOL CMDIFrame::IsMDIChildMaxed() const
{
BOOL bMaxed = FALSE;
GetMDIClient().SendMessage(WM_MDIGETACTIVE, 0L, (LPARAM)&bMaxed);
return bMaxed;
}
inline void CMDIFrame::MDICascade(int nType /* = 0*/) const
{
// Possible values for nType are:
// MDITILE_SKIPDISABLED Prevents disabled MDI child windows from being cascaded.
assert(::IsWindow(m_hWnd));
GetView()->SendMessage(WM_MDICASCADE, (WPARAM)nType, 0L);
}
inline void CMDIFrame::MDIIconArrange() const
{
assert(::IsWindow(m_hWnd));
GetView()->SendMessage(WM_MDIICONARRANGE, 0L, 0L);
}
inline void CMDIFrame::MDIMaximize() const
{
assert(::IsWindow(m_hWnd));
GetView()->SendMessage(WM_MDIMAXIMIZE, 0L, 0L);
}
inline void CMDIFrame::MDINext() const
{
assert(::IsWindow(m_hWnd));
HWND hMDIChild = GetActiveMDIChild()->GetHwnd();
GetView()->SendMessage(WM_MDINEXT, (WPARAM)hMDIChild, FALSE);
}
inline void CMDIFrame::MDIPrev() const
{
assert(::IsWindow(m_hWnd));
HWND hMDIChild = GetActiveMDIChild()->GetHwnd();
GetView()->SendMessage(WM_MDINEXT, (WPARAM)hMDIChild, TRUE);
}
inline void CMDIFrame::MDIRestore() const
{
assert(::IsWindow(m_hWnd));
GetView()->SendMessage(WM_MDIRESTORE, 0L, 0L);
}
inline void CMDIFrame::MDITile(int nType /* = 0*/) const
{
// Possible values for nType are:
// MDITILE_HORIZONTAL Tiles MDI child windows so that one window appears above another.
// MDITILE_SKIPDISABLED Prevents disabled MDI child windows from being tiled.
// MDITILE_VERTICAL Tiles MDI child windows so that one window appears beside another.
assert(::IsWindow(m_hWnd));
GetView()->SendMessage(WM_MDITILE, (WPARAM)nType, 0L);
}
inline void CMDIFrame::OnClose()
{
if (RemoveAllMDIChildren())
{
CFrame::OnClose();
Destroy();
}
}
inline void CMDIFrame::OnViewStatusBar()
{
CFrame::OnViewStatusBar();
UpdateCheckMarks();
GetView()->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN);
}
inline void CMDIFrame::OnViewToolBar()
{
CFrame::OnViewToolBar();
UpdateCheckMarks();
GetView()->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN);
}
inline void CMDIFrame::OnWindowPosChanged()
{
if (IsMenuBarUsed())
{
// Refresh MenuBar Window
HMENU hMenu= GetMenuBar().GetMenu();
GetMenuBar().SetMenu(hMenu);
UpdateCheckMarks();
}
}
inline BOOL CMDIFrame::PreTranslateMessage(MSG* pMsg)
{
if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
{
if (TranslateMDISysAccel(GetView()->GetHwnd(), pMsg))
return TRUE;
}
return CFrame::PreTranslateMessage(pMsg);
}
inline void CMDIFrame::RecalcLayout()
{
CFrame::RecalcLayout();
if (GetView()->IsWindow())
MDIIconArrange();
}
inline BOOL CMDIFrame::RemoveAllMDIChildren()
{
BOOL bResult = TRUE;
int Children = (int)m_vMDIChild.size();
// Remove the children in reverse order
for (int i = Children-1; i >= 0; --i)
{
if (IDNO == m_vMDIChild[i]->SendMessage(WM_CLOSE, 0L, 0L)) // Also removes the MDI child
bResult = FALSE;
}
return bResult;
}
inline void CMDIFrame::RemoveMDIChild(HWND hWnd)
{
// Allocate an iterator for our HWND map
std::vector <MDIChildPtr>::iterator v;
for (v = m_vMDIChild.begin(); v!= m_vMDIChild.end(); ++v)
{
if ((*v)->GetHwnd() == hWnd)
{
m_vMDIChild.erase(v);
break;
}
}
if (GetActiveMDIChild())
{
if (GetActiveMDIChild()->m_hChildMenu)
UpdateFrameMenu(GetActiveMDIChild()->m_hChildMenu);
if (GetActiveMDIChild()->m_hChildAccel)
GetApp()->SetAccelerators(GetActiveMDIChild()->m_hChildAccel, this);
}
else
{
if (IsMenuBarUsed())
GetMenuBar().SetMenu(GetFrameMenu());
else
SetMenu(FromHandle(GetFrameMenu()));
GetApp()->SetAccelerators(GetFrameAccel(), this);
}
}
inline void CMDIFrame::SetActiveMDIChild(CMDIChild* pChild)
{
assert ( pChild->IsWindow() );
GetMDIClient().SendMessage(WM_MDIACTIVATE, (WPARAM)pChild->GetHwnd(), 0L);
// Verify
assert ( m_hActiveMDIChild == pChild->GetHwnd() );
}
inline void CMDIFrame::UpdateCheckMarks()
{
if ((GetActiveMDIChild()) && GetActiveMDIChild()->m_hChildMenu)
{
HMENU hMenu = GetActiveMDIChild()->m_hChildMenu;
UINT uCheck = GetToolBar().IsWindowVisible()? MF_CHECKED : MF_UNCHECKED;
::CheckMenuItem(hMenu, IDW_VIEW_TOOLBAR, uCheck);
uCheck = GetStatusBar().IsWindowVisible()? MF_CHECKED : MF_UNCHECKED;
::CheckMenuItem (hMenu, IDW_VIEW_STATUSBAR, uCheck);
}
}
inline void CMDIFrame::UpdateFrameMenu(HMENU hMenu)
{
int nMenuItems = GetMenuItemCount(hMenu);
if (nMenuItems > 0)
{
// The Window menu is typically second from the right
int nWindowItem = MAX (nMenuItems -2, 0);
HMENU hMenuWindow = ::GetSubMenu (hMenu, nWindowItem);
if (hMenuWindow)
{
if (IsMenuBarUsed())
{
AppendMDIMenu(hMenuWindow);
GetMenuBar().SetMenu(hMenu);
}
else
{
GetView()->SendMessage (WM_MDISETMENU, (WPARAM) hMenu, (LPARAM)hMenuWindow);
DrawMenuBar();
}
}
}
UpdateCheckMarks();
}
inline LRESULT CMDIFrame::WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
OnClose();
return 0;
case WM_WINDOWPOSCHANGED:
// MDI Child or MDI frame has been resized
OnWindowPosChanged();
break; // Continue with default processing
} // switch uMsg
return CFrame::WndProcDefault(uMsg, wParam, lParam);
}
inline HWND CMDIFrame::CMDIClient::Create(CWnd* pParent)
{
assert(pParent != 0);
CLIENTCREATESTRUCT clientcreate ;
clientcreate.hWindowMenu = m_hWnd;
clientcreate.idFirstChild = IDW_FIRSTCHILD ;
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | MDIS_ALLCHILDSTYLES;
// Create the view window
CreateEx(WS_EX_CLIENTEDGE, _T("MDICLient"), TEXT(""), dwStyle, 0, 0, 0, 0, pParent, NULL, (PSTR) &clientcreate);
return m_hWnd;
}
inline LRESULT CMDIFrame::CMDIClient::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_MDIDESTROY:
{
// Do default processing first
CallWindowProc(GetPrevWindowProc(), uMsg, wParam, lParam);
// Now remove MDI child
GetMDIFrame()->RemoveMDIChild((HWND) wParam);
}
return 0; // Discard message
case WM_MDISETMENU:
{
if (GetMDIFrame()->IsMenuBarUsed())
{
return 0L;
}
}
break;
case WM_MDIACTIVATE:
{
// Suppress redraw to avoid flicker when activating maximised MDI children
SendMessage(WM_SETREDRAW, FALSE, 0L);
LRESULT lr = CallWindowProc(GetPrevWindowProc(), WM_MDIACTIVATE, wParam, lParam);
SendMessage(WM_SETREDRAW, TRUE, 0L);
RedrawWindow(0, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
return lr;
}
}
return CWnd::WndProcDefault(uMsg, wParam, lParam);
}
/////////////////////////////////////
//Definitions for the CMDIChild class
//
inline CMDIChild::CMDIChild() : m_pView(NULL), m_hChildMenu(NULL)
{
// Set the MDI Child's menu and accelerator in the constructor, like this ...
// HMENU hChildMenu = LoadMenu(GetApp()->GetResourceHandle(), _T("MdiMenuView"));
// HACCEL hChildAccel = LoadAccelerators(GetApp()->GetResourceHandle(), _T("MDIAccelView"));
// SetHandles(hChildMenu, hChildAccel);
}
inline CMDIChild::~CMDIChild()
{
if (IsWindow())
GetParent()->SendMessage(WM_MDIDESTROY, (WPARAM)m_hWnd, 0L);
if (m_hChildMenu)
::DestroyMenu(m_hChildMenu);
}
inline HWND CMDIChild::Create(CWnd* pParent /*= NULL*/)
// We create the MDI child window and then maximize if required.
// This technique avoids unnecessary flicker when creating maximized MDI children.
{
//Call PreCreate in case its overloaded
PreCreate(*m_pcs);
//Determine if the window should be created maximized
BOOL bMax = FALSE;
pParent->SendMessage(WM_MDIGETACTIVE, 0L, (LPARAM)&bMax);
bMax = bMax | (m_pcs->style & WS_MAXIMIZE);
// Set the Window Class Name
TCHAR szClassName[MAX_STRING_SIZE + 1] = _T("Win32++ MDI Child");
if (m_pcs->lpszClass)
lstrcpyn(szClassName, m_pcs->lpszClass, MAX_STRING_SIZE);
// Set the window style
DWORD dwStyle;
dwStyle = m_pcs->style & ~WS_MAXIMIZE;
dwStyle |= WS_VISIBLE | WS_OVERLAPPEDWINDOW ;
// Set window size and position
int x = CW_USEDEFAULT;
int y = CW_USEDEFAULT;
int cx = CW_USEDEFAULT;
int cy = CW_USEDEFAULT;
if(m_pcs->cx && m_pcs->cy)
{
x = m_pcs->x;
y = m_pcs->y;
cx = m_pcs->cx;
cy = m_pcs->cy;
}
// Set the extended style
DWORD dwExStyle = m_pcs->dwExStyle | WS_EX_MDICHILD;
// Turn off redraw while creating the window
pParent->SendMessage(WM_SETREDRAW, FALSE, 0L);
// Create the window
if (!CreateEx(dwExStyle, szClassName, m_pcs->lpszName, dwStyle, x, y,
cx, cy, pParent, FromHandle(m_pcs->hMenu), m_pcs->lpCreateParams))
throw CWinException(_T("CMDIChild::Create ... CreateEx failed"));
if (bMax)
ShowWindow(SW_MAXIMIZE);
// Turn redraw back on
pParent->SendMessage(WM_SETREDRAW, TRUE, 0L);
pParent->RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ALLCHILDREN);
// Ensure bits revealed by round corners (XP themes) are redrawn
SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED);
if (m_hChildMenu)
GetMDIFrame()->UpdateFrameMenu(m_hChildMenu);
if (m_hChildAccel)
GetApp()->SetAccelerators(m_hChildAccel, this);
return m_hWnd;
}
inline CMDIFrame* CMDIChild::GetMDIFrame() const
{
CMDIFrame* pMDIFrame = (CMDIFrame*)GetParent()->GetParent();
assert(dynamic_cast<CMDIFrame*>(pMDIFrame));
return pMDIFrame;
}
inline LRESULT CMDIChild::FinalWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return ::DefMDIChildProc(m_hWnd, uMsg, wParam, lParam);
}
inline void CMDIChild::MDIActivate() const
{
GetParent()->SendMessage(WM_MDIACTIVATE, (WPARAM)m_hWnd, 0L);
}
inline void CMDIChild::MDIDestroy() const
{
GetParent()->SendMessage(WM_MDIDESTROY, (WPARAM)m_hWnd, 0L);
}
inline void CMDIChild::MDIMaximize() const
{
GetParent()->SendMessage(WM_MDIMAXIMIZE, (WPARAM)m_hWnd, 0L);
}
inline void CMDIChild::MDIRestore() const
{
GetParent()->SendMessage(WM_MDIRESTORE, (WPARAM)m_hWnd, 0L);
}
inline void CMDIChild::OnCreate()
{
// Create the view window
assert(GetView()); // Use SetView in CMDIChild's constructor to set the view window
GetView()->Create(this);
RecalcLayout();
}
inline void CMDIChild::RecalcLayout()
{
// Resize the View window
CRect rc = GetClientRect();
m_pView->SetWindowPos( NULL, rc.left, rc.top, rc.Width(), rc.Height(), SWP_SHOWWINDOW );
}
inline void CMDIChild::SetHandles(HMENU hMenu, HACCEL hAccel)
{
m_hChildMenu = hMenu;
m_hChildAccel = hAccel;
// Note: It is valid to call SetChildMenu before the window is created
if (IsWindow())
{
CWnd* pWnd = GetMDIFrame()->GetActiveMDIChild();
if (pWnd == this)
{
if (m_hChildMenu)
GetMDIFrame()->UpdateFrameMenu(m_hChildMenu);
if (m_hChildAccel)
GetApp()->SetAccelerators(m_hChildAccel, GetMDIFrame());
}
}
}
inline void CMDIChild::SetView(CWnd& wndView)
// Sets or changes the View window displayed within the frame
{
if (m_pView != &wndView)
{
// Destroy the existing view window (if any)
if (m_pView) m_pView->Destroy();
// Assign the view window
m_pView = &wndView;
if (m_hWnd)
{
// The frame is already created, so create and position the new view too
assert(GetView()); // Use SetView in CMDIChild's constructor to set the view window
GetView()->Create(this);
RecalcLayout();
}
}
}
inline LRESULT CMDIChild::WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_MDIACTIVATE:
{
// This child is being activated
if (lParam == (LPARAM) m_hWnd)
{
GetMDIFrame()->m_hActiveMDIChild = m_hWnd;
// Set the menu to child default menu
if (m_hChildMenu)
GetMDIFrame()->UpdateFrameMenu(m_hChildMenu);
if (m_hChildAccel)
GetApp()->SetAccelerators(m_hChildAccel, this);
}
// No child is being activated
if (0 == lParam)
{
GetMDIFrame()->m_hActiveMDIChild = NULL;
// Set the menu to frame's original menu
GetMDIFrame()->UpdateFrameMenu(GetMDIFrame()->GetFrameMenu());
GetApp()->SetAccelerators(GetMDIFrame()->GetFrameAccel(), this);
}
}
return 0L ;
case WM_WINDOWPOSCHANGED:
{
RecalcLayout();
break;
}
}
return CWnd::WndProcDefault(uMsg, wParam, lParam);
}
} // namespace Win32xx
#endif // _WIN32XX_MDI_H_
|