【问题标题】:Using "Inspect.exe" can't find some elements, why?使用“Inspect.exe”找不到某些元素,为什么?
【发布时间】:2017-02-18 07:19:54
【问题描述】:

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

image1:https://i.stack.imgur.com/NqpKA.png

图片2:https://i.stack.imgur.com/2PIuj.png

代码示例:

var desktop = AutomationElement.RootElement;

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

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

【问题讨论】:

    标签: c# ui-automation


    【解决方案1】:

    您发布的用于获取元素的代码与我在您从检查的屏幕截图中看到的内容是正确的。我有一种感觉,因为文本编码,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)
     }
    

    来源:

    GetWindowText
    SendMessage

    【讨论】:

    • 非常感谢您的回答,但这不是答案。我用“spy++”发现它是一个面板控件,只有一个面板句柄。标签文字应该画什么,通过DrawString方法的“.Net System.Drawing”命名空间
    猜你喜欢
    • 2019-03-02
    • 2016-10-04
    • 2020-08-11
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多