MFC 功能包类菜单上的图标

发布于 2024-07-05 10:04:20 字数 302 浏览 8 评论 0原文

在新的 MFC 功能(功能包)中,菜单显示在三个位置:

  • 在菜单栏 (CMFCMenuBar)
  • 弹出菜单 (CMFCPopupMenu)
  • 在 CMFCButton 的“下拉菜单”版本中,

我想放置图标(高色和带有透明度)在所有菜单中。 我找到了 CFrameWndEx::OnDrawMenuImage() ,我可以用它来自定义绘制菜单栏项目前面的图标。 这不是很方便,2008年必须实现图标绘制,但它可以工作。 对于其他人我还没有找到解决方案。 有没有一种自动设置菜单图标的方法?

There are three places where menus show up in the new MFC functionality (Feature Pack):

  • In menu bars (CMFCMenuBar)
  • In popup menus (CMFCPopupMenu)
  • In the 'dropdown menu' version of CMFCButton

I want to put icons (high-color and with transparancy) in the menus in all of them. I have found CFrameWndEx::OnDrawMenuImage() which I can use to custom draw the icons in front of the menu bar items. It's not very convenient, having to implement icon drawing in 2008, but it works. For the others I haven't found a solution yet. Is there an automagic way to set icons for menus?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妞丶爷亲个 2024-07-12 10:04:20

这就是我让它工作的方式:

首先

,正如其他人所说,在主工具栏旁边创建一个不可见的工具栏(我使用基于AppWizard名称的常用名称):

MainFrm.h:
class CMainFrame
{
    //...    
    CMFCToolBar m_wndToolBar;
    CMFCToolBar m_wndInvisibleToolBar;
    //...
};

MainFrm.cpp:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    //...

    // Normal, visible toolbar
    if(m_wndToolBar.Create(this,
        TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
    {
        VERIFY( m_wndToolBar.LoadToolBar(
            theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME) );

        // Only the docking makes the toolbar visible
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
        DockPane(&m_wndToolBar);
    }

    // Invisible toolbar; simply calling Create(this) seems to be enough
    if(m_wndInvisibleToolBar.Create(this))
    {
        // Just load, no docking and stuff
        VERIFY( m_wndInvisibleToolBar.LoadToolBar(IDR_OTHERTOOLBAR) );
    }
}

第二:图像和工具栏资源

IDR_MAINFRAMEIDR_MAINFRAME_256 由 AppWizard 生成。 前者是丑陋的16色版本,后者是有趣的高色版本。
尽管它的名字如此,如果我没记错的话,即使是 AppWizard 生成的图像也具有 24 位颜色深度。 最酷的事情是:只需将其替换为 32 位图像即可。

有一个不可见的工具栏IDR_OTHERTOOLBAR:我用资源编辑器创建了一个工具栏。 只是一些虚拟图标和命令 ID。 然后 VS 生成了一个位图,我用我的高颜色版本替换了它。 完毕!

注意

不要使用资源编辑器打开工具栏:它可能必须先将其转换为 4 位,然后才能对其执行任何操作。 即使如果你让它这样做(因为,在 Visual Studio 的背后,你将再次用高彩色图像替换结果,哈!),我发现它(有时?)根本无法编辑工具栏。 很奇怪。
在这种情况下,我建议直接编辑 .rc 文件。

This is how I got it to work:

First

, as the others said, create an invisible toolbar next to your main toolbar (I'm using the usual names based on AppWizard's names):

MainFrm.h:
class CMainFrame
{
    //...    
    CMFCToolBar m_wndToolBar;
    CMFCToolBar m_wndInvisibleToolBar;
    //...
};

MainFrm.cpp:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    //...

    // Normal, visible toolbar
    if(m_wndToolBar.Create(this,
        TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
    {
        VERIFY( m_wndToolBar.LoadToolBar(
            theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME) );

        // Only the docking makes the toolbar visible
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
        DockPane(&m_wndToolBar);
    }

    // Invisible toolbar; simply calling Create(this) seems to be enough
    if(m_wndInvisibleToolBar.Create(this))
    {
        // Just load, no docking and stuff
        VERIFY( m_wndInvisibleToolBar.LoadToolBar(IDR_OTHERTOOLBAR) );
    }
}

Second: The images and toolbar resources

IDR_MAINFRAME and IDR_MAINFRAME_256 were generated by AppWizard. The former is the ugly 16 color version and the latter is the interesting high color version.
Despite its name, if I remember correctly, even the AppWizard-generated image has 24bit color depth. The cool thing: Just replace it with a 32bit image and that'll work, too.

There is the invisible toolbar IDR_OTHERTOOLBAR: I created a toolbar with the resource editor. Just some dummy icons and the command IDs. VS then generated a bitmap which I replaced with my high color version. Done!

Note

Don't open the toolbars with the resource editor: It may have to convert it to 4bit before it can do anything with it. And even if you let it do that (because, behind Visual Studio's back, wou're going to replace the result with the high color image again, ha!), I found that it (sometimes?) simply cannot edit the toolbar. Very strange.
In that case I advise to directly edit the .rc file.

天冷不及心凉 2024-07-12 10:04:20

我相信(但我可能是错的)这些类与 Microsoft 收购 BCG 时包含在 MFC 中的 BCGToolbar 类相同。 如果是这样,您可以创建一个工具栏,并在工具栏按钮上使用与您要为其创建图标的菜单项中相同的 ID,它们应该会自动显示。 当然,您不必实际显示工具栏。

I believe (but I may be wrong) that these classes are the same as the BCGToolbar classes that were included in MFC when Microsoft bought BCG. If so, you can create a toolbar with and use the same ID on a toolbar button as in the menu items you want to create icons for, and they should appear automatically. Of course, you don't have to actually display the toolbars.

金橙橙 2024-07-12 10:04:20

在BCGToolbar中,在资源和工具栏中创建一个工具栏就足够了。 加载它(但不显示窗口),但工具栏按钮必须与要链接到的菜单项具有相同的 ID。

In BCGToolbar, it's enough to create a toolbar in the resources & load it (but not display the window), but the toolbar button must have the same ID as the menu item you want to link it to.

迷爱 2024-07-12 10:04:20

尝试使用这个功能:

CMFCToolBar::AddToolBarForImageCollection(UINT uiResID,
   UINT uiBmpResID=0,
   UINT uiColdResID=0,
   UINT uiMenuResID=0,
   UINT uiDisabledResID=0,
   UINT uiMenuDisabledResID=0);

所以例如:

CMFCToolBar::AddToolBarForImageCollection(IDR_TOOLBAROWNBITMAP_256);

对我来说效果很好。

Try using this function:

CMFCToolBar::AddToolBarForImageCollection(UINT uiResID,
   UINT uiBmpResID=0,
   UINT uiColdResID=0,
   UINT uiMenuResID=0,
   UINT uiDisabledResID=0,
   UINT uiMenuDisabledResID=0);

So e.g.:

CMFCToolBar::AddToolBarForImageCollection(IDR_TOOLBAROWNBITMAP_256);

Worked very well for me.

伊面 2024-07-12 10:04:20

令人惊讶的一件事是,对于可定制(即非锁定)工具栏,您制作的第一个工具栏,框架会分裂并变成程序中所有图标的某种调色板位图。 如果您稍后尝试添加更多具有与第一个颜色深度不同的位图(或 png)的工具栏(或不同的工具栏),它们似乎会失败,因为它无法将它们添加到同一调色板。

One thing that can catch a person by surprise is that for customizable (ie, non-locked) toolbars, the first toolbar you make, the framework splits up and turns into some sort of palette bitmap of all icons in the program. If you try to add more toolbars later (or different toolbars) that have bitmaps (or pngs) with a different color depth than that first one, they seem to fail because it can't add them to the same palette.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文