转载-CToolBar元素添加Tooltips

转载来源: http://www.ucancode.net/Visual_C_Source_Code/Tooltips-CToolBar-EnableToolTips-CBRS_ALIGN_ANY-CBRS_TOOLTIPS-CBRS_FLYBY-_countof.htm#

其它知识: 

1.   Toolbar创建的时候使用dwCtrlStyle属性使用:TBSTYLE_TOOLTIPS

m_wndToolBar.CreateEx(this, TBSTYLE_TOOLTIPS, WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP|CBRS_TOOLTIPS)

 2.  ON_NOTIFY_EX_RANGE相关ON_NOTIFY_EX来说是范围晌应,可以定义一个ID段都进入晌应处理

  • Download source files - 25.1 Kb

转载-CToolBar元素添加Tooltips_第1张图片

Introduction

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

So I sat down, and placed the lineEnableToolTips() in my dialogsOnInitDialog function. And what shall I tell you, it did not work. Every dialog and every view showed thetoolbar, 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 fromCFrameWnd .

And because a CDialog orCView is not a descendant ofCFrameWnd 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 thetoolbar 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 typeCToolBar, and ... (or look at the source). In the creation process duringOnInit...(), make sure that the bar style flagCBRS_TOOLTIPS is set.

 

Collapse Copy Code

 

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.

 

Collapse Copy Code

 

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 theEND_MESSAGE_MAP() line in your dialogs CPP-file.

According to the documentation, you must create an entry for bothTTN_NEEDTEXTW andTTN_NEEDTEXTA notifications.

In your header file declare a function prototype forOnToolTipNotify:

 

Collapse Copy Code

 

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

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

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

Step 3

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

 

Collapse Copy Code

 

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

Open WINFRM.CPP in your MFC-source directory. Copy thecomplete 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 andAfxLoadString are known. Find the following lines inAFXISAPI.H (source directory for MFC->Include)

 

Collapse Copy Code

 

#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 anyCWnd - window. LikeCDialogs,CViews,CStatics,CTreeCtrls, just to name a few? Happy copying and pasting.

 

 

 

你可能感兴趣的:(tooltips,CToolbar)