在C#中使用EnumWindows函数遍历窗口时,可以通过指定不显示隐藏窗口的标志来过滤掉隐藏窗口。具体步骤如下:
首先定义一个回调函数来处理EnumWindows函数枚举到的窗口:public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);使用EnumWindows函数遍历所有窗口,并在回调函数中判断窗口是否可见:[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, int lParam);[DllImport("user32.dll", SetLastError = true)]public static extern bool IsWindowVisible(IntPtr hWnd);public static bool EnumWindowCallback(IntPtr hWnd, int lParam){ if (IsWindowVisible(hWnd)) { // 处理可见窗口的逻辑 } return true;}调用EnumWindows函数并传入回调函数:EnumWindows(EnumWindowCallback, 0);通过这种方式,可以过滤掉隐藏窗口,只处理可见窗口。


