Windows 系统上用 .NET/C# 查找所有窗口,并获得窗口信息

一.需要使用的 API
枚举所有窗口仅需要使用到 EnumWindows,其中需要定义一个委托 WndEnumProc 作为传入参数的类型。

剩下的我们需要其他各种方法用于获取窗口的其他属性。

GetParent 获取窗口的父窗口,这可以确认找到的窗口是否是顶层窗口。(关于顶层窗口,可以延伸 使用 SetParent 跨进程设置父子窗口时的一些问题(小心卡死) - walterlv。)
IsWindowVisible 判断窗口是否可见
GetWindowText 获取窗口标题
GetClassName 获取窗口类名
GetWindowRect 获取窗口位置和尺寸,为此我们还需要定义一个结构体 LPRECT

private delegate bool WndEnumProc(IntPtr hWnd, int lParam);
 
[DllImport("user32")]
private static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam);
 
[DllImport("user32")]
private static extern IntPtr GetParent(IntPtr hWnd);
 
[DllImport("user32")]
private static extern bool IsWindowVisible(IntPtr hWnd);
 
[DllImport("user32")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lptrString, int nMaxCount);
 
[DllImport("user32")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 
[DllImport("user32")]
private static extern bool GetWindowRect(IntPtr hWnd, ref LPRECT rect);
 
[StructLayout(LayoutKind.Sequential)]
private readonly struct LPRECT
{
    public readonly int Left;
    public readonly int Top;
    public readonly int Right;
    public readonly int Bottom;
 
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace Walterlv.WindowDetector
{
    /// 
    /// 包含枚举当前用户空间下所有窗口的方法。
    /// 
    public class WindowEnumerator
    {
        /// 
        /// 查找当前用户空间下所有符合条件的窗口。如果不指定条件,将仅查找可见窗口。
        /// 
        /// 过滤窗口的条件。如果设置为 null,将仅查找可见窗口。
        /// 找到的所有窗口信息。
        public static IReadOnlyList<WindowInfo> FindAll(Predicate<WindowInfo> match = null)
        {
            var windowList = new List<WindowInfo>();
            EnumWindows(OnWindowEnum, 0);
            return windowList.FindAll(match ?? DefaultPredicate);
            bool OnWindowEnum(IntPtr hWnd, int lparam)
            {
                // 仅查找顶层窗口。
                if (GetParent(hWnd) == IntPtr.Zero)
                {
                    // 获取窗口类名。
                    var lpString = new StringBuilder(512);
                    GetClassName(hWnd, lpString, lpString.Capacity);
                    var className = lpString.ToString();
 
                    // 获取窗口标题。
                    var lptrString = new StringBuilder(512);
                    GetWindowText(hWnd, lptrString, lptrString.Capacity);
                    var title = lptrString.ToString().Trim();
 
                    // 获取窗口可见性。
                    var isVisible = IsWindowVisible(hWnd);
 
                    // 获取窗口位置和尺寸。
                    LPRECT rect = default;
                    GetWindowRect(hWnd, ref rect);
                    var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
 
                    // 添加到已找到的窗口列表。
                    windowList.Add(new WindowInfo(hWnd, className, title, isVisible, bounds));
 
                }
 
                return true;
            }
 
        }
 
        /// 
        /// 默认的查找窗口的过滤条件。可见 + 非最小化 + 包含窗口标题。
        /// 
        private static readonly Predicate<WindowInfo> DefaultPredicate = x => x.IsVisible && !x.IsMinimized && x.Title.Length > 0;
        private delegate bool WndEnumProc(IntPtr hWnd, int lParam);
 
        [DllImport("user32")]
        private static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam);
 
        [DllImport("user32")]
        private static extern IntPtr GetParent(IntPtr hWnd);
 
        [DllImport("user32")]
        private static extern bool IsWindowVisible(IntPtr hWnd);
 
        [DllImport("user32")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lptrString, int nMaxCount);
 
        [DllImport("user32")]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 
        [DllImport("user32")]
        private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
 
        [DllImport("user32")]
        private static extern bool GetWindowRect(IntPtr hWnd, ref LPRECT rect);
 
        [StructLayout(LayoutKind.Sequential)]
        private readonly struct LPRECT
        {
            public readonly int Left;
            public readonly int Top;
            public readonly int Right;
            public readonly int Bottom;
        }
    }
 
    /// 
    /// 获取 Win32 窗口的一些基本信息。
    /// 
    public readonly struct WindowInfo
    {
        public WindowInfo(IntPtr hWnd, string className, string title, bool isVisible, Rectangle bounds) : this()
        {
            Hwnd = hWnd;
            ClassName = className;
            Title = title;
            IsVisible = isVisible;
            Bounds = bounds;
        }
 
        /// 
        /// 获取窗口句柄。
        /// 
        public IntPtr Hwnd { get; }
 
        /// 
        /// 获取窗口类名。
        /// 
        public string ClassName { get; }
 
        /// 
        /// 获取窗口标题。
        /// 
        public string Title { get; }
 
        /// 
        /// 获取当前窗口是否可见。
        /// 
        public bool IsVisible { get; }
 
        /// 
        /// 获取窗口当前的位置和尺寸。
        /// 
        public Rectangle Bounds { get; }
 
        /// 
        /// 获取窗口当前是否是最小化的。
        /// 
        public bool IsMinimized => Bounds.Left == -32000 && Bounds.Top == -32000;
 
    }
 
}

你可能感兴趣的:(C#,windows,.net,c#)