简体   繁体   English

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

[英]MFC: How do you get CMFCToolBarComboBoxButton to show on a CMFCToolBar?

Something I thought would be easy I can't get to work.我认为很容易的事情我无法上班。 How do I get a ComboBox to show on the CMFCToolBar ?如何让 ComboBox 显示在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 - 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). 1 - 我在资源编辑器的工具栏中添加了一个按钮,为其提供了 ID ID_EDIT_FIND_COMBO(我还在其中放置了一个圆圈,只是为了知道它正在使用中)。

2- in CMainFrame I added ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset) to message map and the OnToolbarReset() function below: 2- 在CMainFrame我添加ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset)到消息 map 和OnToolbarReset() function 下面:

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;
}

The ReplaceButton() returns 1 saying it replaced it. ReplaceButton()返回 1 表示已替换它。

What am I missing?我错过了什么?

To be clear, here's what MS says to do which is what is done above:需要明确的是,这是 MS 所说的,即上面所做的:

1 - Reserve a dummy resource ID for the button in the parent toolbar resource. 1 - 为父工具栏资源中的按钮保留一个虚拟资源 ID。 For more information about how to create buttons by using the Toolbar Editor in Visual Studio, see the Toolbar Editor article.有关如何使用 Visual Studio 中的工具栏编辑器创建按钮的详细信息,请参阅工具栏编辑器一文。

2 - Reserve a toolbar image (button icon) for the button in all bitmaps of the parent toolbar. 2 - 为父工具栏的所有位图中的按钮保留工具栏图像(按钮图标)。

3 - In the message handler that processes the AFX_WM_RESETTOOLBAR message, do the following steps: 3 - 在处理 AFX_WM_RESETTOOLBAR 消息的消息处理程序中,执行以下步骤:

a.一个。 Construct the button control by using a CMFCToolbarButton-derived class.使用 CMFCToolbarButton 派生的 class 构造按钮控件。

b.湾。 Replace the dummy button with the new control by using CMFCToolBar::ReplaceButton.使用 CMFCToolBar::ReplaceButton 将虚拟按钮替换为新控件。 You can construct the button object on the stack, because ReplaceButton copies the button object and maintains the copy.您可以在堆栈上构造按钮 object,因为 ReplaceButton 复制按钮 object 并维护副本。

TIA!!蒂亚!!

Here's a sample project where it doesn't work.这是一个不起作用的示例项目。 Just the button on the toolbar, not a combobox.只是工具栏上的按钮,而不是 combobox。 Even took code sample from MS sample.甚至从 MS 示例中获取代码示例。

Sample Project 示例项目

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.答案是 MFC 缓存工具栏,因此如果您在没有组合的情况下运行它,一旦您更换按钮以使用组合,它仍然不会使用它。 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.您可以通过自定义工具栏(如果您启用了该选项)并选择Reset All来使其接受更改,否则,您将在Computer\HKEY_CURRENT_USER\Software\{NameAsUsedInSetRegistryCall}\{appname}\Workspace下找到Computer\HKEY_CURRENT_USER\Software\{NameAsUsedInSetRegistryCall}\{appname}\Workspace的所有缓存项,其中的各个 MFCToolBar 项。 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?为什么他们不让它变得聪明并且有一个时间戳来知道是否自动更新,不确定?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM