您的位置:首页 > 其它

视图中根据不同位置,响应提示窗口(Tooltip)

2009-11-08 18:53 357 查看
在MFC工程中,添加如下代码即可。

/   MyToolTipCtrl.h   :   header   file
//

/////////////////////////////////////////////////////////////////////////////
//   CMyToolTipCtrl   window

class   CMyToolTipCtrl   :   public   CToolTipCtrl
{
//   Construction
public:
CMyToolTipCtrl();

//   Attributes
public:

//   Operations
public:

//   Overrides
//   ClassWizard   generated   virtual   function   overrides
//{{AFX_VIRTUAL(CMyToolTipCtrl)
//}}AFX_VIRTUAL

//   Implementation
public:
BOOL   AddRectTool   (CWnd*   pWnd,   LPCTSTR   pszText,   LPCRECT   pRect,   UINT   nIDTool);
BOOL   AddWindowTool   (CWnd*   pWnd,   LPCTSTR   pszText);
virtual   ~CMyToolTipCtrl();

//   Generated   message   map   functions
protected:
//{{AFX_MSG(CMyToolTipCtrl)
//   NOTE   -   the   ClassWizard   will   add   and   remove   member   functions   here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

//   MyToolTipCtrl.cpp   :   implementation   file
//

#include   "stdafx.h"
#include   "GridDemo.h"
#include   "MyToolTipCtrl.h"

CMyToolTipCtrl::CMyToolTipCtrl()
{
}

CMyToolTipCtrl::~CMyToolTipCtrl()
{
}

BEGIN_MESSAGE_MAP(CMyToolTipCtrl,   CToolTipCtrl)
//{{AFX_MSG_MAP(CMyToolTipCtrl)
//   NOTE   -   the   ClassWizard   will   add   and   remove   mapping   macros   here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
//   CMyToolTipCtrl   message   handlers

BOOL   CMyToolTipCtrl::AddWindowTool(CWnd   *pWnd,   LPCTSTR   pszText)
{
TOOLINFO   ti;
ti.cbSize   =   sizeof   (TOOLINFO);
ti.uFlags   =   TTF_IDISHWND   |   TTF_SUBCLASS;
ti.hwnd   =   pWnd->GetParent   ()->GetSafeHwnd   ();
ti.uId   =   (UINT)   pWnd->GetSafeHwnd   ();
ti.hinst   =   AfxGetInstanceHandle   ();
ti.lpszText   =   (LPTSTR)   pszText;

return   (BOOL)   SendMessage   (TTM_ADDTOOL,   0,   (LPARAM)   &ti);
}

BOOL   CMyToolTipCtrl::AddRectTool(CWnd   *pWnd,   LPCTSTR   pszText,   LPCRECT   pRect,   UINT   nIDTool)
{
TOOLINFO   ti;
ti.cbSize   =   sizeof   (TOOLINFO);
ti.uFlags   =   TTF_SUBCLASS;
ti.hwnd   =   pWnd->GetSafeHwnd   ();
ti.uId   =   nIDTool;
ti.hinst   =   AfxGetInstanceHandle   ();
ti.lpszText   =   (LPTSTR)   pszText;
::CopyRect   (&ti.rect,   pRect);

return   (BOOL)   SendMessage   (TTM_ADDTOOL,   0,   (LPARAM)   &ti);
}


然后在你的视图(CYourView)中,添加如下代码

#include   "MyToolTipCtrl.h" //   Added   by   ClassView
public:
CMyToolTipCtrl   m_ctlTT;


cpp的OnDraw()函数中添加如下代码:

m_ctlTT.Create   (this);
m_ctlTT.AddRectTool   (this,_T("This   is   a   rectangle1"),CRect(32,32,64,64),IDT_RECTANGLE1);

m_ctlTT.AddRectTool   (this,_T("This   is   a   rectangle2"),CRect(100,100,164,164),IDT_RECTANGLE2);


其中,宏IDT_RECTANGLE1和宏IDT_RECTANGLE2需要你自己定义(不与系统冲突即可)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐