is there another way to use CToolTipCtrl
时间: 2023-07-09 17:17:14 浏览: 127
Yes, there is another way to use CToolTipCtrl in an MFC application. Instead of adding the tooltip for a control in the OnInitDialog function, you can add the tooltip for a control in response to a user action, such as clicking a button. Here are the steps to use CToolTipCtrl in this way:
1. Create a CToolTipCtrl object:
```CToolTipCtrl m_ToolTip;```
2. Initialize the CToolTipCtrl object in the OnInitDialog function of your dialog:
```
BOOL CMyDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Create the tooltip control
m_ToolTip.Create(this);
return TRUE;
}
```
3. Add a message map entry for the control that you want to associate with the tooltip:
```
BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
ON_CONTROL_RANGE(CBN_SELCHANGE, IDC_MYCONTROL1, IDC_MYCONTROL2, &CMyDialog::OnMyControlSelChange)
END_MESSAGE_MAP()
```
This message map entry handles the CBN_SELCHANGE message for a range of controls, from IDC_MYCONTROL1 to IDC_MYCONTROL2.
4. Add a handler for the message map entry that adds the tooltip for the control:
```
void CMyDialog::OnMyControlSelChange(UINT nID)
{
CString strTooltip;
// Determine the tooltip text based on the control ID
switch (nID)
{
case IDC_MYCONTROL1:
strTooltip = _T("My Tooltip 1");
break;
case IDC_MYCONTROL2:
strTooltip = _T("My Tooltip 2");
break;
}
// Add the tooltip for the control
m_ToolTip.AddTool(GetDlgItem(nID), strTooltip);
}
```
This handler determines the tooltip text based on the control ID and adds the tooltip for the control using the AddTool method.
5. Handle the WM_MOUSEMOVE message in your dialog to display the tooltip:
```
BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
// Pass mouse messages to the tooltip control
m_ToolTip.RelayEvent(pMsg);
return CDialogEx::PreTranslateMessage(pMsg);
}
```
This method handles the WM_MOUSEMOVE message and passes it to the tooltip control using the RelayEvent method.
That's it! Now when the user selects a control, the tooltip will be displayed. You can also remove the tooltip for a control using the DeleteTool method.
阅读全文