C#查找指定窗口的子窗口的句柄

本文介绍如何在C#中使用EnumChildWindows和FindWindowEx API来查找axWebBrowser加载网页时实际显示内容的Internet Explorer_Server窗口的句柄。通过FindWindow类实现,该类的FoundHandle属性提供查找到的窗口句柄。此外,更新内容加入了Timeout机制,提高查找效率。
摘要由CSDN通过智能技术生成

用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就是查找到的窗口的句柄。

完整的类如下: 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;

namespace  SystemManager.Utility
{
    
/// <summary>
    
/// This class is to find the given window's child window accroding to the given child window's name.
    
/// The useage: FindWindow fw = new FindWindow(wndHandle, "ChildwndClassName"); IntPtr ip = fw.FoundHandle;
    
/// I adapt the code from Paul DiLascia,who is the MSDN Magazine's writer.
    
/// The original class is named CFindWnd which is written in C++, and you could get it on Internet.
    
/// www.pinvoke.net is a great website.It includes almost all the API fuctoin to be used in C#.
    
/// </summary>

    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, 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值