C# API之常用操作窗口类函数详解[查找所有窗口、获取目标句柄的类名、获取窗口文本、获取当前活动窗口、通过窗口句柄获取线程ID、获取指定窗口位置]

        /// 
        /// 查找所有窗口(只要是在进程里面的)
        /// 如果不限制类名或者标题使用null代替
        /// 
        /// 窗口类名,不限制使用null
        /// 窗口标题,不限制使用null
        /// 找到的窗口句柄
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // 遍历窗口的所有子窗口,通过CallBack回调
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        public delegate bool CallBack(IntPtr hwnd, int lParam);

        // 获取窗口的类名
        /// 
        /// 获取目标句柄的类名
        /// 
        /// 目标窗口句柄
        /// 返回的Class名称
        /// 允许返回的Class名称的字符数量上限
        /// 获取到的实际Class字符长度
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        // 判断窗口是否可见
        /// 
        /// 判断目标窗口是否可见
        /// 
        /// 窗口句柄
        /// 可见true,不可见false
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        // 获取窗口文本长度
        /// 
        /// 获取目标窗口标题的文本长度
        /// 
        /// 目标窗口句柄
        /// 标题文本长度
        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);

        /// 
        /// 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount
        /// 
        /// 窗口句柄
        /// 返回目标窗口的内容
        /// 允许返回的字符数量上限
        /// 实际获取到的文本长度
        [DllImport("User32.dll", EntryPoint = "GetWindowText")]
        private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);

        // 给窗口发送消息
        /// 
        /// 给目标句柄发送消息
        /// 
        /// 目标句柄
        /// 消息内容
        /// 附加自定义参数1
        /// 附加自定义参数2
        /// 
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        /// 
        /// 给窗口发送消息,事件返回的数据通过Byte[]数组获得
        /// 
        /// 目标句柄
        /// 消息内容
        /// 附加自定义参数1
        /// 附加自定义参数2
        /// 
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);

        /// 
        /// 获取当前活动窗口(当前焦点窗口,键盘可操作的那种)
        /// 
        /// 返回窗口句柄
        [DllImport("user32.dll")]
        public static extern IntPtr GetActiveWindow();

        /// 
        /// 获取当前置顶窗口(窗口总在最前,类似于QQ那种)
        /// 
        /// 返回窗口句柄
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        /// 
        /// 通过窗口句柄获取线程ID(TID)和进程ID(PID)
        /// 
        /// 窗口句柄
        /// 返回进程ID
        /// 返回线程ID
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int PID);   //获取线程ID

        /// 
        /// 获取指定窗口(或控件)在屏幕中的位置信息 (左边界,上边界,右边界,下边界)
        /// 
        /// 窗口句柄
        /// LPRECT矩形结构的长指针,数据存储使用struct类型
        /// 获取成功返回非0值,失败返回0
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT_INFO lpRect);

        const int WM_GETTEXT = 0x000D;
        const int WM_GETTEXTLENGTH = 0x000E;
        const int WM_CLOSE = 0x10;

       /// 
        /// 矩形范围信息(结构体)
        /// 
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT_INFO
        {
            /// 
            /// 当前矩形范围的最左边界
            /// 
            public int Left;
            /// 
            /// 当前矩形的最上边界
            /// 
            public int Top;
            /// 
            /// 当前矩形的最右边界
            /// 
            public int Right;
            /// 
            /// 当前矩形的最下边界
            /// 
            public int Bottom;
        }

来源:参考百度词条和自己实际测试

你可能感兴趣的:(C#代码分享,c#,开发语言)