Вот два одинаковых приложения.Они написаны на C ++ / CLI и C # соответственно
Вопрос Почему UI Api показывает разные результаты для одного и того же окна и почти идентичного кода?Код, кажется, не читает дерево пользовательского интерфейса правильно.
код C ++ / CLI
#include "stdafx.h"
using namespace System;
void MarshalString(String ^ s, std::string& os);
int main(array<System::String ^> ^args)
{
std::string p;
std::string s;
HWND hWnd = (HWND)0x005D0B52;
AutomationElement^ ele = AutomationElement::FromHandle(IntPtr(hWnd));
TreeWalker ^pWalk = TreeWalker::ControlViewWalker;
AutomationElement^ pNode = pWalk->GetFirstChild(ele);
while (pNode) {
String^ localCT = pNode->Current.LocalizedControlType;
MarshalString(localCT, s);
std::cout << "Localized string: " << s << std::endl;
pNode = pWalk->GetNextSibling(pNode);
}
std::cin >> p;
return 0;
}
void MarshalString(String ^ s, std::string& os) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
код C #
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
IntPtr ptr = new IntPtr(0x005D0B52);
AutomationElement ele = AutomationElement.FromHandle(ptr);
if (ele != null)
{
TreeWalker pWalk = TreeWalker.ControlViewWalker;
AutomationElement pNode = pWalk.GetFirstChild(ele);
while (pNode!=null)
{
Console.WriteLine($"Control Type = {pNode.Current.LocalizedControlType}");
pNode = pWalk.GetNextSibling(pNode);
}
Console.Read();
}
}
}
}
Выход для C ++ / CLI Выход C ++ / CLI
Выход для выхода C # C #