2017-02-14 103 views
1

我想用c#寫一個UI自動化程序,但是我找不到一些帶有「Inspect.exe」的元素,無法找到文本標籤的圖片(比如image1),爲什麼?使用「Inspect.exe」找不到某些元素,爲什麼?

此搜索: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); 

回答

0

您發佈用於獲取元素的代碼是從我可以從檢查你的截圖中看到正確的。我有一種感覺,由於文本編碼的原因,UI Automation沒有得到名稱。除非您有權訪問源代碼,並且可能會弄亂它的獲取方式併爲這些標籤設置文本,否則將無法通過UI Automation檢索文本。

您可以使用您擁有的代碼,並使用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

+0

非常感謝您的回答,但這不是問題的答案。我使用「spy ++」發現它是一個面板控件,只有一個面板句柄。 DrawString方法的「.Net System.Drawing」命名空間應該如何繪製標籤文本 –

相關問題