版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C#寫個(gè)類操作窗口(句柄操作)實(shí)現(xiàn)過(guò)程:過(guò)程一:找到當(dāng)前鼠標(biāo)位置的句柄您的使用2個(gè)WinAPI(俺喜歡自己封裝下來(lái)用):ViewCode[DllImport(
publicstaticextern,boolEntryPoint= )]GetCUrsorPos(outPointpt);[DllImport(
publicstaticextern,EntryPoint=IntPtrWindowFromPoint(Pointpt);)]//鼠標(biāo)位置的坐標(biāo)publicstaticPointGetCUrsorPosPoint(){PointP=newPoint();if(GetCursorPos(outp)){returnp;}returndefault(Point);}///<summary>///找到句柄///<∕summary>/// 坐標(biāo)</param>///<returnsx/returns>publicstaticIntPtrGetHandle(PointP)
{returnWindowFromPoint(p);
}過(guò)程二:改變窗口的Text您的使用1個(gè)WinAPI:田日V■ C7ViewCode[DllImport( ,EntryPoint= )]privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrWParam,string///<summary>///給窗口發(fā)送內(nèi)容///</summary>lParam);///句柄</param>/// 要發(fā)送的內(nèi)容<∕param>publicstaticvoidSetText(IntPtrhWnd,stringlParam){SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);}privateconstintWM_SETTEXT=0x000C;通過(guò)這個(gè)方法就能改變Text的值了。思考:如果俺把這個(gè)窗口的句柄記錄下來(lái),下次不用鼠標(biāo)獲取,直接就能改變值不蠻好的嘛。例如:我有個(gè)桌面系統(tǒng)老叫我輸入用戶名,密碼。我記錄用戶名和密碼的窗口句柄,然后改變他們的輸入值,這樣多省事。(只是舉例,不考慮安全性)問(wèn)題:你會(huì)告訴我,窗口句柄的每次重建會(huì)變的呀,咋辦?;卮穑侯惷蛔冄?。過(guò)程三:您的準(zhǔn)備一些工具吧,例如:句柄找類名呀,類名找句柄什么的等等,下面會(huì)用到一些WinAPIViewCode[DllImport( ,EntryPoint= )]privatestaticexternIntPtrFindWindow(stringIpClassName,stringIpWindowName);[DllImport( ,EntryPoint= )]privatestaticexternIntPtrFindWindoWEx(IntPtrhwndParent,IntPtrhwndChildAfter,stringIpszClass,stringIpszWindow);[DllImport(publicstaticextern,EntryPoint=)]IntPtrGetParent(IntPtrhWnd);t);[DllImport(publicstaticextern,CharSet=CharSet.Auto)]intGetClaSSName(IntPtrhWnd,StringBuilderlpClassName,intnMaxCoun///<summary>〃/找到句柄///</summary>/// 類名</param>///<returns></returns>publicstaticIntPtrGetHandle(stringIpClassName){returnFindWindow(IPCIaSSName,null);}///<summary>///子窗口句柄///</summary>///父窗口句柄</param>///前一個(gè)同目錄級(jí)同名窗口句柄<∕param>/// 類名<∕param>///<returnsx/returns>publicstaticIntPtrGetChildHandle(IntPtrhwndParent,IntPtrhwndChildAfter,stringIpszClass){returnFindWindowEx(hwndParent,hwndChildAfter,lpszClass,null);}///<summary>///全部子窗口句柄///</summary>/// 父窗口句柄</param>/// 類名</param>///<returns></returns>publicstaticList<IntPtr>GetChildHandleS(IntPtrhwndParent,stringClaSSName){List<IntPtr>resultList=newList<IntPtr>();for(IntPtrhwndClient=GetChildHandle(hwndParent,IntPtr.Zero,className);hwndClient=IntPtr.Zero;hwndClient=GetChildHandle(hwndParent,hwndClient,className)){resultList.Add(hwndClient);}returnresultList;}///<summary>///找類名///</summary>/// 句柄</param>///<returnsx/returns>publicstaticstringGetClassName(IntPtrhWnd){StringBuilderlpClassName=newStringBuilder(128);if(GetClassName(hWnd,IpClassName,IpClassName.Capacity)==0){thrownewException( );}returnlpClassName.ToString();}思考:遍歷桌面上所有的窗口,然后找類名,然后改他的Text,扯淡嘛,相同的類名太多了,找毛呀。實(shí)現(xiàn):不僅記錄類名,而且記錄類名在父窗口出現(xiàn)的位置,然后通過(guò)桌面一層層找下來(lái),最后找到這個(gè)句柄。(雖然不是太準(zhǔn),但是一般的還是能解決了,如果你有什么好方式一起研究)。過(guò)程四:實(shí)現(xiàn)一個(gè)WinHWND的類,可以把他的規(guī)則,他的父窗口類名,以及在父窗口中同類名出現(xiàn)的順序記錄下來(lái),然后通過(guò)這些記錄的信息還原句柄。[+IEIt7'CJViewCodepublicclassWinHWND{publicIntPtrHWND{get;set;}publicstringClassName{get;set;}publicWinHWNDParent{get;set;}publicintInParentSequence{get;set;}privateWinHWND(){}publicWinHWND(IntPtrhWnd){this.HWND=hWnd;this.ClassName=GetClaSSName();this.Parent=GetParent();this.InParentSequence=GetInParentSeqUenCe();}privatestringGetClassName(){returnWinAPI.GetClassName(this.HWND);}privateWinHWNDGetParent(){if(WinAPI.GetParent(this.HWND)==null){thrownewException( );}if(WinAPI.GetParent(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);}publicoverridestringToString(){StringBuilderresult=newStringBUilder();for(WinHWNDwinHandle=this;winHandle!=null;winHandle=WinHandle.Parent){result.Append(String.Format( ,Escape(WinHandle.ClassName),WinHandle.InParentSequence.ToString()));if(WinHandle.InParentSequence==-1)break;}returnresult.ToString().TrimEnd(';');}privatestaticstringGetBaSeMark(StringSMark){string[]sMarks=sMark.Split(';');returnSMarkS[sMarks.Length-1].Split(':')[0];}privatestaticstring[]GetChildMarkS(StringsMark){string[]sMarks=sMark.Split(';');string[]SChildMarks=newstring[sMarks.Length-1];for(inti=0;i<SChildMarkS.Length;i++){SChildMarks[i]=sMarks[i];}returnSChildMarks;}//我一直覺(jué)得這段寫很丑陋,誰(shuí)能幫幫我改改publicstaticWinHWNDGetWinHWND(StringSMark){List<IntPtr>baseHwnds=WinAPI.GetChildHandles(IntPtr.Zero,GetBaseMark(SMark));string[]SChildMarkS=GetChildMarkS(SMark);//由于主窗口在桌面出現(xiàn)所以很可能同名,所以要看看他的兒子和孫子 ...是不是都匹配foreach(IntPtrbaseHwndinbaseHwnds){IntPtrhandle=baseHwnd;for(inti=SChildMarkS.Length-1;i>=0;i--){string[]SChildMark=SChildMarkS[i].Split(':');try{handle=WinAPI.GetChildHandles(handle,UnEScape(SChildMark[0]))[int.Parse(sChildMark[1])];}catch{break;}if(i==0)returnnewWinHWND(handle);}continue;}returnnull;}#region轉(zhuǎn)義privatestaticstringEscape(Stringarg){returnarg.Replace(, ).Replace(, );}privatestaticstringUnEscape(Stringarg){returnarg.Replace(,).Replace(, );}#endregionpublicstaticWinHWNDGetWinHWND(){returnnewWinHWND(WinAPI.GetHandle(WinAPI.GetCursorPosPoint()));}}上全部代碼,里面加了窗口的部分屬性,擴(kuò)展其他的屬性,自己發(fā)揮吧,就是搞WinAPIViewCodeusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSyStem.Text;usingSyStem.Runtime.InteropServices;usingSyStem.Drawing;usingSyStem.Collections;namespaceInformationCollectionDataFill{publicclassWinAPI{#regionWinodwsAPI[DllImport(privatestaticextern,EntryPoint=)]IntPtrFindWindow(stringIpClaSSName,stringIpWindowName);[DllImport( ,EntryPoint= )]privatestaticexternIntPtrFindWindOWEx(IntPtrhwndParentlpszClass,stringlpszWindow);[DllImport( , EntryPoint= )]privatestaticexternintSendMessage(IntPtrhWnd,intMsg,[DllImport( , EntryPoint= )]publicstatic extern IntPtrGetParent(IntPtr hWnd);IntPtrhwndChildAfter,stringIntPtrwParam,stringlParam);[DllImport(
publicstaticextern[DllImport(
ing=true)]publicstaticextern[DllImport(publicstaticextern
t);[DllImport(
publicstaticextern
StringBuilderIPString,int[DllImport(
publicstaticextern[DllImport(
publicstaticextern[DllImport(
publicstaticextern
bRepaint);,EntryPoint= )]boolGetCUrSorPos(outPointpt);,EntryPoint= ,CharSet=CharSet.Auto,ExactSpellIntPtrWindowFromPoint(Pointpt);,CharSet=CharSet.Auto)]
intGetCIaSSName(IntPtrhWnd,StringBuilderlpClassName,intnMaxCoun,CharSet=CharSet.Auto)]
intGetWindOwTeXt(IntPtrhWnd,[Out,MarShaIAS(UnmanagedTyPe.LPTStr)]
nMaxCount);,CharSet=CharSet.Auto)]
intGetWindOwReCt(IntPtrhwnd,refRectanglerc);,CharSet=CharSet.Auto)]
intGetClientRect(IntPtrhwnd,refRectanglerc);,CharSet=CharSet.Auto)]
intMOveWindow(IntPtrhwnd,intx,inty,intnWidth,intnHeight,bool[DllImport(publicstaticextern#endregion,CharSet=CharSet.Auto,SetLastError=true,ExactSpelling=true)]
intScreenToClient(IntPtrhWnd,refRectanglerect);#region封裝API方法///<summary>〃/找到句柄///</summary>/// 類名</param>///<returns></returns>publicstaticIntPtrGetHandle(stringIpCIassName){returnFindWindow(IpCIassName,null);}///<summary>///找到句柄///</summary>/// 坐標(biāo)</param>///<returnsx/returns>publicstaticIntPtrGetHandle(Pointp)returnWindowFromPoint(p);//鼠標(biāo)位置的坐標(biāo)publicstaticPointGetCursorPosPoint()PointP=newPoint();if(GetCursorPos(outp))returnp;returndefault(Point);/…〃/子窗口句柄///</summary>/// 父窗口句柄</param>/// 前一個(gè)同目錄級(jí)同名窗口句柄</param>/// 類名</param>///<returns><∕returns>publicstaticIntPtrGetChildHandle(IntPtrhwndParent,IntPtrhwndChildAfter,stringIpszClass){returnFindWindowEx(hwndParent,hwndChildAfter,lpszClass,null);}///<summary>///全部子窗口句柄///</summary>/// 父窗口句柄</param>/// 類名</param>///<returns></returns>publicstaticList<IntPtr>GetChildHandleS(IntPtrhwndParent,stringClaSSName){List<IntPtr>resultList=newList<IntPtr>();for(IntPtrhwndClient=GetChildHandle(hwndParent,IntPtr.Zero,className);hwndClient=IntPtr.Zero;hwndClient=GetChildHandle(hwndParent,hwndClient,className)){resultList.Add(hwndClient);}returnresultList;}///<summary>〃/給窗口發(fā)送內(nèi)容///</summary>/// 句柄</param>/// 要發(fā)送的內(nèi)容</param>publicstaticvoidSetText(IntPtrhWnd,stringlParam){SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);}privateconstintWM_SETTEXT=0x000C;///<summary>///獲得窗口內(nèi)容或標(biāo)題///</summary>/// 句柄</param>///<returns></returns>publicstaticstringGetText(IntPtrhWnd){StringBuilderresult=newStringBUilder(128);GetWindowText(hWnd,result,result.Capacity);returnresult.ToString();}///<summary>〃/找類名///</summary>/// 句柄</param>///<returns></returns>publicstaticstringGetClassName(IntPtrhWnd){StringBuilderlpClassName=newStringBUilder(128);if(GetClaSSName(hWnd,lpClassName,lpClassName.Capacity)==0){thrownewException( );}returnlpClassName.ToString();}///<summary>///窗口在屏幕位置///</summary>///句柄</param>///<returns><∕returns>publicstaticRectangleGetWindowRect(IntPtrhWnd){Rectangleresult=default(Rectangle);GetWindowRect(hWnd,refresult);returnresult;}///<summary>///窗口相對(duì)屏幕位置轉(zhuǎn)換成父窗口位置///</summary>/////////<returns></returns>publicstaticRectangleSCreenToCIient(IntPtrhWnd,Rectanglerect){Rectangleresult=rect;SCreenToClient(hWnd,refresult);returnresult;}///<summary>///窗口大小///</summary>//////<returns><∕returns>publicstaticRectangleGetClientRect(IntPtrhWnd){Rectangleresult=default(Rectangle);GetClientRect(hWnd,refresult);returnresult;}#endregion}publicclassWinHWND{publicIntPtrHWND{get;set;}publicstringClassName{get;set;}publicWinHWNDParent{get;set;}publicintInParentSequence{get;set;}privateRectangle_CUrrentRect;privatestring_Text;privateint_Left;privateint_Top;privateint_Width;privateintHeight;publicstringTextgetreturn_Text==default(String)?WinAPI.GetText(this.HWND):_Text;set_Text=value;WinAPI.SetText(this.HWND,value);publicintLeftgetreturn_Left==default(int)?_CUrrentRect.Left:_Left;set_Left=value;WinAPI.MoveWindow(this.HWND,value,this.Top,this.Width,this.Height,true)publicintTopget{return_Top==default(int)?_CurrentRect.Top:_Top;}set{_Top=value;WinAPI.MoveWindow(this.HWND,this.Left,value,this.Width,this.Height,true);}}publicintWidth{get{return_Width==default(int)?_currentRect.Width:_Width;}set{_Width=value;WinAPI.MoveWindow(this.HWND,this.Left,this.Top,value,this.Height,true);}}publicintHeight{get{returnHeight==default(int)?currentRect.Height:Height;}set{_Height=value;WinAPI.MoveWindow(this.HWND,this.Left,this.Top,this.Width,value,true);}}privateWinHWND(){}publicWinHWND(IntPtrhWnd){this.HWND=hWnd;this.ClassName=GetClaSSName();this.Parent=GetParent();this.InParentSequence=GetInParentSeqUence();this._currentRect=GetRect();}privatestringGetClassName(){returnWinAPI.GetClassName(this.HWND);}privateWinHWNDGetParent(){if(WinAPI.GetParent(this.HWND)==null){}thrownewException();if(WinAPI.GetParent(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)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(WinAPI.GetHandle(WinAPI.GetCursorPosPoint()));}publicoverridestringToString(){StringBuilderresult=newStringBuilder();for(WinHWNDWinHandle=this;WinHandle!=null;WinHandle=WinHandle.Parent){result.Append(string.Format( ,Escape(WinHandle.ClassName),WinHandle.InParentSequence.ToString()));if(WinHandle.I
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 在酒店實(shí)習(xí)報(bào)告模板集合七篇
- 貨運(yùn)綜合樞紐項(xiàng)目運(yùn)營(yíng)方案
- 供排水項(xiàng)目建設(shè)方案
- 初中音樂(lè)教師個(gè)人工作總結(jié)4篇
- xx省內(nèi)河港口項(xiàng)目可行性研究報(bào)告
- 上課講話檢討書合集15篇
- 社團(tuán)聯(lián)合會(huì)工作總結(jié)
- 城市停車場(chǎng)項(xiàng)目計(jì)劃書
- 頂崗實(shí)習(xí)報(bào)告模板集錦6篇
- 建設(shè)工程承包合同(合集15篇)
- 2024北京西城初二(上)期末語(yǔ)文試卷及答案
- 強(qiáng)制報(bào)告制度課件
- 33-提高附著式升降腳手架首層防護(hù)一次驗(yàn)收合格率(4-3)
- 《礦山隱蔽致災(zāi)因素普查規(guī)范》解讀培訓(xùn)
- 電信業(yè)務(wù)運(yùn)營(yíng)與服務(wù)規(guī)范
- 報(bào)考中級(jí)會(huì)計(jì)的從事會(huì)計(jì)工作年限證明模板
- 滅火器、消防栓安全檢查表
- 東方電影學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 2024年-2025年公路養(yǎng)護(hù)工理論知識(shí)考試題及答案
- 地 理世界的聚落 課件-2024-2025學(xué)年七年級(jí)地理上學(xué)期(湘教版2024)
- 建筑施工安全檢查標(biāo)準(zhǔn)JGJ59-2011
評(píng)論
0/150
提交評(píng)論