Click here to Skip to main content
11,484,162 members (70,548 online)
Click here to Skip to main content

How to display tooltips for a toolbar in a dialog

, 10 Jan 2000 195.3K 58
Rate this:
Please Sign up or sign in to vote.
Simple technique for getting tooltips when you have a toolbar in a dialog
<!-- Article Starts -->

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

Share

About the Author

Randy More

United States United States
No Biography provided

Comments and Discussions

 
QuestionAfxLoadString Pin
Chris F.15-Sep-11 7:01
memberChris F.15-Sep-11 7:01 
AnswerRe: AfxLoadString Pin
Member 1093212418-Sep-14 2:34
memberMember 1093212418-Sep-14 2:34 
QuestionExcelent!! Pin
donghyuk80kim26-Jul-11 16:14
memberdonghyuk80kim26-Jul-11 16:14 
GeneralIt's better: Pin
Member #1631198-Jan-07 6:24
memberMember #1631198-Jan-07 6:24 
Questiondll-modeless dialogbox-toolbar-tooltips Pin
jgcollins26-Aug-05 13:26
memberjgcollins26-Aug-05 13:26 
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pin
Miles Davies6-Oct-05 4:28
memberMiles Davies6-Oct-05 4:28 
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pin
martypku6-Jun-07 5:36
membermartypku6-Jun-07 5:36 
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pin
ToBach28-Nov-07 23:49
memberToBach28-Nov-07 23:49 
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pin
shazzababs27-May-08 22:41
membershazzababs27-May-08 22:41 
GeneralThanks a lot Pin
russel_ak25-May-05 19:01
memberrussel_ak25-May-05 19:01 
GeneralDialog in dll doesn't send these notification! Pin
daveice8-Apr-05 1:01
memberdaveice8-Apr-05 1:01 
GeneralI meet this problem too Pin
Anonymous17-May-05 18:06
sussAnonymous17-May-05 18:06 
GeneralRe: I meet this problem too Pin
daveice17-May-05 18:50
memberdaveice17-May-05 18:50 
GeneralRe: I meet this problem too Pin
meijun24-May-05 21:07
membermeijun24-May-05 21:07 
GeneralRe: I meet this problem too Pin
Mycro31-Oct-07 19:31
memberMycro31-Oct-07 19:31 
GeneralA fix for a crash Pin
Yawar Maajed25-Apr-04 20:08
memberYawar Maajed25-Apr-04 20:08 
GeneralRe: A fix for a crash Pin
Michel Wassink25-Apr-04 21:28
memberMichel Wassink25-Apr-04 21:28 
GeneralRe: A fix for a crash Pin
Yawar Maajed26-Apr-04 7:09
memberYawar Maajed26-Apr-04 7:09 
GeneralTooltips on menu items Pin
j3ves15-Feb-04 23:59
memberj3ves15-Feb-04 23:59 
GeneralDoesn't work with modeless dialogs Pin
S Macdonald21-Oct-03 5:37
memberS Macdonald21-Oct-03 5:37 
QuestionHow to make the certain toolbar button disable under certain case? Pin
Jack_Cai25-Mar-03 5:38
memberJack_Cai25-Mar-03 5:38 
AnswerRe: How to make the certain toolbar button disable under certain case? Pin
jan_mfff28-Apr-03 8:45
sussjan_mfff28-Apr-03 8:45 
GeneralRe: How to make the certain toolbar button disable under certain case? Pin
horsewww5-Sep-04 18:24
memberhorsewww5-Sep-04 18:24 
GeneralToolTips from ToolBar on the StatusBar in Dialog Pin
kostax1-Aug-02 22:57
memberkostax1-Aug-02 22:57 
GeneralDockable Toolbars in Dialogs Pin
Rabentochter17-Jul-02 2:07
memberRabentochter17-Jul-02 2:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 11 Jan 2000
Article Copyright 2000 by Randy More
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid