在Win 7 SP1和方便更新(最新发布的Win 7)上,我的C++代码使用WindowsAutomation3.0的CUIAutomation无法从内置WordPad应用程序中的RichEdit控件获取TextPattern。但是,使用UIAutomationClient和UIAutomationTypes的等效的UIAutomationTypes代码可以。
Win 10更好的成功: C++代码和C#代码都成功地获得了TextPattern。
我的主要项目与另一个C# UIA应用程序有兼容性问题,当我在Win 10上使用C++代码时,这个应用程序就消失了。所以我真的很想在Win 7上使用C++代码。有人知道为什么C++代码失败以及如何修复吗?我很惊讶从内置的TextPattern控件中获取一个简单的RichEdit不能可靠地工作!
下面是C#代码(更容易阅读!),后面是C++代码:
using System;
using System.Threading;
using System.Windows.Automation;
namespace UIAutomationNET
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Getting element at cursor in 3 seconds...");
Thread.Sleep(3000);
var element = AutomationElement.FocusedElement;
if (element != null)
{
var textPatternElement = element.FindFirst(
TreeScope.Subtree,
new PropertyCondition(AutomationElement.IsTextPatternAvailableProperty, true));
if (textPatternElement == null)
Console.WriteLine("No element supporting TextPattern found.");
else
Console.WriteLine("TextPattern is supported! :-)");
}
}
}
}以下C++代码基于此MSDN代码库示例
#include <windows.h>
#include <ole2.h>
#include <uiautomation.h>
#include <strsafe.h>
IUIAutomation *_automation;
int _cdecl wmain(_In_ int argc, _In_reads_(argc) WCHAR* argv[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
// Initialize COM before using UI Automation
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
wprintf(L"CoInitialize failed, HR:0x%08x\n", hr);
}
else
{
// Use CUIAutomation instead of CUIAutomation8 on Win 7
hr = CoCreateInstance(__uuidof(CUIAutomation), NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&_automation));
if (FAILED(hr))
{
wprintf(L"Failed to create a CUIAutomation, HR: 0x%08x\n", hr);
}
else
{
IUIAutomationElement *element = NULL;
wprintf( L"Getting element at cursor in 3 seconds...\n" );
Sleep(3000);
POINT pt;
GetCursorPos(&pt);
hr = _automation->ElementFromPoint(pt, &element);
if (FAILED(hr))
{
wprintf( L"Failed to ElementFromPoint, HR: 0x%08x\n\n", hr );
}
if (SUCCEEDED(hr) && element != NULL)
{
IUIAutomationElement *textElement = NULL;
// Create a condition that will be true for anything that supports Text Pattern
// Use UIA_IsTextPatternAvailablePropertyId instead of UIA_IsTextPattern2AvailablePropertyId on Win 7
IUIAutomationCondition* textPatternCondition;
VARIANT trueVar;
trueVar.vt = VT_BOOL;
trueVar.boolVal = VARIANT_TRUE;
hr = _automation->CreatePropertyCondition(UIA_IsTextPatternAvailablePropertyId, trueVar, &textPatternCondition);
if (FAILED(hr))
{
wprintf(L"Failed to CreatePropertyCondition, HR: 0x%08x\n", hr);
}
else
{
// Actually do the search
hr = element->FindFirst(TreeScope_Subtree, textPatternCondition, &textElement);
if (FAILED(hr))
{
wprintf(L"FindFirst failed, HR: 0x%08x\n", hr);
}
else if (textElement == NULL)
{
wprintf(L"No element supporting TextPattern found.\n");
hr = E_FAIL;
}
else
{
wprintf(L"TextPattern is supported! :-)\n");
}
textPatternCondition->Release();
}
}
_automation->Release();
}
CoUninitialize();
}
return 0;
}发布于 2021-02-23 01:41:29
如果我没记错的话,Win7本机UI自动化提供程序不支持TextPattern,而托管UI自动化提供程序支持。
本机UI自动化提供程序直到Windows8才添加TextPattern支持。
https://stackoverflow.com/questions/65333921
复制相似问题