How to get tooltip text for a given HWND?

前端 未结 4 1651
眼角桃花
眼角桃花 2020-12-07 03:20

I\'m looking for a way to get the tooltip control (if any) which is associated with a given HWND. The text of the tooltip control would be sufficient, too. The closest thing

4条回答
  •  爱一瞬间的悲伤
    2020-12-07 03:45

    You could enumerate the windows looking for a tooltip control that has a parent of the required window. You'll need to supply the window together with the tool id (normally from GetDlgCtrlID)...:

    HWND hToolTipWnd = NULL;
    
    BOOL GetToolTipText(HWND hWnd, UINT nId, std::wstring& strTooltip)
    {
        hToolTipWnd = NULL;
        EnumWindows(FindToolTip, (LPARAM)hWnd);
    
        if (hToolTipWnd == NULL)
            return FALSE;
    
        WCHAR szToolText[256];
        TOOLINFO ti;
        ti.cbSize = sizeof(ti);
        ti.hwnd = hWnd;
        ti.uId = nId;
        ti.lpszText = szToolText;
    
        SendMessage(hToolTipWnd, TTM_GETTEXT, 256, (LPARAM)&ti);
        strTooltip = szToolText;
    
        return TRUE;
    }
    
    BOOL CALLBACK FindToolTip(HWND hWnd, LPARAM lParam)
    {
        WCHAR szClassName[256];
        if (GetClassName(hWnd, szClassName, 256) == 0)
            return TRUE;
    
        if (wcscmp(szClassName, L"tooltips_class32") != 0)
            return TRUE;
        if (GetParent(hWnd) != (HWND)lParam)
            return TRUE;
    
        hToolTipWnd = hWnd;
    
        return FALSE;
    }
    

提交回复
热议问题