CPPToolTip的应用

CPPToolTip的应用,CPPToolTip作者原文参见http://www.codeproject.com/miscctrl/pptooltip.asp#AddTool
1.CPPToolTip是一个MFC的类,用以实现ToolTip方式显示功能,作者:Eugene Pustovoyt 
CPPToolTip可以实现在View,Dlg,Menu,ToolBar,TrayIcon处实现ToolTip方式的显示效果。
2.使用方法(只实现了最基本的功能)
预期目标:在View的client区鼠标停止时显示当前点的X,Y坐标
a.在工程中添加相关的.h,.cpp文件,一共有8个文件,不过不一定全部都添加(没做过测试)。
b.我需要在CtestView中实现该功能,所以我要在testView.h文件中定义该变量
CPPToolTip m_tooltip;
同时添加pptooltip.h头文件,放在#endif // _MSC_VER > 1000前面就行了
c.添加消息
afx_msg void NotifyDisplayTooltip(NMHDR * pNMHDR, LRESULT * result);
在DECLARE_MESSAGE_MAP()之前就可以了
同时要在TestView.cpp中完成响应函数
ON_NOTIFY (UDM_TOOLTIP_DISPLAY, NULL, NotifyDisplayTooltip)
d.此外还需要添加一个PreTranslateMessage(MSG* pMsg)功能,来完成预先将鼠标动作发送到tooltip上,可以使用classwizard添加这个消息和函数
然后再PreTranslateMessage(MSG* pMsg)中添加一句:
m_tooltip.RelayEvent(pMsg);就可以了
e.接下来需要在void CTestView::OnInitialUpdate() 完成如下初始化(没有OnInitialUpdate()的话,用ClassWizard建立就行了)
CView::OnInitialUpdate();

// TODO: Add your specialized code here and/or call the base class
m_tooltip.Create(this); //创建这个实例了
m_tooltip.SetNotify(TRUE); //设定消息发送,也就是说如果这个设置为FALSE的话,m_tooltip这个东西是不会向他的主窗口或者目标窗口发送NOTIFY的消息的

CRect rc; //取得窗口Client区的大小
GetClientRect(&rc);
//Add tooltip to the client area
m_tooltip.AddTool(this, _T("hii..."),rc); //这里的三个参数为this是包含m_tooltip的窗体,第二个参数为tooltip的默认提示文字,第三个参数是使能tooltip的区域
m_tooltip.SetBehaviour(PPTOOLTIP_MULTIPLE_SHOW); //设定m_tooltip的behavior,这里设定的是使得在该区域内只要鼠标停止,就会响应tooltip响应,否则的话默认的方式是在该区域内只出现一次,必须移出该区域以后才能实现第二次tooltip
f.接下来要实现显示当前位置的X,Y坐标,需要在NotifyDisplayTooltip()中实现该功能
void CTestView::NotifyDisplayTooltip(NMHDR * pNMHDR, LRESULT * result)
{
*result = 0;
NM_PPTOOLTIP_DISPLAY * pNotify = (NM_PPTOOLTIP_DISPLAY*)pNMHDR;
CString str;
str.Format("X:%d,Y:%d\n\n",pNotify->pt->x,pNotify->pt->y);
pNotify->ti->sTooltip.Format("X:%d,Y:%d\n",pNotify->pt->x,pNotify->pt->y);
}
g.另外需要说的一点是,目前实现的功能中还有一个弱点,就是当窗体改变大小以后会使得并不是当前的全部clientArea都是tooltip有效地区域,所以要在onsize()函数中添加以下修改
void CTestView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
CRect rc;
GetClientRect(&rc);

m_tooltip.RemoveAllTools(); //删除当前所有的tooltip
m_tooltip.AddTool(this, _T("hii..."),rc); //重新添加
m_tooltip.SetBehaviour(PPTOOLTIP_MULTIPLE_SHOW); //重新修改当前有效区域
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CPPToolTip控件 链接:http://www.codeproject.com/KB/miscctrl/pptooltip.aspx 截图: 1 CPPToolTip控件介绍 Files Description PPTooltip.h PPTooltip.cpp CPPTooltip class PPHtmlDrawer.h PPHtmlDrawer.cpp CPPHtmlDrawer class. It's need to drawing HTML string in tooltip body PPDrawManager.h PPDrawManager.cpp CPPDrawManager class is a set of methods to work with graphics. CeXDib.h CeXDib.cpp CCeXDib class thanks to Davide Pizzolato and Davide Calabro. This class use for extend background's effect. Extend background effects by Davide Pizzolato and Davide Calabro become available if defined USE_SHADE: in PPDrawManager.h #define USE_SHADE 2 在普通窗体控件中使用 2.1 创建CPPToolTip对象 CPPToolTip m_tooltip; 2.2 在窗口初始化函数OnInitDialog中: // Create the CPPToolTip object m_tooltip.Create(this); 2.3 添加提示控件 m_tooltip.AddTool(GetDlgItem(IDC_BUTTON1), _T("Tooltip to the control IDC_BUTTON1")); 或者: m_tooltip.AddTool(this, _T("Tooltip for rectangle area"),CRect (100, 100, 200, 200)); 2.4 拦截处理鼠标消息 BOOL ... ::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); } 3 在工具栏中使用 3.1 在CMainFrame中定义CPPToolTip对象 CPPToolTip m_tooltip; 3.2 在CMainFrame的OnCreate函数中创建CPPToolTip对象,添加工具栏提示 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ... m_tooltip.Create(this); //Adds tooltip for toolbar m_tooltip.AddToolBar(&m_wndToolBar); return 0; } 3.3 截取和处理鼠标消息 BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); } 4 在菜单中使用 4.1 在CMainFrame中定义CPPToolTip变量 CPPToolTip m_tooltip; 4.2 在CMainFrame的OnCreate()函数中创建CPPToolTip对象 m_tooltip.Create(this); 4.3 Uncomments a line to enable a work with menus. in PPTooltip.h #define PPTOOLTIP_USE_MENU 4.4 为CMainFrame添加两个事件处理对象 //选中菜单事件 void CMainFrame::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSubMenu) { m_tooltip.OnMenuSelect(nItemID, nFlags, hSubMenu); CFrameWnd::OnMenuSelect(nItemID, nFlags, hSubMenu); } //闲置状态事件 void CMainFrame::OnEnterIdle(UINT nWhy, CWnd* pWho) { m_tooltip.OnEnterIdle(nWhy, pWho); } 4.5 截取和处理鼠标消息 BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值