【问题标题】:Problems getting title of windowsapps, such as Photos获取 Windows 应用程序标题的问题,例如照片
【发布时间】:2020-08-26 01:01:06
【问题描述】:

我需要获取显示特定图像的窗口句柄,以便它可以与其他窗口对齐。 ShellExecuteEx 用于为图像类型启动已注册的应用程序,这可以正常工作(即使注册的应用程序是 DLL,例如“照片查看器”)。

不幸的是,Windows 10 下的新 windowsapps(例如“照片”又名“microsoft.photos.exe”)似乎并不公平。 AssocQueryString 表示,如果我手动关联“照片”,则没有应用程序与该图像类型关联,即使当我双击此类图像时“照片”启动正常。

“照片”窗口的标题栏清楚地显示“照片 - file.jpg”之类的内容,但对 GetWindowText 的调用仅返回“照片”部分,没有识别文件名。我也尝试向窗口发送 WM_GETTEXT 消息,但结果是一样的。

这些windowsapps有什么奇怪的吗?只返回窗口标题的通用部分的理由是什么?有没有办法获得显示的整个窗口标题?

【问题讨论】:

  • 窗口标题是“照片”(您可以在任务栏工具提示或 ALT-TAB 窗口列表中看到)。 “照片 - blah.jpeg”文本是内部的/不可靠的,只是绘制的。如果你想要内部细节,你必须使用 UI 自动化。例如,使用 SDK 中的检查工具,您将看到更多详细信息:docs.microsoft.com/en-us/windows/win32/winauto/inspect-objects 您使用该工具看到的任何内容都具有编程访问权限。
  • 你从来没有解释过你要解决什么问题。
  • 谢谢@SimonMourier。我没有注意到它不是一个真正的窗口标题。我已经习惯了在其他图像查看器的真实标题栏中看到文件名。
  • @IInspectable,问题确实说我正在尝试获取显示图像的窗口的句柄。在所有其他情况下,我使用标题栏将一个窗口与另一个窗口区分开来。
  • 这个问题本质上是在解释您提出的解决方案。它没有解释甚至暗示 problem 是什么。这就是众所周知的XY Problem

标签: windows winapi microsoft-metro


【解决方案1】:

问题确实表明我正在尝试获取显示图像的窗口的句柄。在所有其他情况下,我都使用标题栏来区分一个窗口。

在cmets中已经指出需要使用UI Automation才能可靠地获取标题文本。

您可以尝试以下代码获取控件标题。

#include <Windows.h>
#include <stdio.h>
#include <UIAutomation.h>

IUIAutomation* pClientUIA;
IUIAutomationElement* pRootElement;

void FindControl(const long controlType)
{
    HRESULT hr;
    BSTR name;
    IUIAutomationCondition* pCondition;
    VARIANT varProp;
    varProp.vt = VT_I4;
    varProp.uintVal = controlType;
    hr = pClientUIA->CreatePropertyCondition(UIA_ControlTypePropertyId, varProp, &pCondition);
    if (S_OK != hr)
    {
        printf("CreatePropertyCondition error: %d\n", GetLastError());
    }

    IUIAutomationElementArray* pElementFound;
    hr = pRootElement->FindAll(TreeScope_Subtree, pCondition, &pElementFound);
    if (S_OK != hr)
    {
        printf("CreatePropertyCondition error: %d\n", GetLastError());
    }
    IUIAutomationElement* pElement;
    hr = pElementFound->GetElement(0, &pElement);
    if (S_OK != hr)
    {
       printf("CreatePropertyCondition error: %d\n", GetLastError());
    }
    hr = pElement->get_CurrentName(&name);
    if (S_OK != hr)
    {
       printf("CreatePropertyCondition error: %d\n", GetLastError());
    }
    wprintf(L"Control Name: %s\n", name);
}

int main()
{

    HRESULT hr = CoInitializeEx(NULL, COINITBASE_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
    if (S_OK != hr)
    {
        printf("CoInitializeEx error: %d\n", GetLastError());
        return 1;
    }



    hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_IUIAutomation, reinterpret_cast<void**>(&pClientUIA));
    if (S_OK != hr)
    {
        printf("CoCreateInstance error: %d\n", GetLastError());
        return 1;
    }

    HWND hwnd = (HWND)0x00030AF6;  //hwnd of "Photos"
    if (hwnd == NULL)
    {
        printf("FindWindow error: %d\n", GetLastError());
        return 1;
    }

    hr = pClientUIA->ElementFromHandle(hwnd, &pRootElement);
    if (S_OK != hr)
    {
        printf("ElementFromHandle error: %d\n", GetLastError());
        return 1;
    }

    FindControl(UIA_TextControlTypeId);
}

调试:

【讨论】:

    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多