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; } }}
阅读全文
2 0
- c#窗口操作-句柄操控全解
- C#操作窗口句柄
- C# 窗口、控件句柄操作
- C#窗口句柄问题
- c#窗口句柄
- 收集的部分C#中窗口句柄及操作
- 收集的部分C#中窗口句柄及操作
- c#获得活动窗口句柄
- c++操作当前窗口句柄
- c#注册表操作全解
- c# 窗口句柄有关的常见问题
- c# 窗口句柄有关的常见问题
- c# 窗口句柄有关的常见问题
- C#获取进程的主窗口句柄
- c# 窗口句柄有关的常见问题(转载)
- C#遍历进程获取主窗口句柄
- c# 窗口句柄问题(转)
- C# 窗口句柄有关的常见问题
- Letters Gym
- 一些大佬的博客地址
- 打印目标字符串前面的内容
- python多线程模块threading
- Mr. Kitayuta's Colorful Graph
- c#窗口操作-句柄操控全解
- Maven常用命令以及Nexus私服配置
- 获取系统传感器
- POJ2155 Matrix <树套树/二维树状数组>
- CentOS6 单节点安装Redis Sentinel
- [Usaco2005 nov]Asteroids 穿越小行星群
- 【SSLGZ 2673】2017年8月9日提高组T1 水题
- Oracle 数据库的连接方式实现方法
- MVG读书笔记——射影变换的校正(二)