winapicommon-controls

What's the sense passing two HWND handles when doing TTM_ADDTOOL to a tooltip object?


In this MSDN code example:

// Description:
//   Creates a tooltip for an item in a dialog box. 
// Parameters:
//   idTool - identifier of an dialog box item.
//   nDlg - window handle of the dialog box.
//   pszText - string to use as the tooltip text.
// Returns:
//   The handle to the tooltip.
//
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;                          // first HWND
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;             // second HWND
    toolInfo.lpszText = pszText;
    SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);

    return hwndTip;
}

we associate TTM_ADDTOOL operation with two HWND handles, one is the Dialogbox(hDlg), another is a control inside the dialogbox. I tried and found that commenting out toolInfo.hwnd = hDlg; can still have the tooltip show up when mouse hovering on the toolID control.

Then what's the sense passing two HWND handles? Is it a must or useful in other cases?


Solution

  • The hwnd in the TOOLINFO structure is also used when you set the lpszText field to LPSTR_TEXTCALLBACK. When the tooltip needs text it will send a TTN_GETDISPINFO notification through a WM_NOTIFY message to that hwnd. That message's LPARAM will then be a pointer to a NMTTDISPINFO structure which you can then use to set the text of the tool tip. Good in case the tooltip's text needs to change.