How to Display Tooltips for Buttons

When you specify the TBSTYLE_TOOLTIPS style, the toolbar creates and manages a tooltip control. The tooltip control is hidden and appears only when users move the pointer over a toolbar button and leave it there for approximately one second.

Your application can supply text to the tooltip control in any one of the following ways:

  • Set the tooltip text as the iString member of the TBBUTTON structure for each button. You must also send a TB_SETMAXTEXTROWS message and set the maximum text rows to 0, so that the text does not appear as the button label rather than as a tooltip.
  • Create the toolbar with the TBSTYLE_LIST style and then set the TBSTYLE_EX_MIXEDBUTTONS extended style. Labels are shown only for buttons that have the BTNS_SHOWTEXT style. For buttons that do not have this style, a tooltip is shown that contains the button text.
  • Respond to the TTN_GETDISPINFO notification code.
  • Respond to the TBN_GETINFOTIP notification code.

An application that needs to send messages directly to the tooltip control can retrieve the handle to the control by using the TB_GETTOOLTIPS message. An application can replace the tooltip control of a toolbar with another tooltip control by using the TB_SETTOOLTIPS message.

The most flexible way of supplying tooltip text is to respond to the TTN_GETDISPINFO or TBN_GETINFOTIP notification code sent by the toolbar control to its parent in the form of a WM_NOTIFY message. For TTN_GETDISPINFO, the lParam parameter includes a pointer to an NMTTDISPINFO structure (also defined as LPTOOLTIPTEXT) that specifies the command identifier of the button for which Help text is needed. This identifier is in the NMTTDISPINFO.hdr.idFrom member. An application can copy the Help text to the structure, specify the address of a string containing the Help text, or specify the instance handle and resource identifier of a string resource.

What you need to know

Technologies

Prerequisites

  • C/C++
  • Windows User Interface Programming

Instructions

Display a Tooltip for a Button

The following example code handles the TTN_GETDISPINFO tooltip notification code by providing text from resource identifiers.

case WM_NOTIFY: 
            
    switch (((LPNMHDR) lParam)->code) 
    {
    
    case TTN_GETDISPINFO: 
        { 
            LPTOOLTIPTEXT lpttt = (LPTOOLTIPTEXT)lParam; 
            
            // Set the instance of the module that contains the resource.
            lpttt->hinst = g_hInst; 
            
            UINT_PTR idButton = lpttt->hdr.idFrom;
            
            switch (idButton) 
            { 
            case IDM_NEW: 
                lpttt->lpszText = MAKEINTRESOURCE(IDS_TIPS_NEW); 
                break; 
                
            case IDM_OPEN: 
                lpttt->lpszText = MAKEINTRESOURCE(IDS_TIPS_OPEN); 
                break; 
                
            case IDM_SAVE: 
                lpttt->lpszText = MAKEINTRESOURCE(IDS_TIPS_SAVE); 
                break; 
            } 
            
            break; 
        } 
    }
    return TRUE;

Using Toolbar Controls

Windows common controls demo (CppWindowsCommonControls)