![C操作窗口类(句柄操作)_第1页](http://file4.renrendoc.com/view/c3d3594cfac8a27e3c32454fde5f56bf/c3d3594cfac8a27e3c32454fde5f56bf1.gif)
![C操作窗口类(句柄操作)_第2页](http://file4.renrendoc.com/view/c3d3594cfac8a27e3c32454fde5f56bf/c3d3594cfac8a27e3c32454fde5f56bf2.gif)
![C操作窗口类(句柄操作)_第3页](http://file4.renrendoc.com/view/c3d3594cfac8a27e3c32454fde5f56bf/c3d3594cfac8a27e3c32454fde5f56bf3.gif)
![C操作窗口类(句柄操作)_第4页](http://file4.renrendoc.com/view/c3d3594cfac8a27e3c32454fde5f56bf/c3d3594cfac8a27e3c32454fde5f56bf4.gif)
![C操作窗口类(句柄操作)_第5页](http://file4.renrendoc.com/view/c3d3594cfac8a27e3c32454fde5f56bf/c3d3594cfac8a27e3c32454fde5f56bf5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#写个类操作窗口(句柄操作)实现过程:过程一:找到当前鼠标位置的句柄您的使用2个WinAPI(俺喜欢自己封装下来用):田ViewCodeDllImport(user32.dll,EntryPoint=GetCursorPos)publicstaticexternboolGetCursorPos(outPointpt);DllImport(user32.dll,EntryPoint=WindowFromPoint)publicstaticexternIntPtrWindowFromPoint(Pointpt);/鼠标位置的坐标publicstaticPointGetCursorPosPoint
2、()Pointp=newPoint();if(GetCursorPos(outp)returnp;returndefault(Point);/找到句柄/坐标/publicstaticIntPtrGetHandle(Pointp)returnWindowFromPoint(p);过程二:改变窗口的Text您的使用1个WinAPI:田ViewCodeDllImport(user32.dll,EntryPoint=SendMessage)lParam);privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,string/lP
3、aram);/给窗口发送内容/句柄/要发送的内容publicstaticvoidSetText(IntPtrhWnd,stringlParam)SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);privateconstintWM_SETTEXT=OxOOOC;通过这个方法就能改变Text的值了。思考:如果俺把这个窗口的句柄记录下来,下次不用鼠标获取,直接就能改变值不蛮好的嘛。例如:我有个桌面系统老叫我输入用户名,密码。我记录用户名和密码的窗口句柄,然后改变他们的输入值,这样多省事。(只是举例,不考虑安全性)问题:你会告诉我,窗口句柄的每次重建会变的
4、呀,咋办。回答:类名不变呀。过程三:您的准备一些工具吧,例如:句柄找类名呀,类名找句柄什么的等等,下面会用到一些WinAPI国ViewCodeDllImport(user32.dll,EntryPoint=FindWindow)privatestaticexternIntPtrFindWindow(stringIpClassName,stringIpWindowName);DllImport(user32.dll,EntryPoint=FindWindowEx)privatestaticexternIntPtrFindWindowEx(IntPtrhwndParent,IntPtrhwndCh
5、ildAfter,stringlpszClass,stringlpszWindow);DllImport(user32.dll,EntryPoint=GetParent)publicstaticexternIntPtrGetParent(IntPtrhWnd);nMaxCounDllImport(user32.dll,CharSet=CharSet.Auto)nMaxCount);publicstaticexternintGetClassName(IntPtrhWnd,StringBuilderlpClassName,intt);/找到句柄/类名/publicstaticIntPtrGetHa
6、ndle(stringIpClassName)returnFindWindow(IpClassName,null);/子窗口句柄/父窗口句柄/前一个同目录级同名窗口句柄/类名/publicstaticIntPtrGetChildHandle(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass)returnFindWindowEx(hwndParent,hwndChildAfter,lpszClass,null);/全部子窗口句柄/父窗口句柄/类名/publicstaticListGetChildHandles(IntPtrhwndPare
7、nt,stringclassName)ListresultList=newList();for(IntPtrhwndClient=GetChildHandle(hwndParent,IntPtr.Zero,className);hwndClient=IntPtr.Zero;hwndClient=GetChildHandle(hwndParent,hwndClient,className)resultList.Add(hwndClient);returnresultList;/找类名/句柄/publicstaticstringGetClassName(IntPtrhWnd)StringBuild
8、erlpClassName=newStringBuilder(128);if(GetClassName(hWnd,lpClassName,lpClassName.Capacity)=0)thrownewException(notfoundIntPtr!);returnlpClassName.ToString();思考:遍历桌面上所有的窗口,然后找类名,然后改他的Text,扯淡嘛,相同的类名太多了,找毛呀。实现:不仅记录类名,而且记录类名在父窗口出现的位置,然后通过桌面一层层找下来,最后找到这个句柄。(虽然不是太准,但是一般的还是能解决了,如果你有什么好方式一起研究)。过程四:实现一个WinHW
9、ND的类,可以把他的规则,他的父窗口类名,以及在父窗口中同类名出现的顺序记录下来,然后通过这些记录的信息还原句柄。田ViewCodepublicclassWinHWNDTOC o 1-5 h zpublicIntPtrHWNDget;set;publicstringClassNameget;set;publicWinHWNDParentget;set;publicintInParentSequenceget;set;privateWinHWND()publicWinHWND(IntPtrhWnd)this.HWND=hWnd;this.ClassName=GetClassName();this
10、.Parent=GetParent();this.InParentSequence=GetInParentSequence();privatestringGetClassName()returnWinAPI.GetClassName(this.HWND);privateWinHWNDGetParent()if(WinAPI.GetParent(this.HWND)=null)thrownewException(notfoundIntPtr!);if(WinAPI.GetParent(this.HWND)=IntPtr.Zero)returnnull;returnnewWinHWND(WinAP
11、I.GetParent(this.HWND);privateintGetInParentSequence()IntPtrIntprtParent=this.Parent=null?IntPtr.Zero:this.Parent.HWND;returnWinAPI.GetChildHandles(IntprtParent,this.ClassName).IndexOf(this.HWND);publicoverridestringToString()StringBuilderresult=newStringBuilder();for(WinHWNDwinHandle=this;winHandle
12、!=null;winHandle=winHandle.Parent)result.Append(string.Format(0:1;,Escape(winHandle.ClassName),winHandle.InParentSequence.ToString();if(winHandle.InParentSequence=-1)break;returnresult.ToString().TrimEnd(;);privatestaticstringGetBaseMark(stringsMark)stringsMarks=sMark.Split(;);returnsMarkssMarks.Len
13、gth-1.Split(:)0;privatestaticstringGetChildMarks(stringsMark)stringsMarks=sMark.Split(;);stringsChildMarks=newstringsMarks.Length-1;for(inti=0;isChildMarks.Length;i+)sChildMarksi=sMarksi;returnsChildMarks;/我一直觉得这段写很丑陋,谁能帮帮我改改publicstaticWinHWNDGetWinHWND(stringsMark)ListbaseHwnds=WinAPI.GetChildHand
14、les(IntPtr.Zero,GetBaseMark(sMark);stringsChildMarks=GetChildMarks(sMark);/由于主窗口在桌面出现所以很可能同名,所以要看看他的儿子和孙子.是不是都匹配foreach(IntPtrbaseHwndinbaseHwnds)IntPtrhandle=baseHwnd;for(inti=sChildMarks.Length-1;i=0;i-)stringsChildMark=sChildMarksi.Split(:);tryhandle=WinAPI.GetChildHandles(handle,UnEscape(sChildM
15、ark0)int.Parse(sChildMark1);catchbreak;if(i=0)returnnewWinHWND(handle);continue;returnnull;#region转义privatestaticstringEscape(stringarg)returnarg.Replace(:,:).Replace(;,;);privatestaticstringUnEscape(stringarg)returnarg.Replace(:,:).Replace(;,;);#endregionpublicstaticWinHWNDGetWinHWND()returnnewWinH
16、WND(WinAPI.GetHandle(WinAPI.GetCursorPosPoint();上全部代码,里面加了窗口的部分属性,扩展其他的属性,自己发挥吧,就是搞WinAPI田ViewCodeusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Drawing;usingSystem.Collections;namespaceInformationCollectionDataFillpublic
17、classWinAPI#regionWinodwsAPIDllImport(user32.dll,EntryPoint=FindWindow)privatestaticexternIntPtrFindWindow(stringIpClassName,DllImport(user32.dll,EntryPoint=FindWindowEx)privatestaticexternIntPtrFindWindowEx(IntPtrhwndParentlpszClass,stringlpszWindow);DllImport(user32.dll,EntryPoint=SendMessage)priv
18、atestaticexternintSendMessage(IntPtrhWnd,intMsg,DllImport(user32.dll,EntryPoint=GetParent)publicstaticexternIntPtrGetParent(IntPtrhWnd);stringIpWindowName);IntPtrhwndChildAfter,stringIntPtrwParam,stringlParam);Out,MarshalAs(UnmanagedType.LPTStr)refRectanglerc);refRectanglerc);publicstaticexternintMo
19、veWindow(IntPtrhwnd,intx,inty,intnWidth,intnHeight,boolOut,MarshalAs(UnmanagedType.LPTStr)refRectanglerc);refRectanglerc);publicstaticexternintMoveWindow(IntPtrhwnd,intx,inty,intnWidth,intnHeight,boolDllImport(user32.dll,EntryPoint=GetCursorPos)publicstaticexternboolGetCursorPos(outPointpt);DllImpor
20、t(user32.dll,EntryPoint=WindowFromPoint,CharSet=CharSet.Auto,ExactSpelling=true)publicstaticexternIntPtrWindowFromPoint(Pointpt);DllImport(user32.dll,CharSet=CharSet.Auto)publicstaticexternintGetClassName(IntPtrhWnd,StringBuilderlpClassName,intnMaxCount);DllImport(user32.dll,CharSet=CharSet.Auto)pub
21、licstaticexternintGetWindowText(IntPtrhWnd,StringBuilderlpString,intnMaxCount);DllImport(user32.dll,CharSet=CharSet.Auto)publicstaticexternintGetWindowRect(IntPtrhwnd,DllImport(user32.dll,CharSet=CharSet.Auto)publicstaticexternintGetClientRect(IntPtrhwnd,DllImport(user32.dll,CharSet=CharSet.Auto)bRe
22、paint);DllImport(user32.dll,CharSet=CharSet.Auto,SetLastError=true,ExactSpelling=true)publicstaticexternintScreenToClient(IntPtrhWnd,refRectanglerect);#endregion#region封装API方法/找到句柄/类名/publicstaticIntPtrGetHandle(stringIpClassName)returnFindWindow(IpClassName,null);/找到句柄/坐标/publicstaticIntPtrGetHandl
23、e(Pointp)returnWindowFromPoint(p);/鼠标位置的坐标publicstaticPointGetCursorPosPoint()Pointp=newPoint();if(GetCursorPos(outp)returnp;returndefault(Point);/子窗口句柄/父窗口句柄/前一个同目录级同名窗口句柄/类名/publicstaticIntPtrGetChildHandle(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass)returnFindWindowEx(hwndParent,hwndChi
24、ldAfter,lpszClass,null);/全部子窗口句柄/父窗口句柄/类名/publicstaticListGetChildHandles(IntPtrhwndParent,stringclassName)ListresultList=newList();for(IntPtrhwndClient=GetChildHandle(hwndParent,IntPtr.Zero,className);hwndClient=IntPtr.Zero;hwndClient=GetChildHandle(hwndParent,hwndClient,className)resultList.Add(hw
25、ndClient);returnresultList;/给窗口发送内容/句柄/要发送的内容publicstaticvoidSetText(IntPtrhWnd,stringlParam)SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);privateconstintWM_SETTEXT=0 x000C;/获得窗口内容或标题/句柄/publicstaticstringGetText(IntPtrhWnd)StringBuilderresult=newStringBuilder(128);GetWindowText(hWnd,result,result
26、.Capacity);returnresult.ToString();/找类名/句柄/publicstaticstringGetClassName(IntPtrhWnd)StringBuilderlpClassName=newStringBuilder(128);if(GetClassName(hWnd,lpClassName,lpClassName.Capacity)=0)thrownewException(notfoundIntPtr!);returnlpClassName.ToString();/窗口在屏幕位置/句柄/publicstaticRectangleGetWindowRect(
27、IntPtrhWnd)Rectangleresult=default(Rectangle);GetWindowRect(hWnd,refresult);returnresult;/窗口相对屏幕位置转换成父窗口位置/publicstaticRectangleScreenToClient(IntPtrhWnd,Rectanglerect)Rectangleresult=rect;ScreenToClient(hWnd,refresult);returnresult;/窗口大小/publicstaticRectangleGetClientRect(IntPtrRectangleresult=defa
28、ult(Rectangle);GetClientRect(hWnd,refresult);returnresult;#endregionpublicclassWinHWNDpublicpublicpublicpublicIntPtrHWNDget;set;stringClassNameget;set;WinHWNDParentget;set;intInParentSequenceget;set;privateprivateprivateprivateprivateprivateRectanglecurrentRect;stringText;intLeft;intTop;intWidth;int
29、Height;publicstringTexthWnd)getreturn_Text=default(string)?WinAPI.GetText(this.HWND):_Text;set_Text=value;WinAPI.SetText(this.HWND,value);publicintLeftgetreturn_Left=default(int)?_currentRect.Left:_Left;settrue);_Left=value;true);WinAPI.MoveWindow(this.HWND,value,this.Top,this.Width,this.Height,publ
30、icintTopgetcurrentRect.Top:_Top;return_Top=default(int)currentRect.Top:_Top;setset_Top=value;WinAPI.MoveWindow(this.HWND,this.Left,_Top=value;WinAPI.MoveWindow(this.HWND,this.Left,value,this.Width,this.Height,true);publicgetpublicgetsetintWidthreturn_Width=default(int)currentRect.Width:_Width;public
31、get_Width=value;WinAPI.MoveWindow(this.HWND,this.Left,this.Top,value,this.Height,true);intHeightreturn_Height=default(int)currentRect.Height:_Height;settrue);_Height=value;true);WinAPI.MoveWindow(this.HWND,this.Left,this.Top,this.Width,value,privateWinHWND()publicWinHWND(IntPtrhWnd)this.HWND=hWnd;th
32、is.ClassName=GetClassName();this.Parent=GetParent();this.InParentSequence=GetInParentSequence();this._currentRect=GetRect();privatestringGetClassName()returnWinAPI.GetClassName(this.HWND);privateWinHWNDGetParent()if(WinAPI.GetParent(this.HWND)=null)thrownewException(notfoundIntPtr!);if(WinAPI.GetPar
33、ent(this.HWND)=IntPtr.Zero)returnnull;returnnewWinHWND(WinAPI.GetParent(this.HWND);privateintGetInParentSequence()IntPtrIntprtParent=this.Parent=null?IntPtr.Zero:this.Parent.HWND;returnWinAPI.GetChildHandles(IntprtParent,this.ClassName).IndexOf(this.HWND);privateRectangleGetRect()if(this.Parent=null
34、)returndefault(Rectangle);RectangleclientSize=WinAPI.GetClientRect(this.HWND);RectangleclientPoint=WinAPI.ScreenToClient(this.Parent.HWND,WinAPI.GetWindowRect(this.HWND);returnnewRectangle(clientPoint.X,clientPoint.Y,clientSize.Width,clientSize.Height);publicstaticWinHWNDGetWinHWND()returnnewWinHWND
35、(WinAPI.GetHandle(WinAPI.GetCursorPosPoint();publicoverridestringToString()StringBuilderresult=newStringBuilder();for(WinHWNDwinHandle=this;winHandle!=null;winHandle=winHandle.Parent)result.Append(string.Format(0:1;,Escape(winHandle.ClassName),winHandle.InParentSequence.ToString();if(winHandle.InParentSequence=-1)break;returnresult.ToString().TrimEnd(;);privatestaticstringGetBaseMark(stringsMark)stri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年湖北省武汉市武昌区南湖中学初三实验A班小题专项训练2含解析
- 2024-2025学年菏泽市重点中学初三开学考语文试题含解析
- 2024年科级干部轮训心得体会(二篇)
- 2024年办公用房租赁合同参考样本(二篇)
- 市场管理服务相关行业投资方案范本
- 9 心中的“110”( 教学设计)-部编版道德与法治三年级上册
- 2024年保洁员工作总结例文(8篇)
- 2024年一般员工述职报告参考范本(五篇)
- Unit 2 Different Family Part A 第一课时-三年级英语上学期同步备课教学设计(人教PEP版2024新)
- 2024年学校师德师风工作总结参考范文(3篇)
- (高清版)WST 433-2023 静脉治疗护理技术操作标准
- 2024年无线AP市场需求分析报告
- 游船安全技能培训
- 艺术部主任述职报告
- 车辆消防安全知识完整版
- 农场类游戏开发方案
- 公安食药环培训课件
- 化学品的存储与储存要求
- 高二第一学期主题班会
- 铁路职工健康应知课件
- 焊接工艺指导与实际案例分析
评论
0/150
提交评论