BCGControlBar界面编程方法总结--菜单篇

本文总结了BCGControlBar界面编程中的菜单操作,包括动态替换菜单、在CMainFrame中创建和替换菜单、任意位置弹出右键菜单的方法,以及如何为菜单项添加图片。示例代码和更多特色方法可在BCGSoft提供的示例项目中找到。

菜单常用控制

动态的替换菜单使用如下方法

其实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 (00255));    
        CRect rect 
= rectImage;
        rect.DeflateRect (
22);

        pDC
->FillRect (rect, &br);
        
return TRUE;
    }

}

其它特色方法可以参考安装目录BCGSoft/BCGControlBarPro/Samples/下的例子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值