在界面处理经常会使用到 CToolTipCtrl类
一、CtoolTipCtrl封装了一个工具提示的控件,可弹出一个单行的小窗口来显示一些描述信息,大多时候它是隐藏的,除非用户将光标移到要显示提示的工具上面,并且停留大概半秒的时间它才显示,当单击鼠标或者移走光标的时候这个提示窗口就会消失
二、使用方式一:只适用于静态控件、不适用于向树型控件这样的位置不固定的控件(因为树型控件经常被折叠和展开)
实例:(对话框程序为例)
一般用法步骤:
1. 添加CToolTipCtrl成员变量 m_tt。
2. 在父窗口中调用EnableToolTips(TRUE); 此操作不一定必须
3. 在窗口的OnCreate(或者其他适当的位置)中向ToolTip中添加需要显示Tip的子窗口,并同时指定相应的显示字串CToolTipCtrl::AddTool(pWnd,”string to display”)。
4. 重载父窗口的 BOOL PreTranslateMessage(MSG* pMsg) ,在函数中调用 m_tt.RelayEvent(pMsg)。
1、定义对象
public:
CToolTipDlg(CWnd* pParent = NULL); // standard constructor
CToolTipCtrl m_tt;
// Dialog Data
2、初始
BOOL CToolTipDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
EnableToolTips(TRUE);
m_tt.Create(this);
m_tt.Activate(TRUE);
m_tt.AddTool(GetDlgItem(IDC_BUTTON1),"test");
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
3、重载PreTranslateMessage函数
BOOL CToolTipDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
m_tt.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
过程:
1、CWnd::EnableToolTips :指定父窗口
Enables tool tips for the given window.
BOOL EnableToolTips(
BOOL bEnable = TRUE
);
2、CToolTipCtrl::Create
Creates a tool tip control and attaches it to a CToolTipCtrl object.
3、CToolTipCtrl::Activate
Call this function to activate or deactivate a tool tip control.
4、CToolTipCtrl::AddTool
Registers a tool with the tool tip control.
三、使用方式二:动态改变 ToolTip的显示内容
上面所讲的1、2、4步骤。
在增加ToolTip时不指定显示的字串,而是使用LPSTR_TEXTCALLBACK。
在窗口中增加消息映射 ON_NOTIFY_EX( TTN_NEEDTEXT, 0, SetTipText )。
在窗口中增加一个函数用于动态提供显示内容,其原型为
BOOL SetTipText( UINT id, NMHDR * pTTTStruct, LRESULT * pResult ),
下面的代码可以根据传入的参数判定应该显示的内容。
BOOL CWndYour::SetTipText( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
UINT nID =pTTTStruct->idFrom; //得到相应窗口ID,有可能是HWND
if (pTTT->uFlags & TTF_IDISHWND)
{
switch(nID)
{
case(IDC_BUTTON1)
break;
case(IDC_BUTTON2) :
break;
}
}
return TRUE;
}
五、附成员函数:具体参数查看MSDN
CToolTipCtrl类成员
构造 CToolTipCtrl 创建一个CToolTipCtrl对象
Create 创建一个工具提示控件并将它与一个CToolTipCtrl对象连接属性
GetText 获取一个工具提示控件为一个工具维持的文本
GetToolInfo 获取一个工具提示控件维持的关于一个工具的信息
SetToolInfo 设置一个工具提示控件为一个工具维持的文本
GetToolCount 获取由一个工具提示控件支持的工具数
GetDelayTime 获取当前为一个工具提示控件设置的初始,弹出,和再显示持续时间
SetDelayTime 为一个工具提示控件设置初始,弹出,和再显示持续时间
GetMargin 获取当前为一个工具提示窗口设置的上,左,底,和右边距
SetMargin 为一个工具提示窗口设置上,左,底,和右边距
GetMaxTipWidth 获取一个工具提示窗口的最大宽度
SetMaxTipWidth 设置一个工具提示窗口的最大宽度
GetTipBkColor 获取一个工具提示窗口中的背景颜色
SetTipBkColor 设置一个工具提示窗口中的背景颜色
GetTipTextColor 获取一个工具提示窗口中的文本颜色
SetTipTextColor 设置一个工具提示窗口中的文本颜色
操作 Activate 激活工具提示控件或使它成为不活动的
AddTool 向一个工具提示控件注册一个工具
DelTool 从工具提示控件中删除一个工具
HitTest 测试一个点,以确定它是否位于给定工具的边界矩形之内,如果是,返回关于这个工具的信息
RelayEvent 传递一个鼠标消息给工具提示控件处理
SetToolRect 为一个工具设置一个新的边界矩形
UpdateTipText 为一个工具设置工具提示文本
Update 强制当前工具被重画
Pop 从视中删除一个被显示的工具提示窗口
其中:
Create,Activate,AddTool,RelayEvent,DelTool,Update比较常用