Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to destroy a tooltip?

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.

like image 294
Favonius Avatar asked Jan 31 '11 12:01

Favonius


People also ask

How do you destroy a tooltip?

.tooltip('dispose') Hides and destroys an element's tooltip. Tooltips that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.

What is tooltip in Javascript?

JS Tooltip (tooltip. js) The Tooltip plugin is small pop-up box that appears when the user moves the mouse pointer over an element. For a tutorial about Tooltips, read our Bootstrap Tooltip Tutorial.

How to add tooltip Bootstrap?

To create a tooltip, add the data-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with jQuery: select the specified element and call the tooltip() method.


2 Answers

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.

like image 140
Tony Avatar answered Sep 28 '22 22:09

Tony


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.

like image 37
vz0 Avatar answered Sep 28 '22 22:09

vz0