AccessibleObjectFromPoint返回客户端对象而不是复选框

时间:2017-06-12 15:51:36

标签: c# c++ wpf msaa

我正在使用Visual Studio 2017.我在解决方案中添加了两个项目。一个项目是使用WPF的C#。另一个是VC ++与ATL。 从C#开始,我在VC ++项目中调用一个函数,它设置了低级鼠标钩子。低级鼠标proc中的部分代码如下:

MSLLHOOKSTRUCT stMouse = *(MSLLHOOKSTRUCT*)(lParam);
POINT pt = stMouse.pt;
IAccessible* pAcc;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromPoint(pt, &pAcc, &varChild);
VARIANT varRole;
hr = pAcc->get_accRole(varChild, &varRole);

当我通过单击MS Word 2013中“视图”选项卡下的复选框进行测试时,我将获得WM_LBUTTONDOWN和WM_LBUTTONUP消息的客户端角色。但我应该把这个角色作为复选框。 我检查了Windows 10 SDK附带的Inspect.exe。 Inspect.exe正确显示角色作为复选框。 Inspect.exe有两个选项 - 一个用于查看UI自动化属性,另一个用于查看MSAA属性。我正在看MSAA的属性。 我怎样才能找到正确的角色? deos Inspect.exe使用什么方法?

1 个答案:

答案 0 :(得分:3)

Microsoft Active Accessibility / MSAA(基于IAccessible接口)是一种遗留API。您现在应该使用Windows Automation。 UIA基于COM,并使用接口,最重要的是IUIAutomationElement。 Inspect.exe使用UIA或MSAA。

注意.NET与UIA兼容,WPF可以通过AutomationPeer classUIElement.OnCreateAutomationPeer Method方法轻松地将其UI元素公开给UIA。 WPF提供了一个默认实现,可以根据需要进行调整。

以下是您在C ++中使用UIA API的类似示例,而不是MSAA(我们可以使用C#编写相同的代码):

#include "stdafx.h" // needs <uiautomation.h>

class CCoInitialize { // https://blogs.msdn.microsoft.com/oldnewthing/20040520-00/?p=39243
public:
  CCoInitialize() : m_hr(CoInitialize(NULL)) { }
  ~CCoInitialize() { if (SUCCEEDED(m_hr)) CoUninitialize(); }
  operator HRESULT() const { return m_hr; }
  HRESULT m_hr;
};

// this is a poor-man COM object class
class CHandler : public IUIAutomationEventHandler
{
public:
  HRESULT QueryInterface(REFIID riid, LPVOID * ppv)
  {
    if (!ppv) return E_INVALIDARG;

    *ppv = NULL;
    if (riid == IID_IUnknown || riid == IID_IUIAutomationEventHandler)
    {
      *ppv = this;
      return NOERROR;
    }
    return E_NOINTERFACE;
  }
  ULONG AddRef() { return 1; }
  ULONG Release() { return 1; }

  // this will be called by UIA
  HRESULT HandleAutomationEvent(IUIAutomationElement *sender, EVENTID eventId)
  {
    wprintf(L"Event id: %u\n", eventId);
    return S_OK;
  }
};

int main()
{
  CCoInitialize init;

  // this sample uses Visual Studio's ATL smart pointers, but it's not mandatory at all
  CComPtr<IUIAutomation> uia;
  if (SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation)))
  {
    // get mouse pos now
    POINT pt;
    GetCursorPos(&pt);

    // find what type of "control" was under the mouse
    CComPtr<IUIAutomationElement> element;
    if (SUCCEEDED(uia->ElementFromPoint(pt, &element)))
    {
      CComBSTR type;
      element->get_CurrentLocalizedControlType(&type);
      wprintf(L"type at %u,%u: %s\n", pt.x, pt.y, type.m_str);
    }

    // get root
    CComPtr<IUIAutomationElement> root;
    if (SUCCEEDED(uia->GetRootElement(&root)))
    {
      // add a handler that will trigger every time you open any window on the desktop
      // in the real world, you'll need to delete this pointer to avoid memory leaks of course
      CHandler *pHandler = new CHandler();
      if (SUCCEEDED(uia->AddAutomationEventHandler(UIA_Window_WindowOpenedEventId, root, TreeScope::TreeScope_Children, NULL, pHandler)))
      {
        // since this is a console app, we need to run the Windows message loop otherwise, you'll never get any UIA events
        // for this sample, just press CTRL-C to stop the program. in the real world, you'll need to get out properly
        // if you have a standard windows app, the loop will be already there
        while (TRUE)
        {
          MSG msg;
          while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
          {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
          }
        }
      }
    }
  }
  return 0;
}
相关问题