How to get tooltip text for a given HWND?

前端 未结 4 1656
眼角桃花
眼角桃花 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:27

    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:

    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.

提交回复
热议问题