随笔 - 1  文章 - 1218  评论 - 155  阅读 - 516万

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\下的例子

posted on   carekee  阅读(1553)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示