您發佈用於獲取元素的代碼是從我可以從檢查你的截圖中看到正確的。我有一種感覺,由於文本編碼的原因,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
非常感謝您的回答,但這不是問題的答案。我使用「spy ++」發現它是一個面板控件,只有一個面板句柄。 DrawString方法的「.Net System.Drawing」命名空間應該如何繪製標籤文本 –