BCGControlBar菜单编程方法详解
菜單常用控制:
動態(tài)的替換菜單使用如下方法
其實CBCGPMenuBar是繼承于CBCGPToolBar,菜單可以看作是按鈕來替換
在其加載時即可替換
CMainFrame中創(chuàng)建的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\下的例子
總結
以上是生活随笔為你收集整理的BCGControlBar菜单编程方法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Home Server中
- 下一篇: scull的编译