SAMPLE: Adding Control Bars to Foundation Classes DialogsID: Q123158
|
In a Microsoft Foundation Classes (MFC) application, you can attach control
bars such as status bars and toolbars to a frame window. However, for many
applications a simple dialog-based user interface is sufficient. MFC does
not provide built-in support for adding control bars to dialogs.
DLGCBR is a sample application that demonstrates how to add a status bar
and toolbar to a dialog. In addition, it demonstrates a number of
techniques related to using a modeless dialog as the main window of an MFC
application.
The following file is available for download from the Microsoft Software
Library:
~ Dlgcbr.exeFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online ServicesFor additional information on Visual C++ versions 4.0 and greater, please see the following article in the Microsoft Knowledge Base:
Q141751 Adding Control Bars to Dialog Boxes in MFC
To add a control bar to a dialog, you must create the control bar as
usual, and then make room for the control bar within the client area of
the dialog. For the control bar to function properly, the dialog must
duplicate some of the functionality of frame windows. If you want
ON_UPDATE_COMMAND_UI handlers to work for the control bars, you also need
to derive new control bar classes, and handle the WM_IDLEUPDATECMDUI
message. If your dialog is not the main window of your application, you
will also need to modify its parent frame window to pass the
WM_IDLEUPDATECMDUI message on to the dialog's control bars.
To make room for a control bar within the client area of the dialog, follow
these steps in your dialog's OnInitDialog() function:
CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
AFX_IDW_CONTROLBAR_LAST,
0, reposQuery, rcClientNow);
CPoint ptOffset(rcClientStart.left - rcClientNow.left,
rcClientStart.top - rcClientNow.top);
ptOffset.y += ::GetSystemMetrics(SM_CYMENU);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
MoveWindow(rcWindow, FALSE);
LRESULT CDlgToolBar::OnIdleUpdateCmdUI(WPARAM wParam,
LPARAM lParam)
{
if (IsWindowVisible())
{
CFrameWnd* pParent = (CFrameWnd*)GetParent();
if (pParent)
OnUpdateCmdUI(pParent, (BOOL)wParam);
}
return 0L;
}
To pass WM_IDLEUPDATECMDUI messages on to dialogs other than the main
window, save dialog pointers in your frame window class and create a
WM_IDLEUPDATECMDUI handler in that class. The handler should send the
WM_IDLEUPDATECMDUI message on to the dialog child windows by using
CWnd::SendMessageToDescendants(). Then perform default processing for the
message within the frame window.
Additional query words: CDialog CStatusBar CToolBar
Keywords : kbprg kbsample kbMFC KbUIDesign kbVC
Version : 1.0 1.5 1.51 2.0
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 21, 1999