Visual C++ 菜单
时间:2011-04-05 来源:Clingingboy
2.1 如何动态添加和删除菜单项
- AppendMenu
- InsertMenu
- DeleteMenu
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnAppendMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 4)
{
//在菜单末端添加菜单项
pSubMenu->AppendMenu(MF_STRING, ID_TEST_MENU, _T("新菜单项"));
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnInsertMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 4)
{
//在菜单指定位置添加菜单项
pSubMenu->InsertMenu(4, MF_BYPOSITION, ID_TEST_MENU, _T("新菜单项"));
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnRemoveMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 5)
{
//删除菜单项
pSubMenu->RemoveMenu(4, MF_BYPOSITION);
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnDeleteMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 5)
{
//删除菜单项
pSubMenu->DeleteMenu(4, MF_BYPOSITION);
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnTestMenu()
{
AfxMessageBox(_T("测试菜单项命令"));
}
2.2 如何在系统菜单中添加菜单项
- GetSystemMenu
//获得系统菜单
CMenu* pMenu = GetSystemMenu(FALSE);
//删除系统菜单项
pMenu->RemoveMenu(SC_MOVE, MF_STRING);
//添加菜单项
pMenu->InsertMenu(0, MF_BYPOSITION, ID_TEST_MENU, _T("测试菜单项"));
//重画菜单
DrawMenuBar();
2.3 如何禁用关闭按钮
- EnableMenuItem
//获得系统菜单
CMenu* pMenu = GetSystemMenu(FALSE);
//禁用关闭按钮
pMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
2.4 如何启用和禁用菜单项
CCmdUI的Enable方法
void CMainFrame::OnTestMenu1()
{
m_bEnable1 = FALSE;
m_bEnable2 = TRUE;
}
void CMainFrame::OnUpdateTestMenu1(CCmdUI* pCmdUI)
{
//启用或禁用菜单1
pCmdUI->Enable(m_bEnable1);
}
void CMainFrame::OnTestMenu2()
{
m_bEnable1 = TRUE;
m_bEnable2 = FALSE;
}
void CMainFrame::OnUpdateTestMenu2(CCmdUI* pCmdUI)
{
//启用或禁用菜单2
pCmdUI->Enable(m_bEnable2);
}
2.5 如何设置菜单项的检查状态
SetCheck方法
void CMainFrame::OnUpdateTestMenu(CCmdUI* pCmdUI)
{
//设置菜单项检查状态
pCmdUI->SetCheck(m_nCheck);
}
2.6 如何实现快捷菜单
即右键菜单ContextMenu
void CDemoView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
//加载菜单
if (!menu.LoadMenu(IDR_MENU))
{
return;
}
//获得子菜单
CMenu* pPopupMenu = menu.GetSubMenu(0);
//弹出菜单
pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y, pWnd);
}
void CDemoView::OnTestMenu1()
{
AfxMessageBox(_T("快捷菜单项1命令"));
}
void CDemoView::OnTestMenu2()
{
AfxMessageBox(_T("快捷菜单项2命令"));
}
相关阅读 更多 +