MFC中为控件添加浮动提示框的方法

当我们在写MFC程序时,有时会希望当鼠标悬浮在某个控件之上时,有相关消息提示,下面总结该功能的实现方法;

1.在 XXXDlg.h 中:

添加虚函数:

virtual BOOL PreTranslateMessage(MSG* pMsg);

添加变量声明:

CToolTipCtrl m_tooltip;

2.在 XXXDlg.cpp 中

在初始化函数BOOL XXXDlg::OnInitDialog()中添加:

m_tooltip.Create(this);
m_tooltip.AddTool(GetDlgItem(IDC_BUTTON1), _T("消息提示"));
m_tooltip.AddTool(GetDlgItem(IDC_BUTTON2), _T("tips test"));

m_tooltip.SetMaxTipWidth(123);
m_tooltip.Activate(TRUE);

添加虚函数的实现:

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

猜你喜欢

转载自blog.csdn.net/mikasoi/article/details/83513641