有没有办法让 MSAA IAccessible 遍历更快?

Joh*_*ers 5 c++ winapi msdn accessibility

现在我有这个 C++ 函数(删除了安全检查和一些代码以使其更具可读性):

\n\n
HRESULT WalkTreeWithAccessibleChildren(wstringstream *ss, IAccessible* pAcc, int depth)\n{\n\xc2\xa0 \xc2\xa0 long childCount;\n\xc2\xa0 \xc2\xa0 long returnCount;   \n\n\xc2\xa0 \xc2\xa0 VARIANT* pArray = new VARIANT[childCount];\n\xc2\xa0 \xc2\xa0 hr = AccessibleChildren(pAcc, 0L, childCount, pArray, &returnCount);\n\xc2\xa0 \xc2\xa0 for (int x = 0; x < returnCount; x++) {\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 VARIANT vtChild = pArray[x];\n        Get the role and name of the component here\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // If it's an accessible object, get the IAccessible, and recurse.\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (vtChild.vt == VT_DISPATCH) {\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 IDispatch* pDisp = vtChild.pdispVal;\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 IAccessible* pChild = NULL;\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 hr = pDisp->QueryInterface(IID_IAccessible, (void**) &pChild);\n         \xc2\xa0  WalkTreeWithAccessibleChildren(ss, pChild, depth + 1);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于一些组件相对较少(200 个左右)的程序,例如 Paint.NET,这大约需要 2 秒,有没有一种方法可以让这个函数更快,在一个 COM 调用中获取所有组件或类似的东西?

\n

小智 4

这在某种程度上取决于您想要做什么。

如果您正在查找特定项目,有时可以使用导航 (accNavigate) 更快地找到某个项目,而不是查看所有项目。

如果您确实需要获取所有项目,则后台线程效果很好。

另一种选择是使用 UIAutomation api,它仍然支持所有 IAccessible 服务器,并且内置了更丰富的缓存和过滤功能。有关详细信息,请参阅 MSDN 中的 IUIAutomationCacheRequest 及其方法 TreeFilter 和 TreeScope。如果您处于特定项目案例中,它还有一种方法来搜索特定项目。

将 UIAutomation 视为 IAccessible 的超集。UIAutomation 可在 Vista SP2 及更高版本上使用。

  • [文档](http://msdn.microsoft.com/en-us/library/windows/desktop/dd318473(v=vs.85).aspx) 指出“accNavigate 方法已弃用,不应使用” 。 (2认同)