c#取指定类名窗口的列表信息内容
时间: 2023-12-04 15:02:12 浏览: 121
你可以使用 Windows API 来实现获取指定类名窗口的列表信息内容。具体实现步骤如下:
1. 引入 Windows API 相关命名空间:
```
using System.Runtime.InteropServices;
using System.Text;
```
2. 定义 Windows API 函数:
```
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
```
3. 实现获取指定类名窗口的列表信息内容的方法:
```
public static List<string> GetWindowList(string className)
{
List<string> windowList = new List<string>();
IntPtr hwnd = FindWindow(className, null);
while (hwnd != IntPtr.Zero)
{
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1);
GetWindowText(hwnd, sb, sb.Capacity);
windowList.Add(sb.ToString());
hwnd = FindWindowEx(IntPtr.Zero, hwnd, className, null);
}
return windowList;
}
```
其中,`className` 参数为指定的窗口类名,`GetWindowText()` 函数用于获取窗口标题,`FindWindowEx()` 用于查找子窗口,`windowList` 用于存储窗口列表信息。
使用示例:
```
List<string> windowList = GetWindowList("类名");
foreach (string window in windowList)
{
Console.WriteLine(window);
}
```
阅读全文