Tips for using user control with CToolTipCtrl? (MFC)

I made my own control derived from CWnd (line chart), and I wonder if I can use CToolTipCtrl to display tooltips for points on the chart. If so, how can I do this?

Btw, when I move the mouse over a point, a line should appear containing a rectangle with information about the values ​​of the point.

+3
source share
1 answer

Yes, it works, in fact I do it the exact same way, also in graphic graphics, however there are a few drawbacks / comments. Message processing is a bit unstable: some messages are not sent according to the documentation, and some workarounds are needed to provide autonomous control (without requiring parental assistance to reflect notifications).

What you do is declare a variable in your derived CWnd class

CToolTipCtrl m_ToolTipCtrl;
CString m_ToolTipContent;

Then do it in OnCreate:

m_ToolTipCtrl.Create(this, TTS_ALWAYSTIP);
m_ToolTipCtrl.Activate(TRUE);

Additionally, 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);

If you want to show your tooltip (presumably in OnMouseMove ()), use

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;
}

, , , .

+5

Source: https://habr.com/ru/post/1742788/


All Articles