SOFTIST 编程方法筆記 →目録


为控件显示工具提示信息(VC++)

当鼠标在对话框上贴的控件上移动时,显示与该控件相关的工具提示。本文是MFC中的CToolTipCtrl类的使用方法的笔记。

1.在对话框类中追加一个CToolTipCtrl类型的成员变量m_toolTip

class CChipTestDlg : public CDialog
{
// 構築
public:
    CChipTestDlg(CWnd* pParent = NULL);
    CToolTipCtrl m_toolTip;
... ...
};

2.在OnInitDialog()函数里,生成m_toolTip的窗口实体,把要显示工具提示的控件登记到m_toolTip

BOOL CChipTestDlg::OnInitDialog()
{
... ...
    CDialog::OnInitDialog();    
    m_toolTip.Create(this,TTS_ALWAYSTIP);
    m_toolTip.AddTool(GetDlgItem(IDC_BUTTON1), "It is button1 ...");
    m_toolTip.AddTool(GetDlgItem(IDC_CHECK1), "It is Check1 ...");
... ...
    return TRUE;
}

3.为了显示工具提示,还要重载填写WM_MOUSEMOVE消息的处理函数。

BOOL CChipTestDlg::PreTranslateMessage(MSG* pMsg) 
{
    if (pMsg->message == WM_MOUSEMOVE)
    {
        if( pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->m_hWnd || 
            pMsg->hwnd == GetDlgItem(IDC_CHECK1)->m_hWnd )
        {
            m_toolTip.RelayEvent(pMsg);
        }
        else
        {
            m_toolTip.Pop();
        }
        return TRUE;
    }
    return CDialog::PreTranslateMessage(pMsg);
}
4.气球形状工具提示。调用m_toolTip的成员函数Create()时,代入TTS_BALLOON(=0x40)参数就可以显示出气球形状工具提示
m_toolTip.Create(this, TTS_ALWAYSTIP | TTS_BALLOON);