是的,这有效,实际上我也做了同样的事情,也在折线图中,但是有一些缺点/评论。消息处理有点不稳定,一些消息没有根据文档发送,并且需要一些解决方法来保持控件的独立性(不需要父级的帮助来反映通知)。
您要做的就是在 CWnd 派生类中声明一个变量
CToolTipCtrl m_ToolTipCtrl;
CString m_ToolTipContent;
然后在 OnCreate 上执行此操作:
m_ToolTipCtrl.Create(this, TTS_ALWAYSTIP);
m_ToolTipCtrl.Activate(TRUE);
您还可以选择设置延迟时间:
m_ToolTipCtrl.SetDelayTime(TTDT_AUTOPOP, -1);
m_ToolTipCtrl.SetDelayTime(TTDT_INITIAL, 0);
m_ToolTipCtrl.SetDelayTime(TTDT_RESHOW, 0);
当您想显示工具提示时(大概在 OnMouseMove() 中),请使用
m_ToolTipCtrl.Pop();
但这仅适用于 UNICODE 版本。因此,如果您仍在使用 MBCS(像我一样),则只能在一定延迟后显示工具提示。
使用它来设置工具提示文本(也在 OnMouseMove 中):
// Not using CToolTipCtrl::AddTool() because
// it redirects the messages to the parent
TOOLINFO ti = {0};
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_IDISHWND; // Indicate that uId is handle to a control
ti.uId = (UINT_PTR)m_hWnd; // Handle to the control
ti.hwnd = m_hWnd; // Handle to window
// to receive the tooltip-messages
ti.hinst = ::AfxGetInstanceHandle();
ti.lpszText = LPSTR_TEXTCALLBACK;
ti.rect = <rectangle where, when the mouse is over it, the tooltip should be shown>;
m_ToolTipCtrl.SendMessage(TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
m_ToolTipCtrl.Activate(TRUE);
m_ToolTipContent = "my tooltip content";
此外,您需要处理 TTNNeedText:
// The build-agnostic one doesn't work for some reason.
ON_NOTIFY_EX(TTN_NEEDTEXTA, 0, OnTTNNeedText)
ON_NOTIFY_EX(TTN_NEEDTEXTW, 0, OnTTNNeedText)
BOOL GraphCtrlOnTTNNeedText(UINT id, NMHDR* pTTTStruct, LRESULT* pResult)
{
TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pTTTStruct;
//pTTT->lpszText = "some test text";
//pTTT->lpszText = m_ToolTipContent;
strncpy_s(pTTT->lpszText, 80, m_ToolTipContent, _TRUNCATE);
return TRUE;
}
您必须对此进行一些修改,并阅读函数和消息的文档,才能使其在您的项目中工作,但是是的,这是可以完成的。