C# 使用;Inspect.exe“;can';我找不到一些元素,为什么?

C# 使用;Inspect.exe“;can';我找不到一些元素,为什么?,c#,ui-automation,C#,Ui Automation,我想用c#编写一个UI自动化程序,但是我找不到一些带有“Inspect.exe”的元素,找不到文本标签的图片(例如image1),为什么 图1: 图2: 代码示例: var desktop = AutomationElement.RootElement; var condition = new PropertyCondition(AutomationElement.NameProperty, "Customer Register"); var window = desktop.FindFir

我想用c#编写一个UI自动化程序,但是我找不到一些带有“Inspect.exe”的元素,找不到文本标签的图片(例如image1),为什么

图1:

图2:

代码示例:

var desktop = AutomationElement.RootElement;

var condition = new PropertyCondition(AutomationElement.NameProperty, "Customer Register");

var window = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, condition);

从inspect的屏幕截图中可以看出,您发布的获取元素的代码是正确的。我有一种感觉,UI自动化并没有因为文本编码而恢复名称。除非您能够访问源代码,并且能够搞乱它如何获取和设置这些标签的文本,否则就不可能通过UI自动化检索文本

您可以使用现有的代码,并使用win32 api中窗口的本机窗口句柄来获取文本

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);
 public static string GetWindowTextRaw(IntPtr hwnd)
 {
     // Allocate correct string length first
     int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
     StringBuilder sb = new StringBuilder(length + 1);
     SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
     return sb.ToString();
 }

 public static void YourMethod() 
 {
     var desktop = AutomationElement.RootElement;

     var process = Process.Start("Path/To/Your/Process.exe");

     var condition = new PropertyCondition(AutomationElement.ProcessId, process.Id);

     var window = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, condition);

     var windowTitle = GetWindowTextRaw(window.NativeWindowHandle)
 }
资料来源:



从inspect的屏幕截图中可以看出,您发布的获取元素的代码是正确的。我有一种感觉,UI自动化并没有因为文本编码而恢复名称。除非您能够访问源代码,并且能够搞乱它如何获取和设置这些标签的文本,否则就不可能通过UI自动化检索文本

您可以使用现有的代码,并使用win32 api中窗口的本机窗口句柄来获取文本

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);
 public static string GetWindowTextRaw(IntPtr hwnd)
 {
     // Allocate correct string length first
     int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
     StringBuilder sb = new StringBuilder(length + 1);
     SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
     return sb.ToString();
 }

 public static void YourMethod() 
 {
     var desktop = AutomationElement.RootElement;

     var process = Process.Start("Path/To/Your/Process.exe");

     var condition = new PropertyCondition(AutomationElement.ProcessId, process.Id);

     var window = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, condition);

     var windowTitle = GetWindowTextRaw(window.NativeWindowHandle)
 }
资料来源:



非常感谢您的回答,但这不是答案。我用“spy++”发现它是一个面板控件,只有一个面板句柄。标签文本应该是什么,由“.Net System.Drawing”名称空间的DrawString方法绘制非常感谢您的回答,但这不是答案。我用“spy++”发现它是一个面板控件,只有一个面板句柄。标签文本应该是什么样的绘画,由“.Net System.Drawing”DrawString方法的名称空间绘制