Click here to Skip to main content
15,886,091 members
Articles / Desktop Programming / MFC
Article

Tooltips for Toolbars in Non-CFrameWnd Windows

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
20 May 20033 min read 118.7K   2.3K   42   15
Explains how to add tool tips for toolbars attached to CDialog or CView descentants.

Sample Image - ToolTipsInDialog.gif

Introduction

When I posted an article discussing the subject of placing a toolbar right in the middle of elsewhere, Daniel Kaminsky pointed out, that the tool tips were not displayed.

So I sat down, and placed the line EnableToolTips() in my dialogs OnInitDialog function. And what shall I tell you, it did not work. Every dialog and every view showed the toolbar, but no tool tips, or occasionally only very odd ones popped up.

Having a look at the help, there it was stated:

Simply calling EnableToolTips is not enough to display tool tips for your child controls unless the parent window is derived from CFrameWnd.

And because a CDialog or CView is not a descendant of CFrameWnd the tool tips will not get displayed. But a solution was also at the bottom of this article, which I implemented without further thinking. It worked well except to the fact that it showed something like: "Your mouse is hovering over the control with the ID 1234". That was something I never used as tool tip text. Because I wanted the text, stored in the resource of the toolbar being displayed, I looked up the source codes of the MFC itself.

The solution provided here is solely from the people of Microsoft. My only creative part, is finding the places to copy!

Step1

Create a toolbar and attach it to your dialog. Use the normal method of creating a variable of type CToolBar, and ... (or look at the source). In the creation process during OnInit...(), make sure that the bar style flag CBRS_TOOLTIPS is set.

m_wndToolBar.SetBarStyle(CBRS_ALIGN_ANY | CBRS_TOOLTIPS | CBRS_FLYBY);

Then call EnableToolTips(true);. If you compile now, and run the application, you can see the tool tips not to work.

Step2

Now comes the typing part. Create a message map entry in your CPP-file.

ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)

Or copy these two lines just before the END_MESSAGE_MAP() line in your dialogs CPP-file.

According to the documentation, you must create an entry for both TTN_NEEDTEXTW and TTN_NEEDTEXTA notifications.

In your header file declare a function prototype for OnToolTipNotify:

afx_msg BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR,LRESULT *pResult);

Or copy this line just before the DECLARE_MESSAGE_MAP() line in your dialogs h-file.

If you compile now, the compiler will tell you, that it needs a function named OnToolTipNotify.

Step 3

Now comes the copy part. Write a new line in your dialogs implementation file (*.cpp):

BOOL CMyDlgOrView:OnToolTipNotify(UINT, NMHDR* pNMHDR, LRESULT* pResult)

Open WINFRM.CPP in your MFC-source directory. Copy the complete function body of BOOL CFrameWnd::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult) and paste it just beneath the new written line in your dialogs/view implementation file. Do not compile!

I told you not to! Add #include <afxpriv.h> either at the beginning of the dialogs header file or elsewhere in stdafx.h. Thus the functions AfxExtractSubString and AfxLoadString are known. Find the following lines in AFXISAPI.H (source directory for MFC->Include)

#ifndef _countof
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#endif

and paste them into stdafx.h.

Compile, and see the tool tips work.

Extroduction

If you have no access to the sources of the MFC, open the sources of the demo project. I did already copy and paste. If I understand right, what is said in the documentation, this will work for any CWnd - window. Like CDialogs, CViews, CStatics, CTreeCtrls, just to name a few? Happy copying and pasting.

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
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwhy i can not download the demo? Pin
loverc19-May-06 22:49
loverc19-May-06 22:49 
GeneralNot work in ChildFramexxx.cpp Pin
zilnus27-Jul-05 18:57
zilnus27-Jul-05 18:57 
QuestionHelp!How to create a non-rectangular Toolbar? Pin
tododay21-Dec-03 22:17
tododay21-Dec-03 22:17 
Questionwhat abt in ActiveX controls? Pin
Anonymous17-Jun-03 22:23
Anonymous17-Jun-03 22:23 
AnswerRe: what abt in ActiveX controls? Pin
Tzoockee11-Jul-03 12:55
Tzoockee11-Jul-03 12:55 
GeneralStatusbar text Pin
Konrad Windszus7-Jun-03 3:05
Konrad Windszus7-Jun-03 3:05 
GeneralMaybe you can help me Pin
Josema21-May-03 6:33
Josema21-May-03 6:33 
GeneralRe: Maybe you can help me Pin
G. Steudtel23-May-03 2:46
G. Steudtel23-May-03 2:46 
GeneralRe: Maybe you can help me Pin
Josema25-May-03 8:49
Josema25-May-03 8:49 
GeneralRe: Maybe you can help me Pin
G. Steudtel30-May-03 6:26
G. Steudtel30-May-03 6:26 
GeneralAnd why not just use CFrameWnd???... Pin
igor19605-May-03 19:44
igor19605-May-03 19:44 
GeneralRe: And why not just use CFrameWnd???... Pin
G. Steudtel6-May-03 2:58
G. Steudtel6-May-03 2:58 
GeneralRe: And why not just use CFrameWnd???... Pin
igor19606-May-03 7:16
igor19606-May-03 7:16 
GeneralRe: And why not just use CFrameWnd???... Pin
Nish Nishant6-May-03 18:25
sitebuilderNish Nishant6-May-03 18:25 
GeneralRe: And why not just use CFrameWnd???... Pin
armentage27-May-03 4:12
armentage27-May-03 4:12 
Sometimes the customer is incompetent. Tell them that, they come to you as the expert after all, don't they?

CWnd::BeginModalState() -- Disables all input to the current window and its ancestors.

CWnd::BeginModalState() -- Puts things right again.

In your CFrameWnd descendent, try doing a GetParent()->BeginModalState() in OnCreateClient(), and put an EndModalState() in the OnDestroy().


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.