环境:VS2003
前提:
自定义button类
class
MyButton : public CBitmapButton
假设自定义button类名为:mybutton
可以在类中加入成员变量
CToolTipCtrl m_ToolTip; 然后重载 PreSubClass(), PreTranslateMessage() 最后加入成员函数 SetToolTipText()
具体实现如下:
mybutton.h
class
MyButton : public CBitmapButton
{
......
protected:
static CToolTipCtrl m_ToolTip;
......
public:
//set tooltip
virtual void PreSubclassWindow();
virtual BOOL PreTranslateMessage(MSG* pMsg);
void SetToolTipText(LPCTSTR lpszToolTipText); .....
};
mybutton.cpp
//CToolTipCtrl mybutton::m_ToolTip;
void mybutton::PreSubclassWindow()
{
if(!m_ToolTip.GetSafeHwnd())
m_ToolTip.Create(this);
m_ToolTip.AddTool(this);
CButton::PreSubclassWindow();
}
BOOL mybutton::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
void mybutton::SetToolTipText(LPCTSTR lpszToolTipText)
{
m_ToolTip.UpdateTipText(lpszToolTipText, this);
}
例如在一个dlg中设置button的tooltip,则实现如下:
xxx
Dlg.h
中
#include
"MyButton.h"
protected
:
MyButton m_btnTest;
xxx
Dlg.cpp
中
BOOL
xxxDlg::OnInitDialog()
{
CDialog::OnInitDialog();
。。。。。
。。。。。
m_btnTest
.SetToolTipText(L"Test");
。。。。
。。。。。
return
TRUE;
}
使用上面的方法,在普通dialog上是好使的,但是如果在ActiveX控件内的Button上使用,还要多加一些代码。
1
.上面的方法中不用重载PreTranslateMessage(),因为在
ActiveX控件中根本就不会执行这个函数。
2.在自定义的
Button类中添加
OnMouseMove
事件处理函数。然后在OnMouseMove函数中生成一个MSG对象,然后调用m_ToolTip
.RelayEvent(&pMsg);
代码如下:
MyButton.h
中
//
添加函数声明
afx_msg
void OnMouseMove(UINT nFlags, CPoint point);
MyButton.cpp
中
BEGIN_MESSAGE_MAP
(MyButton, CBitmapButton)
//
添加OnMouseMove事件触发
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP
()
//
添加OnMouseMove函数定义
void
MyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_ToolTip.m_hWnd != NULL)
{
//
构造一个MSG
MSG msg;
msg.hwnd = m_hWnd;
msg.message = WM_MOUSEMOVE;
msg.wParam = LOWORD(point.x);
msg.lParam = LOWORD(point.y);
msg.time = 0;
msg.pt.x = LOWORD(point.y);
msg.pt.y = HIWORD(point.y);
m_ToolTip.RelayEvent(&msg);
}
CBitmapButton::OnMouseMove(nFlags, point);
}
OK,大功告成,写一个程序,调用这个
ActiveX控件,
ToolTip
一下就出来了。