MFC:如何使CMFCToolBarComboBoxButton显示在CMFCToolBar上? [英] MFC: How do you get CMFCToolBarComboBoxButton to show on a CMFCToolBar?

查看:403
本文介绍了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为ID_EDIT_FIND_COMBO(我也在其中加了一个圆圈,以了解它的用途).

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中,我在消息映射和OnToolbarReset()函数中添加了ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, 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:

a.使用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来使其进行更改,然后选择Reset All,否则,您将在Computer\HKEY_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 Computer\HKEY_CURRENT_USER\Software\{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上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆