ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:82.63KB ,
资源ID:5751660      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5751660.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#操作窗口类句柄操作.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#操作窗口类句柄操作.docx

1、C#操作窗口类句柄操作C#写个类操作窗口(句柄操作)实现过程:过程一:找到当前鼠标位置的句柄您的使用2个WinAPI(俺喜欢自己封装下来用):View Code DllImport(user32.dll, EntryPoint = GetCursorPos) public static extern bool GetCursorPos(out Point pt); DllImport(user32.dll, EntryPoint = WindowFromPoint) public static extern IntPtr WindowFromPoint(Point pt); /鼠标位置的坐标

2、public static Point GetCursorPosPoint() Point p = new Point(); if (GetCursorPos(out p) return p; return default(Point); / / 找到句柄 / / 坐标 / public static IntPtr GetHandle(Point p) return WindowFromPoint(p); 过程二:改变窗口的Text您的使用1个WinAPI:View Code DllImport(user32.dll, EntryPoint = SendMessage) private sta

3、tic extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); / / 给窗口发送内容 / / 句柄 / 要发送的内容 public static void SetText(IntPtr hWnd, string lParam) SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, lParam); private const int WM_SETTEXT = 0x000C;通过这个方法就能改变Text的值了。思考:如果俺把这个窗口的句柄记录下来,下次不用鼠标获取,直

4、接就能改变值不蛮好的嘛。例如:我有个桌面系统老叫我输入用户名,密码。我记录用户名和密码的窗口句柄,然后改变他们的输入值,这样多省事。(只是举例,不考虑安全性)问题:你会告诉我,窗口句柄的每次重建会变的呀,咋办。回答:类名不变呀。过程三:您的准备一些工具吧,例如:句柄找类名呀,类名找句柄什么的等等,下面会用到一些WinAPIView Code DllImport(user32.dll, EntryPoint = FindWindow) private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);

5、 DllImport(user32.dll, EntryPoint = FindWindowEx) private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); DllImport(user32.dll, EntryPoint = GetParent) public static extern IntPtr GetParent(IntPtr hWnd); DllImport(user32.dll, CharSet

6、= CharSet.Auto) public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); / / 找到句柄 / / 类名 / public static IntPtr GetHandle(string IpClassName) return FindWindow(IpClassName, null); / / 子窗口句柄 / / 父窗口句柄 / 前一个同目录级同名窗口句柄 / 类名 / public static IntPtr GetChildHandle(IntP

7、tr hwndParent, IntPtr hwndChildAfter, string lpszClass) return FindWindowEx(hwndParent, hwndChildAfter, lpszClass, null); / / 全部子窗口句柄 / / 父窗口句柄 / 类名 / public static List GetChildHandles(IntPtr hwndParent, string className) List resultList = new List(); for (IntPtr hwndClient = GetChildHandle(hwndPar

8、ent, IntPtr.Zero, className); hwndClient != IntPtr.Zero; hwndClient = GetChildHandle(hwndParent, hwndClient, className) resultList.Add(hwndClient); return resultList; / / 找类名 / / 句柄 / public static string GetClassName(IntPtr hWnd) StringBuilder lpClassName = new StringBuilder(128); if (GetClassName(

9、hWnd, lpClassName, lpClassName.Capacity) = 0) throw new Exception(not found IntPtr!); return lpClassName.ToString(); 思考:遍历桌面上所有的窗口,然后找类名,然后改他的Text,扯淡嘛,相同的类名太多了,找毛呀。实现:不仅记录类名,而且记录类名在父窗口出现的位置,然后通过桌面一层层找下来,最后找到这个句柄。(虽然不是太准,但是一般的还是能解决了,如果你有什么好方式一起研究)。过程四:实现一个WinHWND的类,可以把他的规则,他的父窗口类名,以及在父窗口中同类名出现的顺序记录下来

10、,然后通过这些记录的信息还原句柄。View Code public class WinHWND public IntPtr HWND get; set; public string ClassName get; set; public WinHWND Parent get; set; public int InParentSequence get; set; private WinHWND() public WinHWND(IntPtr hWnd) this.HWND = hWnd; this.ClassName = GetClassName(); this.Parent = GetParen

11、t(); this.InParentSequence = GetInParentSequence(); private string GetClassName() return WinAPI.GetClassName(this.HWND); private WinHWND GetParent() if (WinAPI.GetParent(this.HWND) = null) throw new Exception(not found IntPtr!); if (WinAPI.GetParent(this.HWND) = IntPtr.Zero) return null; return new

12、WinHWND(WinAPI.GetParent(this.HWND); private int GetInParentSequence() IntPtr IntprtParent = this.Parent = null ? IntPtr.Zero : this.Parent.HWND; return WinAPI.GetChildHandles(IntprtParent, this.ClassName).IndexOf(this.HWND); public override string ToString() StringBuilder result = new StringBuilder

13、(); for (WinHWND winHandle = this; winHandle != null; winHandle = winHandle.Parent) result.Append(string.Format(0:1;, Escape(winHandle.ClassName), winHandle.InParentSequence.ToString(); if (winHandle.InParentSequence = -1) break; return result.ToString().TrimEnd(;); private static string GetBaseMark

14、(string sMark) string sMarks = sMark.Split(;); return sMarkssMarks.Length - 1.Split(:)0; private static string GetChildMarks(string sMark) string sMarks = sMark.Split(;); string sChildMarks = new stringsMarks.Length - 1; for (int i = 0; i sChildMarks.Length; i + ) sChildMarksi = sMarksi ; return sCh

15、ildMarks; /我一直觉得这段写很丑陋,谁能帮帮我改改 public static WinHWND GetWinHWND(string sMark) List baseHwnds = WinAPI.GetChildHandles(IntPtr.Zero, GetBaseMark(sMark); string sChildMarks = GetChildMarks(sMark); /由于主窗口在桌面出现所以很可能同名,所以要看看他的儿子和孙子.是不是都匹配 foreach (IntPtr baseHwnd in baseHwnds) IntPtr handle = baseHwnd; fo

16、r (int i = sChildMarks.Length - 1; i = 0; i-) string sChildMark = sChildMarksi.Split(:); try handle = WinAPI.GetChildHandles(handle, UnEscape(sChildMark0)int.Parse(sChildMark1); catch break; if (i = 0) return new WinHWND(handle); continue; return null; #region 转义 private static string Escape(string

17、arg) return arg.Replace(:, :).Replace(;,;); private static string UnEscape(string arg) return arg.Replace(:, :).Replace(;, ;); #endregion public static WinHWND GetWinHWND() return new WinHWND(WinAPI.GetHandle(WinAPI.GetCursorPosPoint(); 上全部代码,里面加了窗口的部分属性,扩展其他的属性,自己发挥吧,就是搞WinAPIView Code using System

18、;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Drawing;using System.Collections;namespace InformationCollectionDataFill public class WinAPI #region WinodwsAPI DllImport(user32.dll, EntryPoint = FindWindow) private static extern

19、 IntPtr FindWindow(string IpClassName, string IpWindowName); DllImport(user32.dll, EntryPoint = FindWindowEx) private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); DllImport(user32.dll, EntryPoint = SendMessage) private static exter

20、n int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); DllImport(user32.dll, EntryPoint = GetParent) public static extern IntPtr GetParent(IntPtr hWnd); DllImport(user32.dll, EntryPoint = GetCursorPos) public static extern bool GetCursorPos(out Point pt); DllImport(user32.dll, EntryP

21、oint = WindowFromPoint, CharSet = CharSet.Auto, ExactSpelling = true) public static extern IntPtr WindowFromPoint(Point pt); DllImport(user32.dll, CharSet = CharSet.Auto) public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); DllImport(user32.dll, CharSet = Cha

22、rSet.Auto) public static extern int GetWindowText(IntPtr hWnd, Out, MarshalAs(UnmanagedType.LPTStr) StringBuilder lpString, int nMaxCount); DllImport(user32.dll, CharSet = CharSet.Auto) public static extern int GetWindowRect(IntPtr hwnd, ref Rectangle rc); DllImport(user32.dll, CharSet = CharSet.Aut

23、o) public static extern int GetClientRect(IntPtr hwnd, ref Rectangle rc); DllImport(user32.dll, CharSet = CharSet.Auto) public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint); DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError = true, ExactSpel

24、ling = true) public static extern int ScreenToClient(IntPtr hWnd, ref Rectangle rect); #endregion #region 封装API方法 / / 找到句柄 / / 类名 / public static IntPtr GetHandle(string IpClassName) return FindWindow(IpClassName, null); / / 找到句柄 / / 坐标 / public static IntPtr GetHandle(Point p) return WindowFromPoin

25、t(p); /鼠标位置的坐标 public static Point GetCursorPosPoint() Point p = new Point(); if (GetCursorPos(out p) return p; return default(Point); / / 子窗口句柄 / / 父窗口句柄 / 前一个同目录级同名窗口句柄 / 类名 / public static IntPtr GetChildHandle(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass) return FindWindowEx(hwndPa

26、rent, hwndChildAfter, lpszClass, null); / / 全部子窗口句柄 / / 父窗口句柄 / 类名 / public static List GetChildHandles(IntPtr hwndParent, string className) List resultList = new List(); for (IntPtr hwndClient = GetChildHandle(hwndParent, IntPtr.Zero, className); hwndClient != IntPtr.Zero; hwndClient = GetChildHandle(hwndParent, hwndClient, className) resultList.Add(hwndClient); return resultList;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1