MFC 设置控件的 ToolTip

来源:互联网 发布:淘宝音响精品推荐 编辑:程序博客网 时间:2024/06/07 13:40

建议使用CToolTipCtrl对象

例如:

 

1.在对画框类(CTooltipDlg.cpp)中声明:

CToolTipCtrl m_ToolTipCtrl;

 

2.在对画框类(CTooltipDlg.cpp)初始化方法OnInitDialog()中初始化:

m_ToolTipCtrl.Create(this);
m_ToolTipCtrl.AddTool(GetDlgItem(IDC_BUTTON_CLOSE), _T("This is a test tooltip"));
m_ToolTipCtrl.SetMaxTipWidth(300);
m_ToolTipCtrl.Activate(TRUE);

 

3.在头文件(CTooltipDlg.h)的构造函数中声明方法:

BOOL PreTranslateMessage(MSG* pMsg);

 

4. 在对画框类(CTooltipDlg.cpp)中添加virtual BOOL PreTranslateMessage(MSG* pMsg)方法并实现如下:

BOOL CTooltipDlg::PreTranslateMessage(MSG* pMsg)
{
    ASSERT(pMsg != NULL);
    if (pMsg->message == WM_MOUSEMOVE || pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_LBUTTONUP)
    m_ToolTipCtrl.RelayEvent(pMsg);
    return CDialogEx::PreTranslateMessage(pMsg);
}

 

运行后,鼠标移动到按钮上,即显示出ToolTip了,如下图所示结果。

 

原创粉丝点击