转载来源: 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段都进入晌应处理
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 callingEnableToolTips
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!
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.
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.
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 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
:
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
.
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 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)
#ifndef _countof
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#endif
and paste them into stdafx.h.
Compile, and see the tool tips work.
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. LikeCDialog
s,CView
s,CStatic
s,CTreeCtrl
s, just to name a few? Happy copying and pasting.