WindowsHelper

本文介绍了WindowsHelper类,包含了用于处理Windows系统窗口的各种功能,如获取窗口句柄、位置、文本、可见性等,并提供获取打开窗口信息的方法。
摘要由CSDN通过智能技术生成

WindowsHelper

namespace ZL_WorkPlatform
{
    public class WindowsHelper
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);


        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public static IntPtr GetHandleWindow(string title)
        {
            return FindWindow(null, title);
        }

        private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);

        [DllImport("USER32.DLL")]
        private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("USER32.DLL")]
        private static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("USER32.dll")]
        public static extern bool IsIconic(IntPtr hWnd);

        [DllImport("USER32.DLL")]
        private static extern IntPtr GetShellWindow();

        //[DllImport("USER32.DLL")]
        //private static extern IntPtr GetDesktopWindow();

        [DllImport("dwmapi.dll")]
        static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out bool pvAttribute, int cbAttribute);
        [Flags]
        public enum DwmWindowAttribute : uint
        {
            DWMWA_NCRENDERING_ENABLED = 1,
            DWMWA_NCRENDERING_POLICY,
            DWMWA_TRANSITIONS_FORCEDISABLED,
            DWMWA_ALLOW_NCPAINT,
            DWMWA_CAPTION_BUTTON_BOUNDS,
            DWMWA_NONCLIENT_RTL_LAYOUT,
            DWMWA_FORCE_ICONIC_REPRESENTATION,
            DWMWA_FLIP3D_POLICY,
            DWMWA_EXTENDED_FRAME_BOUNDS,
            DWMWA_HAS_ICONIC_BITMAP,
            DWMWA_DISALLOW_PEEK,
            DWMWA_EXCLUDED_FROM_PEEK,
            DWMWA_CLOAK,
            DWMWA_CLOAKED,
            DWMWA_FREEZE_REPRESENTATION,
            DWMWA_LAST
        }
        public static IDictionary<IntPtr, string> GetOpeningWindows()
        {
            IntPtr shellWindow = GetShellWindow();
            Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();

            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                if (hWnd == shellWindow) return true;
                if (!IsWindowVisible(hWnd)) return true;
                if (IsIconic(hWnd)) return true;

                bool isCloacked;
                DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
                if (isCloacked)
                    return true;


                int length = GetWindowTextLength(hWnd);
                if (length == 0) return true;

                StringBuilder builder = new StringBuilder(length);
                GetWindowText(hWnd, builder, length + 1);
                windows[hWnd] = builder.ToString();

                return true;

            }, 0);

            return windows;
        }

        public static IDictionary<IntPtr, string> GetOpeningWindowsAll()
        {
            IntPtr shellWindow = GetShellWindow();
            Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();

            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                if (hWnd == shellWindow) return true;
                if (!IsWindowVisible(hWnd)) return true;
                //if (IsIconic(hWnd)) return true;

                bool isCloacked;
                DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
                if (isCloacked)
                    return true;


                int length = GetWindowTextLength(hWnd);
                if (length == 0) return true;

                StringBuilder builder = new StringBuilder(length);
                GetWindowText(hWnd, builder, length + 1);
                windows[hWnd] = builder.ToString();

                return true;

            }, 0);

            return windows;
        }

        public static IDictionary<IntPtr, string> GetOpeningWindowsAllWithHide()
        {
            IntPtr shellWindow = GetShellWindow();
            Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();

            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                if (hWnd == shellWindow) return true;

                bool isCloacked;
                DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
                if (isCloacked)
                    return true;


                int length = GetWindowTextLength(hWnd);
                if (length == 0) return true;

                StringBuilder builder = new StringBuilder(length);
                GetWindowText(hWnd, builder, length + 1);
                windows[hWnd] = builder.ToString();

                return true;

            }, 0);

            return windows;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner

            public override string ToString()
            {
                return $"Left:{Left}, Top:{Top}, Right:{Right}, Bottom:{Bottom}";
            }
        }


        public static Rectangle GetWinRectangleByName(string winName)
        {
            if (string.IsNullOrEmpty(winName))
                return Rectangle.Empty;
            var winHandle = GetHandleWindow(winName);
            return GetWinRectangleByHandle(winHandle);
        }

        public static Rectangle GetWinRectangleByHandle(IntPtr hWnd)
        {
            if (hWnd == IntPtr.Zero) return Rectangle.Empty;
            RECT rect;
            var suc = GetWindowRect(new HandleRef(null, hWnd), out rect);
            //Debug.WriteLine($"{winName} RECT: {rect.ToString()}");
            Rectangle rectangle = new Rectangle();
            rectangle.X = rect.Left;
            rectangle.Y = rect.Top;
            rectangle.Width = rect.Right - rect.Left;
            rectangle.Height = rect.Bottom - rect.Top;
            //Debug.WriteLine($"{winName} Rectangle: {rectangle.ToString()}");
            return rectangle;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是刘彦宏吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值