版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、C#寫個類操作窗口(句柄操作)實現(xiàn)過程:過程一:找到當(dāng)前鼠標(biāo)位置的句柄您的使用2個WinAPI(俺喜歡自己封裝下來用):田ViewCodeDllImport(user32.dll,EntryPoint=GetCursorPos)publicstaticexternboolGetCursorPos(outPointpt);DllImport(user32.dll,EntryPoint=WindowFromPoint)publicstaticexternIntPtrWindowFromPoint(Pointpt);/鼠標(biāo)位置的坐標(biāo)publicstaticPointGetCursorPosPoint
2、()Pointp=newPoint();if(GetCursorPos(outp)returnp;returndefault(Point);/找到句柄/坐標(biāo)/publicstaticIntPtrGetHandle(Pointp)returnWindowFromPoint(p);過程二:改變窗口的Text您的使用1個WinAPI:田ViewCodeDllImport(user32.dll,EntryPoint=SendMessage)lParam);privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,string/lP
3、aram);/給窗口發(fā)送內(nèi)容/句柄/要發(fā)送的內(nèi)容publicstaticvoidSetText(IntPtrhWnd,stringlParam)SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);privateconstintWM_SETTEXT=OxOOOC;通過這個方法就能改變Text的值了。思考:如果俺把這個窗口的句柄記錄下來,下次不用鼠標(biāo)獲取,直接就能改變值不蠻好的嘛。例如:我有個桌面系統(tǒng)老叫我輸入用戶名,密碼。我記錄用戶名和密碼的窗口句柄,然后改變他們的輸入值,這樣多省事。(只是舉例,不考慮安全性)問題:你會告訴我,窗口句柄的每次重建會變的
4、呀,咋辦?;卮穑侯惷蛔冄?。過程三:您的準(zhǔn)備一些工具吧,例如:句柄找類名呀,類名找句柄什么的等等,下面會用到一些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,扯淡嘛,相同的類名太多了,找毛呀。實現(xiàn):不僅記錄類名,而且記錄類名在父窗口出現(xiàn)的位置,然后通過桌面一層層找下來,最后找到這個句柄。(雖然不是太準(zhǔn),但是一般的還是能解決了,如果你有什么好方式一起研究)。過程四:實現(xiàn)一個WinHW
9、ND的類,可以把他的規(guī)則,他的父窗口類名,以及在父窗口中同類名出現(xiàn)的順序記錄下來,然后通過這些記錄的信息還原句柄。田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);/由于主窗口在桌面出現(xiàn)所以很可能同名,所以要看看他的兒子和孫子.是不是都匹配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轉(zhuǎn)義privatestaticstringEscape(stringarg)returnarg.Replace(:,:).Replace(;,;);privatestaticstringUnEscape(stringarg)returnarg.Replace(:,:).Replace(;,;);#endregionpublicstaticWinHWNDGetWinHWND()returnnewWinH
16、WND(WinAPI.GetHandle(WinAPI.GetCursorPosPoint();上全部代碼,里面加了窗口的部分屬性,擴展其他的屬性,自己發(fā)揮吧,就是搞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);/找到句柄/坐標(biāo)/publicstaticIntPtrGetHandl
23、e(Pointp)returnWindowFromPoint(p);/鼠標(biāo)位置的坐標(biāo)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;/給窗口發(fā)送內(nèi)容/句柄/要發(fā)送的內(nèi)容publicstaticvoidSetText(IntPtrhWnd,stringlParam)SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);privateconstintWM_SETTEXT=0 x000C;/獲得窗口內(nèi)容或標(biāo)題/句柄/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;/窗口相對屏幕位置轉(zhuǎn)換成父窗口位置/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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 管理工作心得體會感悟(6篇)
- 誠信國旗下講話稿范本7篇
- 無害化處理廢棄電子線路板項目可行性研究報告
- 開展校園觀光車調(diào)查問卷
- 商品房服務(wù)合同
- 上海工程建設(shè)合同范本
- 高中班主任教育工作期末個人總結(jié)范文5篇
- 商業(yè)綜合體租賃托管
- 團隊合作分紅合同
- 知識產(chǎn)權(quán)代理爭議調(diào)解協(xié)議
- 經(jīng)導(dǎo)管主動脈瓣置換術(shù)(TAVR)患者的麻醉管理
- 運籌學(xué)智慧樹知到答案2024年哈爾濱工程大學(xué)
- 行政執(zhí)法證專業(yè)法律知識考試題庫含答案(公路路政)
- 《人行自動門安全要求》標(biāo)準(zhǔn)
- 廣鐵集團校園招聘機考題庫
- 第一章、總體概述:施工組織總體設(shè)想、工程概述、方案針對性及施工標(biāo)段劃分
- 2024-2030年中國語言服務(wù)行業(yè)發(fā)展規(guī)劃與未來前景展望研究報告
- 2024-2030年白玉蝸牛養(yǎng)殖行業(yè)市場發(fā)展現(xiàn)狀及發(fā)展前景與投資機會研究報告
- HGT 2902-2024《模塑用聚四氟乙烯樹脂》
- 2024 年上海市普通高中學(xué)業(yè)水平等級性考試 物理 試卷
- 國家開放大學(xué)??啤斗ɡ韺W(xué)》(第三版教材)形成性考核試題及答案
評論
0/150
提交評論