使用CToolTipCtrl进行定制控制的工具提示? (MFC) [英] Tool tips for custom made control using CToolTipCtrl ? (MFC)
问题描述
我做了一个从CWnd(线图)派生的自定义控件,我想知道是否可以使用CToolTipCtrl来显示图上的点的工具提示。如果是的话,我该怎么办?
Btw,当我将鼠标移到点上时,应该弹出包含点值信息的矩形字符串。
是的,这是可行的,实际上我做同样的事情,几个缺点/备注。消息处理有点won,,有些消息不是根据文档发送,并且一些解决方法是必要的,以保持控制自包含(不需要父母的帮助反映通知)。
你在CWnd派生类中声明一个变量
CToolTipCtrl m_ToolTipCtrl;
CString m_ToolTipContent;
然后在OnCreate上执行此操作:
m_ToolTipCtrl.Create(this,TTS_ALWAYSTIP);
m_ToolTipCtrl.Activate(TRUE);您也可以设置延迟时间:
$ b < b 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中):
//不使用CToolTipCtrl :: AddTool )因为
//它将消息重定向到父
TOOLINFO ti = {0};
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_IDISHWND; //表示uId是控件的句柄
ti.uId =(UINT_PTR)m_hWnd; //处理控件
ti.hwnd = m_hWnd; //处理窗口
//接收工具提示消息
ti.hinst = :: AfxGetInstanceHandle();
ti.lpszText = LPSTR_TEXTCALLBACK;
ti.rect =< rectangle其中,当鼠标在它上面时,工具提示应显示> ;;
m_ToolTipCtrl.SendMessage(TTM_ADDTOOL,0,(LPARAM)(LPTOOLINFO)& ti);
m_ToolTipCtrl.Activate(TRUE);
m_ToolTipContent =我的工具提示内容;此外,还需要处理TTNNeedText:
//构建无关的因为某些原因不工作。
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;
}
您必须修改这一点,函数和消息,让它在你的项目中工作,但是可以做到。
I made a custom control derived from CWnd (a line chart) and I'm wondering if I can use CToolTipCtrl for displaying tool tips for points on the graph. If yes, how could I do that?
Btw, when I move my mouse over the point, the rectangle containg string with information about values of the point, should pop up.
Yes, this works, actually I do this exact same thing, also in a line graph chart, however there are a few drawbacks/remarks. The message handling is a bit wonky, with some messages not being send according to the documentation, and some workarounds being necessary to keep the control self-contained (not requiring help from the parent to reflect notifications).
What you do is declare a variable in your CWnd-derived class
CToolTipCtrl m_ToolTipCtrl;
CString m_ToolTipContent;
Then do this on OnCreate:
m_ToolTipCtrl.Create(this, TTS_ALWAYSTIP);
m_ToolTipCtrl.Activate(TRUE);
Optionally, you can also set the delay time:
m_ToolTipCtrl.SetDelayTime(TTDT_AUTOPOP, -1);
m_ToolTipCtrl.SetDelayTime(TTDT_INITIAL, 0);
m_ToolTipCtrl.SetDelayTime(TTDT_RESHOW, 0);
When you want to show your tooltip (presumably in OnMouseMove()), use
m_ToolTipCtrl.Pop();
BUT this only works in UNICODE builds. So if you're still on MBCS (like I am), you can only show the tooltip after a certain delay. Use this to set your tooltip text (also in 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";
Furthermore, you need to handle 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;
}
You'll have to modify this a bit, and read the documentation of the functions and messages, to get this to work in your project but yes it can be done.
这篇关于使用CToolTipCtrl进行定制控制的工具提示? (MFC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!