代码笔记,对话框OnInitDialog中初始化
CMenu *pMenu = GetMenu();
//子菜单
CMenu *pSubMenu = pMenu->GetSubMenu(0);
m_bmpMenu.LoadBitmap(IDB_BITMAP1);
m_bmpMenu2.LoadBitmap(IDB_BITMAP2);
pSubMenu->SetMenuItemBitmaps(0,MF_BYPOSITION,&m_bmpMenu2,&m_bmpMenu);
//创建一个分隔符
pSubMenu->AppendMenu(MF_SEPARATOR);
pSubMenu->AppendMenu(MF_STRING, IDM_MENU_T, "测试");
//加载快捷键
m_AccF7 = ::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));
快捷键添加PreTranslateMessage(MSG* pMsg)
BOOL CMenuDlg::PreTranslateMessage(MSG* pMsg)
{
if(::TranslateAccelerator(GetSafeHwnd(), m_AccF7, pMsg))
{
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
设置一 图标更换
//设置一选中 图标更换
void CMenuDlg::OnUpdateSet1(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CMenu *pMenu = GetMenu();
CMenu *pSubMenu = pMenu->GetSubMenu(0);
if(m_bmpMenuChecked == FALSE)
{
pSubMenu->CheckMenuItem(IDM_SET1, MF_BYCOMMAND | MF_CHECKED);
m_bmpMenuChecked = TRUE;
}else
{
pSubMenu->CheckMenuItem(IDM_SET1, MF_BYCOMMAND | MF_UNCHECKED);
m_bmpMenuChecked = FALSE;
}
UpdateData(FALSE);
}
设置二勾选 取消
//设置二选中 勾选 取消
void CMenuDlg::OnUpdateSet2(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CMenu *pMenu = GetMenu();
CMenu *pSubMenu = pMenu->GetSubMenu(0);
if(m_bMenuChecked == FALSE)
{
pSubMenu->CheckMenuItem(IDM_SET2, MF_BYCOMMAND | MF_CHECKED);
m_bMenuChecked = TRUE;
}else
{
pSubMenu->CheckMenuItem(IDM_SET2, MF_BYCOMMAND | MF_UNCHECKED);
m_bMenuChecked = FALSE;
}
UpdateData(FALSE);
}
右键弹出式菜单
void CMenuDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//弹出菜单
if(!menuPopup)
menuPopup.LoadMenu(IDR_MENU_POPUP);
ClientToScreen(&point);
menuPopup.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
ScreenToClient(&point);
CDialog::OnRButtonDown(nFlags, point);
}