Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Windows ToolTip Control without bounding to a tool

I want to use the native windows tooltip control (pure Win32 API, no MFC stuff).

I read the doc, it seems that I have to send a TTM_ADDTOOL message to bond a tool to the tooltip control. Only after that can I send TTM_TRACKACTIVATE & TTM_TRACKPOSITION to show the tooltip.

But I want to display the tooltip anywhere I want it to be. For example, when the mouse hovers over a region of my window. This region is not a tool in the eye of Windows, it's just a region in my window.

Perhaps I can bond the window to the tooltip control, but, doesn't this mean that I have to bond every window I created to the tooltip control?

Is there an easy solution so that I don't have to send TTM_ADDTOOL messages for every window?


I actually have written some code, but the tooltip just doesn't appear. Anders' answer solves some questions actually. And after I poke around my code, I make it work.

In case someone wants to know how it work:

HWND toolTipWnd = ::CreateWindowExW(WS_EX_TOPMOST,
            TOOLTIPS_CLASSW,0,WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
        CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
        0,0,appHandle,0);

TOOLINFOW ti = {};
ti.cbSize = sizeof(TOOLINFOW);
ti.uFlags = TTF_ABSOLUTE | TTF_IDISHWND /* | TTF_TRACK */; // Don't specify TTF_TRACK here. Otherwise the tooltip won't show up.
ti.hwnd   = toolTipWnd; // By doing this, you don't have to create another window.
ti.hinst  = NULL;
ti.uId    = (UINT)toolTipWnd;
ti.lpszText = L"";

::SendMessageW(toolTipWnd, TTM_ADDTOOLW, 0, (LPARAM)&ti);
::SendMessageW(toolTipWnd, TTM_SETMAXTIPWIDTH,0, (LPARAM)350);

This will create a tooltip window which is not bound to any other window. So when you want to show the tooltip (e.g. in responds to WM_MOUSEHOVER message), call this:

TOOLINFOW ti = {};
ti.cbSize   = sizeof(TOOLINFOW);
ti.hwnd     = toolTipWnd;
ti.uId      = (UINT)toolTipWnd;
ti.lpszText = L"Sample Tip Text";
::SendMessageW(toolTipWnd,TTM_UPDATETIPTEXTW,0,(LPARAM)&ti); // This will update the tooltip content.
::SendMessageW(toolTipWnd,TTM_TRACKACTIVATE,(WPARAM)TRUE,(LPARAM)&ti);
::SendMessageW(toolTipWnd, TTM_TRACKPOSITION,0,(LPARAM)MAKELONG(x,y)); // Update the position of your tooltip. Screen coordinate.
//::SendMessageW(toolTipWnd,TTM_POPUP,0,0); // TTM_POPUP not working.. Don't know why.
like image 339
MorrisLiang Avatar asked May 05 '11 10:05

MorrisLiang


1 Answers

You need to call TTM_ADDTOOL at least once, you can't call TTM_SETTOOLINFO or get TTN_GETDISPINFO without it AFAIK.

If your target it XP+ you can get away with using TTM_POPUP to display the tip at any position and at any time (But you need to handle the initial delay yourself unless you want a tracking tooltip)

Generally you call TTM_ADDTOOL and associate it with a rectangle (TOOLINFO.rect) or a child window, or you can set the text to LPSTR_TEXTCALLBACK and handle TTN_GETDISPINFO if everything has a tip. MSDN has some sample code you should take a look at...

like image 56
Anders Avatar answered Oct 04 '22 19:10

Anders