簡體   English   中英

如何獲取被點擊的任務欄縮略圖按鈕的標題?

[英]How to get the title of the taskbar thumbnail button being clicked?

我如何獲得正在單擊她的任務欄按鈕縮略圖的 window 的titleHWND

我嘗試了什么:使用CBTProc我聽HCB_ACTIVATE ,當收到消息時我調用UIAutomation閱讀ElementFromPoint

問題是......有時它可以獲取標題,有時它得到的不是title ,當這種情況發生時, title是空白的, classname名是SysListView32

我認為當收到對HCB_ACTIVATE的調用並且縮略圖已經消失時,就會發生這種情況,然后ElementFromPoint得到的不是 window 縮略圖。

不想在 CBT 中使用全局掛鈎,還有其他方法可以完成這項任務嗎?

std::wstring AccWindowFromPoint()
{
    BSTR name = NULL;

    if (SUCCEEDED(CoInitialize(NULL)))
    {
        CComPtr<IUIAutomation> automation;
        if (SUCCEEDED(automation.CoCreateInstance(CLSID_CUIAutomation8))) // or CLSID_CUIAutomation
        {
            POINT pos{};
            GetCursorPos(&pos);

            CComPtr<IUIAutomationElement> pElement;
            if (SUCCEEDED(automation->ElementFromPoint(pos, &pElement)))
            {
                pElement->get_CurrentName(&name);

                UIA_HWND uia_hwnd = nullptr;
                pElement->get_CurrentNativeWindowHandle(&uia_hwnd);
                BSTR classname = nullptr;
                pElement->get_CurrentClassName(&classname);

                RECT rect;
                pElement->get_CurrentBoundingRectangle(&rect);

                Print("[accwindowfrompoint]"
                    "\nname: ", name,
                    "\nclassname: ", classname, "\n");
            }
            else
                Print("[AccWindowFromPoint] failed to get ElementFromPoint\n");
        }
        else
            Print("[AccWindowFromPoint] failed to CoCreateInstance\n");
    }
    else
        Print("[AccWindowFromPoint] failed to CoInitialize\n");

    //CoUninitialize();

    if (name == NULL)
        return L"";

    std::wstring wstrName(name);

    return wstrName;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM