已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#查找指定窗口的子窗口的句柄 用axWebBrowser加载HTML网页时,真正显示内容的窗体并不是axWebBrowser,而是其子窗口的子窗口一个名为Internet Explorer_Server的类。从spy+可知: 公司需要在网页上进行手写,需要对Internet Explorer_Server进行操作,而通过axWebBrowser的Handle不能直接操作Internet Explorer_Server。于是在网上搜到Paul DiLascia写的一个CFindWnd类,是用C+写的,由于我用C#进行了改写。这个类主要用的的API 是EnumChildWindows和FindWindowEx,第一个遍历指定窗口下的子窗口,第二个查找指定名称的窗口,如果找到返回此窗口Handle。该类的用法:FindWindow fw = new FindWindow(wndHandle, ChildwndClassName); /实例化,第一个参数是要查找的起始窗口的句柄;第二个参数是要查找的窗口的类的名称。现在我们需要的传的是Internet Explorer_Server。IntPtr ip = fw.FoundHandle;/FindWindow的公共属性FoundHandle就是查找到的窗口的句柄。完整的类如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceSystemManager.Utility./*/Thisclassistofindthegivenwindowschildwindowaccrodingtothegivenchildwindowsname./Theuseage:FindWindowfw=newFindWindow(wndHandle,ChildwndClassName);IntPtrip=fw.FoundHandle;/IadaptthecodefromPaulDiLascia,whoistheMSDNMagazineswriter./TheoriginalclassisnamedCFindWndwhichiswritteninC+,andyoucouldgetitonInternet./isagreatwebsite.ItincludesalmostalltheAPIfuctointobeusedinC#./classFindWindow.DllImport(user32)return:MarshalAs(UnmanagedType.Bool)/IMPORTANT:LPARAMmustbeapointer(InterPtr)inVS2005,otherwiseanexceptionwillbethrownprivatestaticexternboolEnumChildWindows(IntPtrwindow,EnumWindowProccallback,IntPtri);/thecallbackfunctionfortheEnumChildWindowsprivatedelegateboolEnumWindowProc(IntPtrhWnd,IntPtrparameter);/iffoundreturnthehandle,otherwisereturnIntPtr.ZeroDllImport(user32.dll,EntryPoint=FindWindowEx)privatestaticexternIntPtrFindWindowEx(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass,stringlpszWindow);privatestringm_classname;/classnametolookforprivateIntPtrm_hWnd;/HWNDiffoundpublicIntPtrFoundHandle.get.returnm_hWnd;/ctordoesthework-justinstantiateandgopublicFindWindow(IntPtrhwndParent,stringclassname).m_hWnd=IntPtr.Zero;m_classname=classname;FindChildClassHwnd(hwndParent,IntPtr.Zero);/*/Findthechildwindow,iffoundm_classnamewillbeassigned/parentshandle/theapplicationvalue,nonuse/foundornotfound/TheC+codeisthatlParamistheinstanceofFindWindowclass,iffoundassigntheinstancesm_hWndprivateboolFindChildClassHwnd(IntPtrhwndParent,IntPtrlParam).EnumWindowProcchildProc=newEnumWindowProc(FindChildClassHwnd);IntPtrhwnd=FindWindowEx(hwndParent,IntPtr.Zero,this.m_classname,string.Empty);if(hwnd!=IntPtr.Zero).this.m_hWnd=hwnd;/found:saveitreturnfalse;/stopenumeratingEnumChildWindows(hwndParent,childProc,IntPtr.Zero);/recurseredoFindChildClassHwndreturntrue;/keeplooking注意:在VS2005中,MDA检查的很严格,LPARAM是64位,要用IntPtr表示(网上有人用long来表示,我试了,MDA会抛出异常)。是个不错的网站,它包含了几乎所有的API在.NET中的调用说明,还有例子。附Paul DiLascia用C+写的CFindWnd类:/*/MSDNMagazine-August2003/Ifthiscodeworks,itwaswrittenbyPaulDiLascia./Ifnot,Idontknowwhowroteit./CompileswithVisualStudio.NETonWindowsXP.Tabsize=3./-/Thisclassencapsulatestheprocessoffindingawindowwithagivenclassname/asadescendantofagivenwindow.Touseit,instantiatelikeso:/CFindWndfw(hwndParent,classname);/fw.m_hWndwillbetheHWNDofthedesiredwindow,iffound./classCFindWnd.private:/*/ThisprivatefunctionisusedwithEnumChildWindowstofindthechild/withagivenclassname.ReturnsFALSEiffound(tostopenumerating)./staticBOOLCALLBACKFindChildClassHwnd(HWNDhwndParent,LPARAMlParam).CFindWnd*pfw=(CFindWnd*)lParam;HWNDhwnd=FindWindowEx(hwndParent,NULL,pfw-m_classname,NULL);if(hwnd).pfw-m_hWnd=hwnd;/found:saveitreturnFALSE;/stopenumeratingEnumChildWindows(hwndParent,FindChildClassHwnd,lParam);/recursereturnTRUE;/keeplookingpublic:LPCSTRm_classname;/classnametolookforHWNDm_hWnd;/HWNDiffound/ctordoesthework-justinstantiateandgoCFindWnd(HWNDhwndParent,LPCSTRclassname):m_hWnd(NULL),m_classname(classname).FindChildClassHwnd(hwndParent,(LPARAM)this);2009年更新通过实验得知,FindWindowEx可以通过classname或caption(也就是窗口的title)查找窗口,且如果第一个参数传IntPtr.Zero的话,将从Windows最顶层窗口开始查找,但是窗口很多的话这样会非常的慢,所以加入Timeout的判断,如果超时还没找到,返回false。用法:FindWindow fw = new FindWindow(IntPtr.Zero, null, ThunderDFrame, 10);/查找Title为ThunderDFrame的窗口,如果10秒内还没找到,返回false代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace Util class FindWindow DllImport(user32) return: MarshalAs(UnmanagedType.Bool) /IMPORTANT : LPARAM must be a pointer (InterPtr) in VS2005, otherwise an exception will be thrown private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); /the callback function for the EnumChildWindows private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); /if found return the handle , otherwise return IntPtr.Zero DllImport(user32.dll, EntryPoint = FindWindowEx) private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); private string m_classname; / class name to look for private string m_caption; / caption name to look for private DateTime start; private int m_timeout;/If exceed the time. Indicate no windows found. private IntPtr m_hWnd; / HWND if found public IntPtr FoundHandle get return m_hWnd; private bool m_IsTimeOut; public bool IsTimeOut getreturn m_IsTimeOut; set m_IsTimeOut = value; / ctor does the work-just instantiate and go public FindWindow(IntPtr hwndParent, string classname, string caption, int timeout) m_hWnd = IntPtr.Zero; m_classname = classname; m_caption = caption; m_timeout = timeout; start = DateTime.Now; FindChildClassHwnd(hwndParent, IntPtr.Zero); /*/ / / Find the child window, if found m_classname will be assigned / / parents handle / the application value, nonuse / found or not found /The C+ code is that lParam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026四川凉山州西昌市城市管理局招聘工作人员13名农业考试备考题库及答案解析
- 2026湖北教师招聘统考西塞山区城区义务教育学校教师招聘16人农业笔试备考试题及答案解析
- 2026湖北教师招聘统考监利市招聘101人农业笔试备考题库及答案解析
- 2026湖南永州市东安县事业单位人才引进(第二批)5人农业笔试备考试题及答案解析
- 2026年公务用车管理试题(附答案)
- 2026浙江杭州市西湖区审计局招聘编外工作人员1人农业笔试模拟试题及答案解析
- 2026云南轻纺职业学院云南省产业导师选聘10人农业笔试模拟试题及答案解析
- 2026广东汕尾市农文旅发展有限公司招聘12人农业笔试备考试题及答案解析
- 2026年高温中暑应急试题及答案
- 企业技术转让与合作方案
- 2026年血站上岗证测试卷【巩固】附答案详解
- 2025-2026学年天津市河西区七年级下学期期中数学试卷(含答案)
- 2026年钳工技能鉴定考核综合提升练习试题(考点梳理)附答案详解
- 《年历、月历中的信息》教案-2025-2026学年苏教版小学三年级数学下册
- 消防大队保密工作制度
- 2026石嘴山经济技术开发区实业开发有限公司招聘17人考试备考试题及答案解析
- 郑州信息科技职业学院2026年单独招生《职业适应性测试》模拟试题
- 2026年国家药品监督管理局药品和医疗器械审评检查京津冀分中心、华中分中心、西南分中心公开招聘编外人员122名(第一批)笔试参考试题及答案解析
- 2026年春教科版(新教材)小学科学三年级下册(全册)知识点复习要点梳理
- 中国脑外伤康复指南(2025版)
- 2026校招:华夏银行笔试题及答案
评论
0/150
提交评论