Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / WTL
Article

WTL MDI command bar control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
5 Dec 20013 min read 111.7K   1.4K   15   19
A WTL CCommandBarCtrl extension to fully support MDI

Sample Image

Introduction

This is a CCommandBarCtrl extension to fully support the MDI architecture.

The implementation consists of three classes:

  • CCommandBarCtrl2 manages the additional MDI buttons.
  • CMDIChildWindowImpl2 forwards the size changes to CCommandBarCtrl2.
  • CMDIChildWinImplTraits2 implements a helper class to keep the maximize state when creating a new MDI child window.
The class definitions and implementations are in the file AtlCmdBar2.h, which is included in the demo project.

Requirements

You will require the WTL Libraries; these can be downloaded from the Microsoft site. If the WTL Libraries have no meaning to you, see Introduction to WTL - Part 1.

How to use the control in your WTL App

First, include the file AtlCmdBar2.h in your applications .cpp file. Then replace the definition of the main frames command bar CCommandBarCtrl m_CmdBar; with CCommandBarCtrl2 m_CmdBar; in mainfrm.h. Finally replace all instances of CMDIChildWindowImpl with CMDIChildWindowImpl2 in ChildFrm.h. That's all.

History

03 Oct 2001

Initial public release.

06 Dec 2001

Added chevron support (including menu band size update).
Fixed MDI system menu injection.
Fixed size problem of MDI system menu.
Fixed flickering of menu bar during window sizing.

About the implementation

The additional MDI buttons are implemented as custom draw buttons of the menu toolbar and are drawn via DrawFrameControl(). These are added/removed in respond to a size change of the MDI child window. Additionally, WM_MDISETMENU has to be handled to insert the MDI child windows menu into the new frame window menu.

Below are detailed explanations of the most interesting functions.

CCommandBarCtrl2

LRESULT OnMDISetMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

Checks, if the MDI child is maximized and modifies the given frame menu before it will be passed to the CCommandBarCtrl handler. Because the menu toolbar is build in the default handler the MDI buttons are added after the call.

LRESULT OnMDIChildSized(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)

Message send from CMDIChildWindowImpl2 when the size of the MDI child window has been changed. Modifies the menu accordingly to the state of the MDI child. The return value is non-zero if the MDI child is maximized.

LRESULT OnHookMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)

Because the file menu button will be drawn as selected when the cursor will be moved over the MDI child icon this handler resets the current hot item.

void UpdateGapWidth()

Updates the width of the gap between the last menu item and the MDI buttons. Called in respond to a size change of the command bar.

bool UpdateMenu(bool fShow, HMENU hMenu)

Inserts or removes the MDI child window menu into the given menu.

void UpdateMDIButtons(bool fShow)

Adds or removes the MDI buttons to the menu toolbar.

void MDIChildChanged(HWND hWndMDIChild)

Modifies the menu and the toolbar according to the state of the given MDI child window.

CMDIChildWindowImpl2

HWND Create(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL, DWORD dwStyle = 0, DWORD dwExStyle = 0, UINT nMenuID = 0, LPVOID lpCreateParam = NULL)
HWND CreateEx(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL, DWORD dwStyle = 0, DWORD dwExStyle = 0, LPVOID lpCreateParam = NULL)

Provided to prevent flickering during the creation of maximized MDI child windows. This is accomplished with setting the redraw flag of the mainframe window.

LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)

When the MDI child window is created maximized the MDI client window edge will be modified to avoid that both windows have the WS_EX_CLIENTEDGE style.

LRESULT OnGetIcon(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)

Loads the icon resource specified in the window class.

LRESULT OnWindowPosChanging(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)

Signals size changes to the CCommandBarCtrlImpl2 and sets the maximized flag in CMDIChildWinImplTraits2.

CMDIChildWinImplTraits2

static DWORD GetWndStyle(DWORD dwStyle)

Adds the WS_MAXIMIZE style if the maximized flag is set.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCompile error due to m_wndMDIClient Pin
dima_zh_n31-Mar-03 21:21
dima_zh_n31-Mar-03 21:21 
GeneralRe: Compile error due to m_wndMDIClient Pin
Bjoern Graf30-Jul-03 19:03
Bjoern Graf30-Jul-03 19:03 
GeneralWon't Compile of NT version 4 Pin
Anonymous10-Oct-02 3:34
Anonymous10-Oct-02 3:34 
GeneralRe: Won't Compile of NT version 4 Pin
Bjoern Graf10-Oct-02 6:28
Bjoern Graf10-Oct-02 6:28 
QuestionDouble-click to close? Pin
David Sullivan16-Aug-02 6:06
David Sullivan16-Aug-02 6:06 
Generalnitpicking Pin
Anonymous9-Jul-02 8:55
Anonymous9-Jul-02 8:55 
Questioncan not receieve WM_KEYDOWN Pin
17-Apr-02 22:41
suss17-Apr-02 22:41 
GeneralRebar band Pin
8-Jan-02 0:30
suss8-Jan-02 0:30 
GeneralAssertion failed Pin
rdo27-Nov-01 3:04
rdo27-Nov-01 3:04 
GeneralRe: Assertion failed Pin
Bjoern Graf2-Dec-01 13:53
Bjoern Graf2-Dec-01 13:53 
Generalbug found Pin
18-Nov-01 20:00
suss18-Nov-01 20:00 
GeneralRe: bug found Pin
Bjoern Graf2-Dec-01 3:07
Bjoern Graf2-Dec-01 3:07 
GeneralImportant change to PrepareChevronMenu... Pin
29-Oct-01 15:56
suss29-Oct-01 15:56 
The new command bar introduces a small bug if you compile with _WIN32_IE >= 0x0500. When the chevron menu is displayed, the "Help" menu item is inserted a couple of times at the bottom of the pop-up menu.

To over come this problem you will need to modify PrepareChevronMenu in ATLFrame.h.

The section of code, after changing, is:
//ATLASSERT(bRet);
// Note: CmdBar currently supports only drop-down items
if (bRet)
{
//ATLASSERT(::IsMenu(mii.hSubMenu));
if (::IsMenu(mii.hSubMenu))
bRet = menu.AppendMenu(MF_STRING | MF_POPUP | (bEnabled ? MF_ENABLED : MF_GRAYED), (UINT_PTR)mii.hSubMenu, mii.dwTypeData);
ATLASSERT(bRet);
}

If you search for the "Note: CmdBar currently supports only drop-down items" in the PrepareChevronMenu function, you'll see the section of code that has to change.
GeneralRe: Important change to PrepareChevronMenu... Pin
Bjoern Graf6-Dec-01 12:23
Bjoern Graf6-Dec-01 12:23 
GeneralProblems in SystemMenu . Pin
22-Oct-01 6:15
suss22-Oct-01 6:15 
GeneralRe: Problems in SystemMenu . Pin
Bjoern Graf2-Dec-01 13:56
Bjoern Graf2-Dec-01 13:56 
GeneralRe: Problems in SystemMenu . Pin
21-Feb-02 1:57
suss21-Feb-02 1:57 
GeneralProblem in Debug build Pin
4-Oct-01 3:40
suss4-Oct-01 3:40 
GeneralRe: Problem in Debug build Pin
Bjoern Graf4-Oct-01 4:12
Bjoern Graf4-Oct-01 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.