Working with a toolbar that repeats some menu items from the menu of an older MFC application, I worked on this tool hint problem, as well as (1) modifying the toolbar bitmap to include additional icons and (2) providing the user with current feedback the state of the application. My problem is that I have to do most of this manually, and not use various wizards and tools.
What I did was (1) add new members to the CView class to handle additional messages, (2) change the toolbar bitmap to add additional icons using both MS Paint and the resource editor, and (3) Added new event identifiers and event handlers to the message map for the CView derived class.
One problem that I encountered changing the bitmap in the toolbar was that since I inserted the icon, I had to move the existing icon to the right in the bitmap. My first attempt at this led to the change icon being blank on the application toolbar. Then I realized that I needed to add a bit more length to the bitmap in the toolbar. After adding a few more columns to the last icon in the raster toolbar, to make it the standard width in pixels, the icon displays correctly.
For tooltips, I added the following to the message card:
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText)
Then I added the following method to my class to handle notifications for my menu items. As a side note, OnToolTipText()
be the standard method used in the CFrameWnd
and CMDIChildWnd
, however, the CView
comes from CWnd
, like CFrameWnd
, so I doubt that it matters with respect to what the method is named.
inline BOOL CPCSampleView::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult ) { static wchar_t toolTextToggleExportSylk [64] = L"Toggle SYLK export."; static wchar_t toolTextClearWindow [64] = L"Clear the log displayed."; static wchar_t toolTextConnectLan [64] = L"Log on the POS terminal through the LAN."; TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR; switch (pNMHDR->idFrom) { case ID_TOGGLE_SYLK_EXPORT: pTTT->lpszText = toolTextToggleExportSylk; return TRUE; case ID_WINDOW_CLEAR: pTTT->lpszText = toolTextClearWindow; return TRUE; case ID_CONNECT_LAN_ON: pTTT->lpszText = toolTextConnectLan; return TRUE; }
For user feedback on the menu item that switches the file export when executing reports, I presented the following changes on the message map, and then applied the necessary methods. There are two types of messages, so I had to add two methods and two entries for the new entry:
Changes to the resource file were necessary to create a new button for the switch action, and also to add a new menu item for the switch action. I use the same resource identifier for several different things, as they are all separate. Thus, the identifier of the resource string is the same as for the menu item, and the same for the toolbar button to simplify my life and simplify the search for all specific bits and parts.
The toolbar resource file definition looks like this:
IDR_MAINFRAME TOOLBAR 16, 15 BEGIN BUTTON ID_CONNECT_LAN_ON SEPARATOR BUTTON ID_WINDOW_CLEAR SEPARATOR BUTTON ID_TOGGLE_SYLK_EXPORT SEPARATOR BUTTON ID_APP_ABOUT END
And the specific part of the menu that uses the same resource identifier for the switch event identifier is as follows:
MENUITEM "Export to SYLK file", ID_TOGGLE_SYLK_EXPORT
Then, to provide the text of the status bar that is displayed with the mouse, there is the addition of a table of strings:
ID_TOGGLE_SYLK_EXPORT "Toggle export of SYLK format report files for spreadsheets."
A member of the lpszText
structure lpszText
described in the MSDN documentation for the TOOLINFO
structure as follows:
A pointer to a buffer containing the text for the tool, or an identifier for a string resource that contains text. This member is sometimes used to return values. If you need to examine the return value, it must point to a valid buffer of sufficient size. Otherwise, it can be set to NULL. If lpszText is set to LPSTR_TEXTCALLBACK, the control sends a TTN_GETDISPINFO code notification to the owner window to receive the text.
Having examined the existing answer to this question, I wondered about checking the if
for the TTF_IDISHWND
flag. The MSDN documentation for the TOOLINFO
structure says the following:
Indicates that the uId element is a handle to the tool window. If this flag is not set, uId is the identifier of the instrument.