我正在使用Visual Studio2017。我在解决方案中添加了两个项目。一个项目是使用WPF的C#。另一个是带有ATL的VC ++。
从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使用什么方法?
最佳答案
Microsoft Active Accessibility / MSAA(基于IAccessible
接口)是旧版API。您现在应该使用Windows Automation。 UIA基于COM,并使用接口,最重要的是IUIAutomationElement。 Inspect.exe使用UIA或MSAA。
注意.NET与UIA兼容,WPF可以通过AutomationPeer class和UIElement.OnCreateAutomationPeer Method方法很容易地将其UI元素公开给UIA。 WPF提供了一个默认实现,可以根据需要对其进行调整。
这是一个使用UIA API而不是MSAA的类似于C ++的示例(我们可以使用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;
}
关于c# - AccessibleObjectFromPoint返回客户端对象,而不是复选框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44503692/