Click here to Skip to main content
Licence 
First Posted 10 Jan 2000
Views 173,450
Bookmarked 54 times

How to display tooltips for a toolbar in a dialog

By Randy More | 10 Jan 2000
Simple technique for getting tooltips when you have a toolbar in a dialog

1
1 vote, 4.5%
2

3
6 votes, 27.3%
4
15 votes, 68.2%
5
4.59/5 - 40 votes
1 removed
μ 4.54, σa 1.28 [?]

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

About the Author

Randy More



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAfxLoadString PinmemberChris F.7:01 15 Sep '11  
QuestionExcelent!! Pinmemberdonghyuk80kim16:14 26 Jul '11  
GeneralIt's better: PinmemberMember #1631196:24 8 Jan '07  
Questiondll-modeless dialogbox-toolbar-tooltips Pinmemberjgcollins13:26 26 Aug '05  
AnswerRe: dll-modeless dialogbox-toolbar-tooltips PinmemberMiles Davies4:28 6 Oct '05  
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pinmembermartypku5:36 6 Jun '07  
AnswerRe: dll-modeless dialogbox-toolbar-tooltips PinmemberToBach23:49 28 Nov '07  
AnswerRe: dll-modeless dialogbox-toolbar-tooltips Pinmembershazzababs22:41 27 May '08  
GeneralThanks a lot Pinmemberrussel_ak19:01 25 May '05  
GeneralDialog in dll doesn't send these notification! Pinmemberdaveice1:01 8 Apr '05  
GeneralI meet this problem too PinsussAnonymous18:06 17 May '05  
GeneralRe: I meet this problem too Pinmemberdaveice18:50 17 May '05  
GeneralRe: I meet this problem too Pinmembermeijun21:07 24 May '05  
GeneralRe: I meet this problem too PinmemberMycro19:31 31 Oct '07  
GeneralA fix for a crash PinmemberYawar Maajed20:08 25 Apr '04  
GeneralRe: A fix for a crash PinmemberMichel Wassink21:28 25 Apr '04  
GeneralRe: A fix for a crash PinmemberYawar Maajed7:09 26 Apr '04  
GeneralTooltips on menu items Pinmemberj3ves23:59 15 Feb '04  
GeneralDoesn't work with modeless dialogs PinmemberS Macdonald5:37 21 Oct '03  
QuestionHow to make the certain toolbar button disable under certain case? PinmemberJack_Cai5:38 25 Mar '03  
AnswerRe: How to make the certain toolbar button disable under certain case? Pinsussjan_mfff8:45 28 Apr '03  
GeneralRe: How to make the certain toolbar button disable under certain case? Pinmemberhorsewww18:24 5 Sep '04  
GeneralToolTips from ToolBar on the StatusBar in Dialog Pinmemberkostax22:57 1 Aug '02  
GeneralDockable Toolbars in Dialogs PinmemberRabentochter2:07 17 Jul '02  
GeneralClass Wizard barfs PinmemberTodd.Harvey5:33 31 May '02  

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.

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