简体   繁体   English

如何获取给定 HWND 的工具提示文本?

[英]How to get tooltip text for a given HWND?

I'm looking for a way to get the tooltip control (if any) which is associated with a given HWND.我正在寻找一种方法来获取与给定 HWND 关联的工具提示控件(如果有)。 The text of the tooltip control would be sufficient, too.工具提示控件的文本也足够了。 The closest thing I found is the TTM_GETTEXT message, but it's meant to be sent to the tooltip control itself instead of the tool it's associated with.我发现的最接近的是TTM_GETTEXT消息,但它的目的是发送到工具提示控件本身而不是与其关联的工具。 I don't have a handle to the tooltip control though.不过,我没有工具提示控件的句柄。 Does anybody know how to do this?有人知道怎么做这个吗?

All this is done using plain Windows API in C++.所有这些都是使用 C++ 中的普通 Windows API 完成的。

There doesn't seem to be a specific message to get the tip or its text from the control, but this is how MFC's CWnd class implements OnToolHitTest(), which you should be able to adapt to Win32:似乎没有从控件中获取提示或其文本的特定消息,但这就是 MFC 的 CWnd 类实现 OnToolHitTest() 的方式,您应该能够适应 Win32:

INT_PTR SomeFunction(HWND hWndChild, TOOLINFO *pTI)
{
    if (hWndChild != NULL) // Your HWND being tested
    {
        // return positive hit if control ID isn't -1
        INT_PTR nHit = _AfxGetDlgCtrlID(hWndChild);
        // Replace with GetDlgCtrlID().

        // hits against child windows always center the tip
        if (pTI != NULL && pTI->cbSize >= sizeof(AFX_OLDTOOLINFO))
        {
            // setup the TOOLINFO structure
            pTI->hwnd = m_hWnd;
            pTI->uId = (UINT_PTR)hWndChild;
            pTI->uFlags |= TTF_IDISHWND;
            pTI->lpszText = LPSTR_TEXTCALLBACK;

            // set TTF_NOTBUTTON and TTF_CENTERTIP if it isn't a button
            if (!(::SendMessage(hWndChild, WM_GETDLGCODE, 0, 0) & DLGC_BUTTON))
                pTI->uFlags |= TTF_NOTBUTTON|TTF_CENTERTIP;
        }
        return nHit;
    }
    return -1;  // not found
}

Hopefully this will be useful.希望这将是有用的。

To get tooltip text from some control you could use TTN_NEEDTEXT message.要从某些控件获取工具提示文本,您可以使用TTN_NEEDTEXT消息。 It was designed to be used by the ToolTip control, but I cannot see any reason why you could not send it from other place.它被设计为供 ToolTip 控件使用,但我看不出有什么理由不能从其他地方发送它。

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 )...:您需要提供窗口和工具 ID(通常来自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;
}

I don't know if the window whose tooltip you want to retrieve is a child of a window you have created.我不知道您要检索其工具提示的窗口是否是您创建的窗口的子窗口。

If this is the case, you can handle the NM_TOOLTIPSCREATED notification, which is sent by a child window to its parent when it creates a tooltip (or should be sent: it is true for common controls but I don't know for other kinds of windows).如果是这种情况,您可以处理 NM_TOOLTIPSSCREATED 通知,该通知由子窗口在创建工具提示时发送到其父窗口(或应该发送:对于常见控件是正确的,但我不知道其他类型的视窗)。 This notification includes a handle to the tooltip window.此通知包括工具提示窗口的句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM