BCGControlBar菜单编程方法详解
菜单常用控制:
动态的替换菜单使用如下方法
其实CBCGPMenuBar是继承于CBCGPToolBar,菜单可以看作是按钮来替换
在其加载时即可替换
CMainFrame中创建的CBCGPMenuBar
CBCGPMenuBar m_wndMenuBar;
替换
m_wndMenuBar.ReplaceButton(ID_XXX, CBCGPToolbarMenuButton(IDS_EDIT_MYITEM_1, NULL, -1, _T("&MyItem 1")), FALSE);
m_wndMenuBar.AdjustSizeImmediate();
另一种方法
在CMainFrame中重写
virtual BOOL OnShowPopupMenu (CBCGPPopupMenu* pMenuPopup)
...{
//------------- Example --------------------//
// we need to find {Dynamic Command} menu item (it's a dummy item)
// that should be replaced to our dynamic menu items
// in customize mode we should leave the menu as is
int iIndex = -1;
if (!CBCGPToolBar::IsCustomizeMode () &&
(iIndex = pMenuPopup->GetMenuBar ()->CommandToIndex (ID_DYNAMIC_COMMANDS)) >= 0)
...{
// remove the {Dynamic Command} item in noncustomize mode
pMenuPopup->RemoveItem (iIndex);
pMenuPopup->InsertSeparator (iIndex); // insert the separator at the end
// IDS_EDIT_MYITEM_1 and IDS_EDIT_MYITEM_1 should be defined in the string table
// for status text and tooltip
pMenuPopup->InsertItem (
CBCGPToolbarMenuButton (IDS_EDIT_MYITEM_1, NULL, -1, _T("&MyItem 1")), iIndex + 1);
pMenuPopup->InsertItem (
CBCGPToolbarMenuButton (IDS_EDIT_MYITEM_2, NULL, -1, _T("MyItem &2")), iIndex + 2);
// don't forget to add message handlers (ON_COMMAND) to the message map
}
//-------------------------------------------//
return TRUE;
}
任意位置弹出右键菜单的方法
在需要弹出位置使用如下方法
...{
CPoint pt;
GetCursorPos(&pt); //取鼠标位置
HMENU hMenu = ::CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 10001, "打开任务处理对话框");
AppendMenu(hMenu, MF_STRING, 10002, "定义颜色与图像");
AppendMenu(hMenu, MF_STRING, 10003, "恢复默认设置");
theApp.GetContextMenuManager ()->ShowPopupMenu (
hMenu, pt.x, pt.y, this,
TRUE);
}
为菜单添加图片
首先需要在资源中增加toolbar,来收集需要图片的菜单项,在toolbar资源中ID与需要图片的菜单ID一致且按图片顺序排列于toolbar资源上,然后在CMainFrame中OnCreate中加入
// IDR_MENUIMAGE为菜单toolbar资源IDB_BMPMENU菜单图片
CBCGPToolBar::AddToolBarForImageCollection(IDR_MENUIMAGE, IDB_BMPMENU);
另一种方法
在CMainFrame中重写
virtual BOOL OnDrawMenuImage ( CDC* pDC,
const CBCGPToolbarMenuButton* pMenuButton,
const CRect& rectImage)
...{
ASSERT_VALID (pDC);
ASSERT_VALID (pMenuButton);
if (pMenuButton->m_nID == ID_DYNAMIC_ITEM_2)//ID_DYNAMIC_ITEM_2为菜单项ID
...{
// 请加载菜单图片或图标然后自己绘制
CBrush br (RGB (0, 0, 255));
CRect rect = rectImage;
rect.DeflateRect (2, 2);
pDC->FillRect (rect, &br);
return TRUE;
}
}
其它特色方法可以参考安装目录BCGSoft\BCGControlBarPro\Samples\下的例子