c#窗口操作-句柄操控全解

来源:互联网 发布:以太网是什么网络 编辑:程序博客网 时间:2024/06/08 03:33
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;using System.Runtime.InteropServices;namespace util{    public class 句柄操控    {        //使用方法        //控件函数.窗口拖动(this.button1,this);        //控件函数.控件拖动(this.button2);        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);         [DllImport("user32.dll")]         private static extern IntPtr WindowFromPoint(Point p);   //根据坐标点获取句柄         [DllImport("user32.dll")]         private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);  //获取窗口标题         [DllImport("user32.dll")]         private static extern void GetClassName(IntPtr hwnd,StringBuilder s,int nMaxCount);   //获取句柄类名         [DllImport("user32.dll")]         private static extern IntPtr GetParent(IntPtr hwnd);   //获取句柄父句柄         [DllImport("user32.dll")]         private static extern IntPtr SetParent(IntPtr hwnd, IntPtr cwnd);  //设置父句柄         [DllImport("user32.dll")]         private static extern bool GetWindowRect(IntPtr hwnd,ref RECT lpRECT);   //获取句柄矩形位置信息         [StructLayout(LayoutKind.Sequential)]         public struct 句柄信息结构体         {             public Rectangle 位置;             public string 密文显示;             public string 类名;             public List<IntPtr> 父窗句柄;             public List<string> 父窗标题;             public List<string> 父窗类名;         }        [StructLayout(LayoutKind.Sequential)]         public struct RECT         {             public int Left;             public int Top;             public int Right;             public int Bottom;         }        public static 句柄信息结构体 获取句柄信息(IntPtr 句柄)        {            句柄信息结构体 xinxi = new 句柄信息结构体();            xinxi.父窗句柄 = new List<IntPtr>();            xinxi.父窗标题 = new List<string>();            xinxi.父窗类名 = new List<string>();            RECT weizhi = new RECT();            GetWindowRect(句柄, ref weizhi);            xinxi.位置.X = weizhi.Left;            xinxi.位置.Y = weizhi.Top;            xinxi.位置.Width = weizhi.Right - weizhi.Left;            xinxi.位置.Height = weizhi.Bottom - weizhi.Top;            StringBuilder s = new StringBuilder(512);            GetWindowText(句柄, s, s.Capacity);            xinxi.密文显示 = s.ToString();            GetClassName(句柄, s, s.Capacity);            xinxi.类名 = s.ToString();            IntPtr f1 = GetParent(句柄);            IntPtr h;            while (f1 != IntPtr.Zero)            {                xinxi.父窗句柄.Add(f1);                StringBuilder s1 = new StringBuilder(512);                GetWindowText(f1, s1, s1.Capacity);                xinxi.父窗标题.Add(s1.ToString());                GetClassName(f1, s1, s1.Capacity);                xinxi.父窗类名.Add(s1.ToString());                h = f1;                f1 = GetParent(h);            }            return xinxi;        }        [DllImport("user32.dll")]        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        //hwndparent为0表示桌面为父,hwndchildafter为null表示从第一个开始,lpwindowname为null表示步匹配该项        [DllImport("user32.dll")]        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);        //查找控件句柄        public static IntPtr findview(IntPtr parent,string classname,string text)        {            IntPtr hwndtemp = FindWindowEx(parent, IntPtr.Zero, classname, text);            if (hwndtemp==IntPtr.Zero)            {                List<IntPtr> allchild = findallchild(parent);                for (int i = 0; i < allchild.Count;i++ )                {                    句柄信息结构体 ju = 获取句柄信息(allchild[i]);                    if (ju.类名==classname && ju.密文显示==text)                    {                        return allchild[i];                    }                }            }            return hwndtemp;        }        //查找一级子控件句柄        public static List<IntPtr> findchild(IntPtr parent)        {            List<IntPtr> allchild = new List<IntPtr>();            IntPtr hwnd = FindWindowEx(parent, IntPtr.Zero, null, null);            while (hwnd != IntPtr.Zero)            {                allchild.Add(hwnd);                hwnd = FindWindowEx(parent, hwnd, null, null);            }            return allchild;        }        //查找所有子控件-广度遍历        public static List<IntPtr> findallchild(IntPtr parent)        {            List<IntPtr> allchild = new List<IntPtr>();            allchild.Add(parent);   //第一个添加父句柄,最后再删除            for (int i = 0; i < allchild.Count;i++ )            {                IntPtr patenttemp = allchild[i];                IntPtr hwnd = FindWindowEx(patenttemp, IntPtr.Zero, null, null);                while (hwnd != IntPtr.Zero)                {                    allchild.Add(hwnd);                    hwnd = FindWindowEx(patenttemp, hwnd, null, null);                }            }            allchild.RemoveAt(0);            return allchild;        }        //查找窗口        public static IntPtr findform(string classname, string windowname)        {            IntPtr hwnd = FindWindow(classname, windowname);            return hwnd;        }    }}
原创粉丝点击