Created a CMFCDropDownToolBar
in CMainFrame::OnCreate
:
// Loading toolbar with a single icon/entry.
if (!m_wndMyDropdownToolBar.Create(this,
WS_CHILD|CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_HIDE_INPLACE|CBRS_SIZE_DYNAMIC|
CBRS_GRIPPER | CBRS_BORDER_3D,
ID_ACTION_BUTTON) ||
!m_wndMyDropdownToolBar.LoadToolBar (IDR_TOOLBAR_DROPDOWNSELECT))
{
TRACE0("Failed to create build toolbar\n");
return FALSE; // fail to create
}
// build list - use existing icon
for (UINT i=0; i<2; i++) {
CString s;
s.Format(_T("%i: Whatever %i\n"), i, i);
m_wndMyDropdownToolBar.InsertButton(CMFCToolBarButton(ID_COMMAND_START+i, 0, s));
};
Then the rest:
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM idtoolbar, LPARAM)
{
if (idtoolbar==IDR_MAINFRAME) {
ASSERT(m_wndMyDropdownToolBar.GetSafeHwnd() != NULL);
//-----------------------------------
// Replace dropdown button:
//-----------------------------------
m_wndToolBar.ReplaceButton(ID_ACTION_BUTTON_DUMMY,
CMFCDropDownToolbarButton(_T("Text that doesn't show anywhere"), &m_wndMyDropdownToolBar));
}
return 0;
}
BOOL CMainFrame::GetToolbarButtonToolTipText(CMFCToolBarButton* pButton, CString& strTTText)
{
if (pButton->m_nID>=ID_COMMAND_START && pButton->m_nID<=ID_COMMAND_END) {
// use text
strTTText=pButton->m_strText;
return TRUE;
}
return FALSE;
}
It all works fine as far as the drop down shows the buttons, hovering shows the tooltip, but when you select one of the buttons added manually via the InsertButton()
call, the button on the main toolbar is blank and no tooltips. If I then go back and select first button (part of resource loaded), it shows the correct icon and tool tip.
What am I not doing correct?
Thanks!!
The problem is that
CMFCDropDownToolbarButton::SetDefaultCommand(UINT uiCmd)
is flawed. If not abUserButton
type then it assumes the index to them_ImagesLocked
is by position instead of taking the buttonsiIndex
. You can't usebUserButton
and supportbLargeButtons
since the user buttons have to be the same size. Also the button that sits on the main toolbar takes on the first itemsm_strText
(normally empty string since loaded with toolbar) so the tooltip updates viaCMainFrame::GetToolbarButtonToolTipText()
above don't work either (it should pull in the buttons text but doesn't). So probably test best thing to do is takeafxdropdowntoolbar.cpp
and duplicate it in the project then fix it with a new class name. A little code bloat, if onlySetDefaultCommand()
was virtual, could fix it easily.Also note, if using
LoadBitmap
/LoadBitmapEx
, you should setbLocked
to TRUE (as well as when usingInsertButton()
)Have it all working the way it should have to start with, if you need this, here's the core changes:
Ugh, needed to override the protected CMFCToolBarImages::CopyTemp() so instead of having to redo almost all of the MFC toolbar support I added this hack! I doesn't add any data members so should maintain compatiblity with CMFCToolBar.
Have it call CMainFrame for tooltip:
For the actual first fix needed that started this whole thing:
To get the tool tips to look consistent with bold and normal, had it use CMainFrame::GetMessageString:
Also needed to change CMyDropDownFrame::OnCreate() to deal with protect member: