一、 对话框中增加菜单
1、 对话框中增加菜单,只需要在对话框OnInitDialog()函数中增加响应代码即可:
Cmenu menuDlg;
menuDlg.LoadMenu(IDR_MENU_DLG);
SetMenu(&menuDlg);
也可以通过对话框资源,直接选择菜单ID,无需增加任何代码。
2、 对话框中的菜单默认是无法响应ON_UPDATE_COMMAND_UI消息的,对于利用CCmdUI类的SetCheck()等函数设置的状态无法响应,为了与框架中菜单一致,需要在对话框中响应ON_WM_INITMENUPOPUP消息。
a) 在对话框类的.cpp文件,添加一个ON_WM_INITMENUPOPUP入口到消息映射中。
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()
b) 在对话框类的.h文件添加消息函数声明。
afx_msg void OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu);
c) 在对话框类的.cpp文件添加如下函数代码(大部分代码取自WinFrm.cpp文件的函数
CFrameWnd::OnInitMenuPopup):
void CTestDlgDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
ASSERT(pPopupMenu != NULL);
// Check the enabled state of various menu items.
CCmdUI state;
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// Determine if menu is popup in top-level menu and set m_pOther to
// it if so (m_pParentMenu == NULL indicates that it is secondary popup).
HMENU hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
{
CWnd* pParent = this;
// Child windows don't have menus--need to go to the top!
if (pParent != NULL &&
(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
{
int nIndexMax = ::GetMenuItemCount(hParentMenu);
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
{
if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
{
// When popup is found, m_pParentMenu is containing menu.
state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
state.m_nIndex++)
{
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // Menu separator or invalid cmd - ignore it.
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1)
{
// Possibly a popup menu, route to first item of that popup.
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
if (state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1)
{
continue; // First item of popup can't be routed to.
}
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
}
else
{
// Normal menu item.
// Auto enable/disable if frame window has m_bAutoMenuEnable
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// Adjust for menu deletions and additions.
UINT nCount = pPopupMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax)
{
state.m_nIndex -= (state.m_nIndexMax - nCount);
while (state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}
二、 对话框中增加工具栏
1、在资源中添加工具栏资源;
2、在对话框类中定义一个工具栏变量;
3、在对话框的OnInitDialog函数中Create工具栏。程序如下:
在对话框类中定义工具栏成员变量:
CToolBar m_ToolBar;
在OnInitDialog函数中Create:
if(!m_ToolBar.CreateEx(this,TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_TOP
|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_SIZE_DYNAMIC)
||!m_ToolBar.LoadToolBar(IDR_TOOLBAR_GRAPHICS))
{
TRACE0(_T( "创建工具条失败/n "));
return FALSE;
}
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
但要让对话框中的工具栏响应UPDATE_COMMAND_UI消息,做到按下保持的状态仅仅重载UPDATE_COMMAND_UI消息并使用SetCheck是不行的,因为此时的工具栏上的按钮属性为普通的按钮,可以通过下面的方法测试:
if(m_ToolBar.GetButtonStyle(0) == TBBS_BUTTON)
{
AfxMessageBox( "This is Button Style ");
}
必须添加以下的设置:
m_ToolBar.SetButtonStyle(0,TBBS_CHECKBOX);
m_ToolBar.SetButtonStyle(1,TBBS_CHECKBOX);
对于需要响应的按钮都增加以上代码。
(对于利用Xtreme Toolkit 界面库,工具栏使用CXTPToolbar类创建,调用SetButtonStyle()函数进行设置可以省略)