MFC:如何让 CMFCToolBarComboBoxButton 显示在 CMFCToolBar 上?

MFC: How do you get CMFCToolBarComboBoxButton to show on a CMFCToolBar?(MFC:如何让 CMFCToolBarComboBoxButton 显示在 CMFCToolBar 上?)

本文介绍了MFC:如何让 CMFCToolBarComboBoxButton 显示在 CMFCToolBar 上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为很容易的事情我无法开始工作.如何让 ComboBox 显示在 CMFCToolBar 上?这是我所做的不起作用的事情(这意味着它只显示带有圆圈的原始占位符按钮(灰色)).

Something I thought would be easy I can't get to work. How do I get a ComboBox to show on the CMFCToolBar? Here's what I have done that doesn't work (meaning it just shows the original placeholder button with the circle (grayed out)).

1 - 我在资源编辑器的工具栏上添加了一个按钮,并赋予它 ID_EDIT_FIND_COMBO 的 ID(我还在其中放置了一个圆圈,只是为了知道它正在使用中).

1 - I added a button to the toolbar in resource editor giving it the id ID_EDIT_FIND_COMBO (I also put a circle in it just to know it's in use).

2- 在 CMainFrame 我添加了 ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset) 到消息映射和下面的 OnToolbarReset() 函数:

2- in CMainFrame I added ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset) to message map and the OnToolbarReset() function below:

afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wparm, LPARAM )
{
   UINT uitoolbarid = (UINT) wparm;
   if (uitoolbarid == IDR_MAINFRAME) {
     CMFCToolBarComboBoxButton btncombo(ID_EDIT_FIND_COMBO, GetCmdMgr()->GetCmdImage(ID_EDIT_FIND));
     m_wndToolBar.ReplaceButton(ID_EDIT_FIND_COMBO, btncombo);
   }

   return 0;
}

ReplaceButton() 返回 1 表示已替换它.

The ReplaceButton() returns 1 saying it replaced it.

我错过了什么?

为了清楚起见,这是 MS 所说的,即上面所做的:

To be clear, here's what MS says to do which is what is done above:

1 - 为父工具栏资源中的按钮保留一个虚拟资源 ID.有关如何使用 Visual Studio 中的工具栏编辑器创建按钮的详细信息,请参阅工具栏编辑器一文.

1 - Reserve a dummy resource ID for the button in the parent toolbar resource. For more information about how to create buttons by using the Toolbar Editor in Visual Studio, see the Toolbar Editor article.

2 - 为父工具栏的所有位图中的按钮保留工具栏图像(按钮图标).

2 - Reserve a toolbar image (button icon) for the button in all bitmaps of the parent toolbar.

3 - 在处理 AFX_WM_RESETTOOLBAR 消息的消息处理程序中,执行以下步骤:

3 - In the message handler that processes the AFX_WM_RESETTOOLBAR message, do the following steps:

一个.使用 CMFCToolbarButton 派生类构造按钮控件.

a. Construct the button control by using a CMFCToolbarButton-derived class.

b.使用 CMFCToolBar::ReplaceButton 将虚拟按钮替换为新控件.您可以在堆栈上构造按钮对象,因为 ReplaceButton 复制按钮对象并维护副本.

b. Replace the dummy button with the new control by using CMFCToolBar::ReplaceButton. You can construct the button object on the stack, because ReplaceButton copies the button object and maintains the copy.

TIA!!

这是一个不起作用的示例项目.只是工具栏上的按钮,而不是组合框.甚至从 MS 示例中获取代码示例.

Here's a sample project where it doesn't work. Just the button on the toolbar, not a combobox. Even took code sample from MS sample.

示例项目

推荐答案

答案是 MFC 会缓存工具栏,所以如果你在没有组合的情况下运行它,一旦你替换按钮以使用组合它仍然不使用它.您可以通过自定义工具栏(如果您启用了该选项)并选择 Reset All 来使其接受更改,否则,您将在 ComputerHKEY_CURRENT_USER 下找到Software{NameAsUsedInSetRegistryCall}{appname}Workspace 所有缓存项,其中各个MFCToolBar 项.删除密钥并再次运行应用程序,然后它就可以工作了.为什么他们不让它变得聪明并且有一个时间戳来知道是否自动更新,不确定?

The answer is that MFC caches the toolbar so if you ever run it without the combo, once you replace the button to use the combo it still doesn't use it. You can make it pick up the change by either going to customize for the toolbars (if you have that option enabled) and choose Reset All, otherwise, you'll find under ComputerHKEY_CURRENT_USERSoftware{NameAsUsedInSetRegistryCall}{appname}Workspace all the cache items, of which the various MFCToolBar entries. Delete the key and run app again, then it works. Why they wouldn't make it smart and have a timestamp to know if to automactially update, not sure?

这篇关于MFC:如何让 CMFCToolBarComboBoxButton 显示在 CMFCToolBar 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MFC:如何让 CMFCToolBarComboBoxButton 显示在 CMFCToolBar 上?

基础教程推荐