It is not difficult to initialize the menu bar in your main dialog window and the CMainDlg
class in the ProfUIS_Controls sample application demonstrates how to do this. First of all you should drop a custom control resource onto your dialog template resource like the IDC_MAIN_MENUBAR
custom control in the IDD_PROFUIS_CONTROLS_DIALOG
dialog template resource in the ProfUIS_Controls sample application. Please specify the ProfUIS-ControlBar string as the Class property, the 0x50402034 value as the Style property and zero as the ExStyle property of the custom control. Now you can add the CExtMenuControlBar m_wndMenuBar;
member to your dialog class declaration and the DDX_Control(pDX, IDC_MAIN_MENUBAR, m_wndMenuBar);
line at the end of the DoDataExchange()
methods body. The OnInitDialog()
method should initialize the command profile in the command manager and load the menu resource into the menu bar:
CWinApp * pApp = ::AfxGetApp();
ASSERT( pApp != NULL );
ASSERT( pApp->m_pszRegistryKey != NULL );
ASSERT( pApp->m_pszRegistryKey[0] != _T(’\0’) );
ASSERT( pApp->m_pszProfileName != NULL );
ASSERT( pApp->m_pszProfileName[0] != _T(’\0’) );
VERIFY(
g_CmdManager->ProfileSetup(
pApp->m_pszProfileName,
GetSafeHwnd()
)
);
VERIFY(
g_CmdManager->UpdateFromMenu(
pApp->m_pszProfileName,
IDR_MAIN_MENU
)
);
ASSERT( m_wndMenuBar.GetSafeHwnd() != NULL );
if( ! m_wndMenuBar.LoadMenuBar( IDR_MAIN_MENU ) )
{
ASSERT( FALSE );
return FALSE;
}
This code assumes that the
IDR_MAIN_MENU
value is the menu resource identifier for the menu bar. If you don’t need the gripper, please use the following code:
m_wndMenuBar.SetBarStyle(
m_wndMenuBar.GetBarStyle() & ~CBRS_GRIPPER
);
Finally the
OnInitDialog()
method should let the menu bar to recompute positions of its buttons and invoke the
CWnd::RepositionBars()
method to let the menu bar be sticked to the dialog border:
m_wndMenuBar.SetWindowPos(
NULL, 0, 0, 0, 0,
SWP_NOSIZE|SWP_NOMOVE
|SWP_NOZORDER|SWP_NOOWNERZORDER
|SWP_FRAMECHANGED
);
CWnd::RepositionBars( 0, 0xFFFF, 0 );
If your dialog is enabled for resizing, then you should implement the
WM_SIZE
message handler like as follows:
void CMainDlg::OnSize( UINT nType, int cx, int cy )
{
CExtResizableDialog::OnSize( nType, cx, cy );
if( nType != SIZE_MINIMIZED )
CWnd::RepositionBars(0,0xFFFF,0);
}