Question

In my application I am handling the WM_HELP message and then creating a tooltip for a control using this method:

Taken from: http://msdn.microsoft.com/en-us/library/bb760252(v=vs.85).aspx

HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
    if (!toolID || !hDlg || !pszText)
    {
        return FALSE;
    }
    // Get the window of the tool.
    HWND hwndTool = GetDlgItem(hDlg, toolID);

    // Create the tooltip. g_hInst is the global instance handle.
    HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                              WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              hDlg, NULL, 
                              g_hInst, NULL);

   if (!hwndTool || !hwndTip)
   {
       return (HWND)NULL;
   }                              

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hDlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = pszText;
    SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);

    return hwndTip;
}

The tooltip vanishes as soon as I move my mouse pointer.

My questions are:

  1. Is tooltip is destroyed or is it just hidden ?
  2. If it is hidden then how to destroy it and when?

Thanks.

Was it helpful?

Solution

It's been a while since I've done any WinAPI programming but if my memory serves me...

The call to CreateWindowEx passes the hDlg as the hWndParent parameter meaning the dialog window is now the parent of the tooltip.

From the MSDN documentation on the DestroyWindow function it says:

If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.

So you can assume your tooltip window will be destroyed eventually. Be careful if you are calling CreateToolTip in response to every WM_HELP message as you will end up with a number of tooltip windows hanging around in memory until your dialog is closed and DestroyWindow is finally called.

As vz0 pointed out you could create the tooltip once, hang on to the window handle, then show the tooltip in response to the help message rather than creating it again.

In your comment to vz0's answer you said:

there are multiple ways in which a tooltip goes awya. example: mouse move, timeout etc.

All of those only result in the window being hidden so the handle to the tooltip is still valid and can be redisplayed using ShowWindow.

OTHER TIPS

For every CreateWindowEx call you need a matching DestroyWindow call.

As an alternative, instead of creating and destroying the window every time you can use the ShowWindow call with SW_SHOW and SW_HIDE to show and hide the popup.

In my experience, I had to DestroyWindow() on the tooltip so an HFONT (font GDI resource) was properly released. There was a parent-child bond of the two windows at one time - but my system changes this at run-time and could be to blame. Probably no harm in doing it if your system generalizes it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top