gpt4 book ai didi

c++ - AccessibleObjectFromPoint() 返回不正确的对象

转载 作者:行者123 更新时间:2023-11-30 04:49:22 25 4
gpt4 key购买 nike

我正在创建一个程序,该程序使用 AccessibleObjectFromPoint() 通过鼠标光标选择对象,但它会产生不正确的结果。

I hovered the mouse at this icon but it selects a different icon instead.

这是我的示例代码:

#include <windows.h>
#include <oleacc.h>
#include <cstdio>
#include <iostream>
#include <string>

#pragma comment(lib, "oleacc.lib")

HRESULT SelectItemAtPoint(POINT point)
{
VARIANT varItem;
IAccessible* pAcc;
HRESULT hr = AccessibleObjectFromPoint(point, &pAcc, &varItem);
if ((hr == S_OK))
{
hr = pAcc->accSelect((SELFLAG_TAKEFOCUS | SELFLAG_TAKESELECTION), varItem);
VariantClear(&varItem);
pAcc->Release();
}
return hr;
}

int main()
{
CoInitialize(NULL);
while (true)
{
POINT pt;
GetCursorPos(&pt);
printf("x: %d y: %d", pt.x, pt.y);
SelectItemAtPoint(pt);
Sleep(50);
}
return 0;
}

在图像中,我在 Unreal Engine 上徘徊,但程序选择了 MATLAB。我还检查了光标点。有什么方法可以解决这个问题吗?

编辑:我正在使用带有 Visual Studio 2017 的 Windows 10 家庭版

最佳答案

引自微软文档:

Source Link

Microsoft Active Accessibility does not use logical coordinates. The following methods and functions either return physical coordinates or take them as parameters.

  • IAccessible::accHitTest
  • IAccessible::accLocation
  • AccessibleObjectFromPoint

By default, an Microsoft Active Accessibility client application running in a non-96-dpi environment will not be able to obtain correct results from these calls. For example, because the cursor position is in logical coordinates, the client cannot simply pass these coordinates to AccessibleObjectFromPoint to obtain the element that is under the cursor.

The solution is in two parts:

  • Make the client application "dpi-aware". To do this, call the SetProcessDPIAware function at startup. This function makes the entire process dpi-aware, meaning that all windows that belong to the process are unscaled.

  • Use functions that are dpi-aware. For example, to get cursor coordinates, call the GetPhysicalCursorPos function. Do not use GetCursorPos; its behavior in dpi-aware applications is undefined.

If your application performs direct cross-process communication with non-dpi-aware applications, you may have convert between logical and physical coordinates by using the PhysicalToLogicalPoint and LogicalToPhysicalPoint functions.

因此将 GetCursorPos() 更改为 GetPhysicalCursorPos() 解决了我的问题。

关于c++ - AccessibleObjectFromPoint() 返回不正确的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55438576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com