如何调用FindWindow API获取主窗体
2017-06-21 11:55
489 查看
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace HtmlShow
{
/**/
/// <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, string lpszClass, string lpszWindow);
private string m_classname; // class name to look for
private IntPtr m_hWnd; // HWND if found
public IntPtr FoundHandle
{
get { return m_hWnd; }
}
// ctor does the work--just instantiate and go
public FindWindow(IntPtr hwndParent, string classname)
{
m_hWnd = IntPtr.Zero;
m_classname = classname;
FindChildClassHwnd(hwndParent, IntPtr.Zero);
}
//EnumChildWindows是API函数,能够遍历主窗口下所有子窗口。不过它的遍历过程是通过
//第二个参数即回调函数与程序员交互的。EnumChildWindows每找到一个窗口。就调用回调
//函数。回调函数如果返回false。遍历就会结束。
/**/
/// <summary>
/// Find the child window, if found m_classname will be assigned
/// </summary>
/// <param name="hwndParent">parent's handle</param>
/// <param name="lParam">the application value, nonuse</param>
/// <returns>found or not found</returns>
//The C++ code is that lParam is the instance of FindWindow class , if found assign the instance's m_hWnd
private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty);
if (hwnd != IntPtr.Zero)
{
this.m_hWnd = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd
return true;// keep looking
}
}
}
//this.Handle是要查找窗口的父窗口,因为我使用的是webBrowser控件
FindWindow fw = new FindWindow(this.Handle, "Internet Explorer_Server");
hIE = fw.FoundHandle;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace HtmlShow
{
/**/
/// <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, string lpszClass, string lpszWindow);
private string m_classname; // class name to look for
private IntPtr m_hWnd; // HWND if found
public IntPtr FoundHandle
{
get { return m_hWnd; }
}
// ctor does the work--just instantiate and go
public FindWindow(IntPtr hwndParent, string classname)
{
m_hWnd = IntPtr.Zero;
m_classname = classname;
FindChildClassHwnd(hwndParent, IntPtr.Zero);
}
//EnumChildWindows是API函数,能够遍历主窗口下所有子窗口。不过它的遍历过程是通过
//第二个参数即回调函数与程序员交互的。EnumChildWindows每找到一个窗口。就调用回调
//函数。回调函数如果返回false。遍历就会结束。
/**/
/// <summary>
/// Find the child window, if found m_classname will be assigned
/// </summary>
/// <param name="hwndParent">parent's handle</param>
/// <param name="lParam">the application value, nonuse</param>
/// <returns>found or not found</returns>
//The C++ code is that lParam is the instance of FindWindow class , if found assign the instance's m_hWnd
private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty);
if (hwnd != IntPtr.Zero)
{
this.m_hWnd = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd
return true;// keep looking
}
}
}
//this.Handle是要查找窗口的父窗口,因为我使用的是webBrowser控件
FindWindow fw = new FindWindow(this.Handle, "Internet Explorer_Server");
hIE = fw.FoundHandle;
相关文章推荐
- 如何使用python3调用openstack keystone identity REST api接口获取X-AUTH-TOKEN
- iframe子窗体和父窗体互相调用,通过document.getElementById("name_iframe").contentWindow获取iframe的window对象,支持多浏览器!
- 如何在Windows窗体中调用系统调色板对话框,并获取用户所选择的颜色?
- 最近接触到的一些js调用window窗体的属性及通过JavaScript获取页面大小
- 【c/c++】如何调用【Window】cmd命令行命令并获取命令行的输出内容
- 如何获取youtobe中的视频的缩略图等相关信息(调用API返回)?
- 详解如何调用zabbix API获取主机
- 免费PM2.5数据如何获取之PM25.in网站API调用JSON数据处理方法
- 在C#中如何调用windows的API进行关机操作!!! [转]
- 如何动态调用之Java脚本API
- 如何获取错误消息说明使用 FormatMessage API
- 如何获取当前线程的调用堆栈
- 如何动态调用之Java脚本API
- 如何跨越线程调用窗体上的控件
- 如何跨越线程调用窗体上的控件
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API
- API之五在PB中如何获取目录信息
- 【翻译】MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API
- CSharp Tips:调用Win32 API如何处理指针类型的参数
- 在C#中调用API获取网络信息和流量