温馨提示×

如何使用C# EnumWindows遍历窗口

c#
小樊
97
2024-07-18 17:53:30
栏目: 编程语言

您可以使用以下方法来遍历窗口:

  1. 首先,您需要导入用户32.dll库:
using System;
using System.Runtime.InteropServices;
  1. 然后定义EnumWindows函数:
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
  1. 编写遍历窗口的方法:
public static bool EnumWindowCallback(IntPtr hwnd, IntPtr lParam)
{
    // 进行窗口处理的代码
    // 例如获取窗口标题等
    return true; // 返回true继续遍历,返回false停止遍历
}

public void EnumerateWindows()
{
    EnumWindows(EnumWindowCallback, IntPtr.Zero);
}
  1. 调用遍历窗口的方法:
EnumerateWindows();

通过这种方法,您可以遍历所有窗口,并在EnumWindowCallback方法中处理每个窗口的信息。

0