Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / MFC
Article

How to display tooltips for a toolbar in a dialog

Rate me:
Please Sign up or sign in to vote.
4.53/5 (24 votes)
10 Jan 2000 242.2K   59   43
Simple technique for getting tooltips when you have a toolbar in a dialog

It is relatively easy to place a toolbar in a dialog box, but I had a hard time making the tooltips appear since none of the TTN_xxxx message handlers are present in a CDialog derived class. This article describes a simple technique for getting tooltips when you have a toolbar in a dialog. It also gives a brief overview of the steps involved in adding a toolbar to a dialog box.

Step 1

In the dialog's header file you must add a CToolBar instance and an entry in the message map to handle the tooltip messages.

protected:
    CToolBar cToolBar;

at the bottom of the wizard generated message map entries add:

//}}AFX_MSG
afx_msg BOOL OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT* pResult);
DECLARE_MESSAGE_MAP()

Step 2

In dialog's implementation file add the following to the end of OnInitDialog to show the toolbar.

//add the tool bar to the dialog
cToolBar.Create(this);
cToolBar.LoadToolBar(IDR_TOOLBAR);
cToolBar.ShowWindow(SW_SHOW);
cToolBar.SetBarStyle(CBRS_ALIGN_TOP | CBRS_TOOLTIPS | CBRS_FLYBY);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

At the bottom of the message map add the following:

ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()

Finally add the message handler method as entered in the message map:

BOOL CToolBarTipTestDialog::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
    ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

    // if there is a top level routing frame then let it handle the message
    if (GetRoutingFrame() != NULL) return FALSE;

    // to be thorough we will need to handle UNICODE versions of the message also !!
    TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
    TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
    TCHAR szFullText[512];
    CString strTipText;
    UINT nID = pNMHDR->idFrom;

    if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
        pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
    {
        // idFrom is actually the HWND of the tool 
        nID = ::GetDlgCtrlID((HWND)nID);
    }

    if (nID != 0) // will be zero on a separator
    {
        AfxLoadString(nID, szFullText);
        strTipText=szFullText;

#ifndef _UNICODE
        if (pNMHDR->code == TTN_NEEDTEXTA)
        {
            lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));
        }
        else
        {
            _mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
        }
#else
        if (pNMHDR->code == TTN_NEEDTEXTA)
        {
            _wcstombsz(pTTTA->szText, strTipText,sizeof(pTTTA->szText));
        }
        else
        {
            lstrcpyn(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
        }
#endif

        *pResult = 0;

        // bring the tooltip window above other popup windows
        ::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
            SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER);
        
        return TRUE;
    }

    return FALSE;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to make the certain toolbar button disable under certain case? Pin
mfff28-Apr-03 7:45
mfff28-Apr-03 7:45 
GeneralRe: How to make the certain toolbar button disable under certain case? Pin
horsewww5-Sep-04 17:24
horsewww5-Sep-04 17:24 
GeneralToolTips from ToolBar on the StatusBar in Dialog Pin
kostax1-Aug-02 21:57
kostax1-Aug-02 21:57 
GeneralDockable Toolbars in Dialogs Pin
Lindenbluete17-Jul-02 1:07
Lindenbluete17-Jul-02 1:07 
GeneralClass Wizard barfs Pin
Todd.Harvey31-May-02 4:33
Todd.Harvey31-May-02 4:33 
GeneralClass Wizard barfs Pin
Todd.Harvey31-May-02 4:30
Todd.Harvey31-May-02 4:30 
QuestionHow to do the string resource Pin
Todd.Harvey29-May-02 17:10
Todd.Harvey29-May-02 17:10 
AnswerRe: How to do the string resource Pin
Anonymous8-Dec-04 12:02
Anonymous8-Dec-04 12:02 
If you open your toolbar in the resources, select the image and in the menu select "View" -> "Properties" (or just simply select the image and press Alt-Enter) you can change the ID.
GeneralWorked like a champ Pin
29-Apr-02 3:16
suss29-Apr-02 3:16 
GeneralTooltips Notification Message Pin
13-Feb-02 21:38
suss13-Feb-02 21:38 
QuestionWhat about a dialog in dll - it CRASHES! Pin
9-Apr-01 20:37
suss9-Apr-01 20:37 
AnswerRe: What about a dialog in dll - it CRASHES! Pin
14-Jul-01 11:06
suss14-Jul-01 11:06 
AnswerRe: What about a dialog in dll - it CRASHES! Pin
sungee19-Feb-06 15:33
sungee19-Feb-06 15:33 
QuestionWhat about a dialog in dll - it CRASHES! Pin
9-Apr-01 20:37
suss9-Apr-01 20:37 
GeneralJust want i needed - thanks Pin
Ran31-Dec-00 0:44
Ran31-Dec-00 0:44 
GeneralRe: Just want i needed - thanks Pin
23-Apr-01 0:10
suss23-Apr-01 0:10 
GeneralTokenize the string for MFC Pin
csteow25-Sep-00 20:47
csteow25-Sep-00 20:47 
GeneralRe: Tokenize the string for MFC Pin
5-Nov-01 7:13
suss5-Nov-01 7:13 
GeneralTokenize the string for MFC Pin
csteow25-Sep-00 20:46
csteow25-Sep-00 20:46 
QuestionAfxLoadString ? Pin
loujiing3-Sep-00 23:00
loujiing3-Sep-00 23:00 
AnswerRe: AfxLoadString ? Pin
14-Nov-00 21:09
suss14-Nov-00 21:09 
Generalit din't work Pin
muthu4-Jul-00 5:00
muthu4-Jul-00 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.