简体   繁体   中英

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 ? 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).

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

The ReplaceButton() returns 1 saying it replaced it.

What am I missing?

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

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 - Reserve a toolbar image (button icon) for the button in all bitmaps of the parent toolbar.

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

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

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!!

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.

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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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