Chinaunix首页 | 论坛 | 博客
  • 博客访问: 379866
  • 博文数量: 35
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 797
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-02 08:51
文章分类

全部博文(35)

文章存档

2011年(1)

2010年(3)

2009年(3)

2008年(28)

我的朋友

分类: WINDOWS

2008-08-05 15:55:56

前提:
自定义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,则实现如下:
xxxDlg.h
#include "MyButton.h"
 
protected:
     MyButton      m_btnTest;
 
xxxDlg.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一下就出来了。
 
阅读(3286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~