Is it necessary to destroy a tooltip?
Asked Answered
C

3

8

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.

Chockfull answered 31/1, 2011 at 12:22 Comment(0)
P
6

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.

Photochronograph answered 1/2, 2011 at 12:15 Comment(7)
Thanks :). Actually I figured that out. This msdn forum link was helpful (social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/…). After going through it, I have written a wrapper class for the tooltip and reusing the tooltip window. Also as soon as the instance goes out of scope, I am destroying the tooltip in the destructor. Marking your answer as accepted. Thanks again :).Chockfull
Actually, you need to destroy it.It is not a child window or owned, as one might assume. I just experienced that the hard way (hundreds of tooltip windows although the "parent" is destroyed), might be depending on the way it is created. To be on the safe side (and for symmetry): do it.Bonina
BTW: SPY++ helps a lot about finding windows and their state (hidden/shown, size, ...)Bonina
@Bonina - When you created your tooltip windows didi you pass a HWND hWndParent (parameter 9 of the CreateWindowEx function? If not, passing NULL, means you would have to destroy the tooltip window yourself.Photochronograph
Although I did of course pass a parent, the tooltip was not destroyed when the parent went away... Or at least, it could have happened I attached several tooltips to one window, and not all were destroyed. I did not check further, I was glad I had got rid of the hundreds of tooltip windows that had been created after a while when hovering over my windows... ;)Bonina
@Bonina kindly ensure that you are setting the tooltips as owned to that window as the owner. It is very easy to make a mistake in win32. I tested 100 tooltips by creating them in a for loop and setting them as owned to the same window. Then I closed the window and got back 100 report of tooltips that ran their WM_DESTROY routine.Thurlow
Thank you. Well the question was asked 11 years ago ;)Bonina
D
2

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.

Dana answered 31/1, 2011 at 13:37 Comment(1)
Could you please explain 'how does this fit into the tooltip scenario?'. Because there are multiple ways in which a tooltip goes awya. example: mouse move, timeout etc. How to handle for all the cases.Chockfull
S
0

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.

Synchronic answered 11/1, 2018 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.