例如我們想在一個對話框中的一個button控件添加tooltip,實現的方法如下:
1. 在該對話框的類中添加一個CToolTipCtrl類型成員,並在適當的地方將其初始化如下:
m_ToolTipCtrl.Create(this);
m_ToolTipCtrl.AddTool(GetDlgItem(IDC_BUTTON1), _T("This is ToolTip"));
m_ToolTipCtrl.SetMaxTipWidth(123);
m_ToolTipCtrl.Activate(TRUE);
2. 給對畫框類添加virtual BOOL PreTranslateMessage(MSG* pMsg)方法並實現如下:
BOOL CtooltipsDlg::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);
}
OK,現在當鼠標划過該button,就會出現This is ToolTip字樣的Tooltip。很簡單,留着以后用。