Search code examples
menutooltiptoolbarmenuitemsubmenu

CMFCToolbar and CMFCMenubar displaying wrong tooltips


Question asked also on https://social.msdn.microsoft.com/Forums/vstudio/en-US/f64c99e5-f490-454f-951f-aee794e9506b/cmfctoolbar-and-cmfcmenubar-displaying-wrong-tooltips

I am using Visual Studio 2010, but I suspect the bug is still present on the 2013 version, as I compared the winfrm.cpp file and the OnToolTipText are exactly equal on the two files.

The bug is happening always at the 7th, 8th or 16th element. Although separators count as elements for this counting, the tooltips do not appear over them.

Element can be a textual MENUITEM, a POPUP menu or toolbar button.

The string table defines are:

#define IDS_SEVEN            7
#define IDS_EIGHT            8
#define IDS_NINE             9
#define IDS_TEN             10
#define IDS_ELEVEN          11
#define IDS_TWELVE          12
#define IDS_THIRTEEN        13
#define IDS_FOURTEEN        14
#define IDS_FIFTEEN         15
#define IDS_SIXTEEN         16

and the STRINGTABLE contents are:

IDS_SEVEN           "Seven"
IDS_EIGHT           "Eight"
IDS_NINE            "Nine"
IDS_TEN             "Ten"
IDS_ELEVEN          "Eleven"
IDS_TWELVE          "Twelve"
IDS_THIRTEEN        "Thirteen"
IDS_FOURTEEN        "Fourteen"
IDS_FIFTEEN         "Fifteen"
IDS_SIXTEEN         "Sixteen"

So, when I hover with the mouse pointer the 7th, 8th or 16th element I get WRONGLY a tooltip displaying the string with that ID (Example:7th element displays "Seven").

It happens with all my toolbars and all my menus with a sufficient number of items, including in the toplevel horizontal menubar.

I discovered that the guilty function is:

BOOL CFrameWnd::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)

as the nID local variable does not get the ABSOLUTE ID of the Command, but the POSITIONAL order of the button, starting with 1 instead of 0.

If I would move the strings IDs out of that range, whenever a user wants to add new strings, the Visual Studio Resource Editor would see that space free and use it, filling it again. And the problem would happen again. So, it is not a valid solution.

I could also define strings for all commands. But as we are maintaining four TFS branches at the same time and the change needs to go to all branches, this change would be potentially very dangerous when doing merges after.

So, please what solution do you recommend to not display those stupid tooltips?

UPDATE: I didn't copy the string table exactly as it was. It was like this:

IDS_SEVEN           "Seven\nSeven"
IDS_EIGHT           "Eight\nEight"
IDS_NINE            "Nine"
IDS_TEN             "Ten"
IDS_ELEVEN          "Eleven"
IDS_TWELVE          "Twelve"
IDS_THIRTEEN        "Thirteen"
IDS_FOURTEEN        "Fourteen"
IDS_FIFTEEN         "Fifteen"
IDS_SIXTEEN         "Sixteen\nSixteen"

Solution

  • In the update I put a stringtable more similar to the one I had. I had not noticed before a common feature of 7, 8 and 16 th strings: they all had '\n' characters in their contents.

    I think nobody will create a menu with more than 512 menuitems nor toolbars with more than 512 buttons, so my solution was to change all strings containing '\n' characters with an ID below 512 to numbers above 512.

    And it works.