首先介绍基本WindowsApi:

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

函数说明:在窗口列表中寻找与指定条件相符的第一个窗口

导入库:user32.lib
头文件:winuser.h
命名空间 using System.Runtime.InteropServices;
参数说明 
lpClassName String,窗口类名
lpWindowName String,窗口标题
返回值:窗口句柄

public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

函数说明:在窗口列表中寻找与指定条件相符的第一个子窗口

导入库:user32.lib
头文件:winuser.h
命名空间 using System.Runtime.InteropServices;
参数说明 
hwndParent IntPtr ,父窗口句柄,如果hwndParent为 0 ,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。
hwndChildAfter IntPtr ,子窗口句柄,查找从在Z序中的下一个子窗口开始。子窗口必须为hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。
lpszClass string ,控件类名
lpszWindow string ,控件标题,如果该参数为 NULL,则为所有窗口全匹配。
返回值:控件句柄。
 
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
函数功能:枚举一个父窗口的所有子窗口。
导入库:user32.lib
头文件:winuser.h
命名空间 using System.Runtime.InteropServices;
参数说明
hWndParent IntPtr 父窗口句柄
lpfn CallBack 回调函数的地址
lParam int 自定义的参数
注意:回调函数的返回值将会影响到这个API函数的行为。如果回调函数返回true,则枚举继续直到枚举完成;如果返回false,则将会中止枚举。
其中CallBack是这样的一个委托:public delegate bool CallBack(IntPtr hwnd, int lParam); 如果 CallBack 返回的是true,则会继续枚举,否则就会终止枚举。
 
应用实例:

用到的WindowApi类

static class WindowsApi
{
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
public static extern void SetForegroundWindow(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);

public delegate bool CallBack(IntPtr hwnd, int lParam);
}

class Program

{

/// <summary>
/// 查找窗体上控件句柄
/// </summary>
/// <param name="hwnd">父窗体句柄</param>
/// <param name="lpszWindow">控件标题(Text)</param>
/// <param name="bChild">设定是否在子窗体中查找</param>
/// <returns>控件句柄,没找到返回IntPtr.Zero</returns>
private IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild)
{
IntPtr iResult = IntPtr.Zero;
// 首先在父窗体上查找控件
iResult = WindowsApi.FindWindowEx(hwnd, 0, null, lpszWindow);
// 如果找到直接返回控件句柄
if (iResult != IntPtr.Zero) return iResult;

// 如果设定了不在子窗体中查找
if (!bChild) return iResult;

// 枚举子窗体,查找控件句柄
int i = WindowsApi.EnumChildWindows(
hwnd,
(h, l) =>
{
IntPtr f1 = WindowsApi.FindWindowEx(h, 0, null, lpszWindow);
if (f1 == IntPtr.Zero)
return true;
else
{
iResult = f1;
return false;
}
},
0);
// 返回查找结果
return iResult;
}

private void OnRunClick(object sender, EventArgs e)
{
// 查找主界面句柄
IntPtr mainHandle = WindowsApi.FindWindow(null, "主界面(Ver:3.1.3.47)");
if (mainHandle != IntPtr.Zero)
{
// 查找按钮句柄
IntPtr iBt = FindWindowEx(mainHandle, "现(F1)", true);
if (iBt != IntPtr.Zero)
// 发送单击消息
WindowsApi.SendMessage(iBt, 0xF5, 0, 0);
}
}

}

应用工具:

这里可以应用spy工具来查看窗口的句柄、标题、类型等信息,非常方便。

[原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。的更多相关文章

  1. [原创]C#应用WindowsApi实现查找(FindWindowEx)文本框(TextBox、TextEdit)。

    /// <summary> /// 获取文本框控件 /// </summary> /// <param name="hwnd">文本框所在父窗口 ...

  2. WPF中查找指定类型的父控件

    /// <summary> /// 查找父控件 /// </summary> /// <typeparam name="T"></type ...

  3. [原创]C#按比例缩放窗体控件及字体

    按照比例缩放窗体控件及字体,如需等比例缩放,只需将x,y的比例设置成相同即可. 为了减小误差,建议使用原始尺寸来计算比例. private float X, Y; private bool b = f ...

  4. 在gridview里查找模板里的button控件

    这个问题,真是搞了我1天,这次记住他 第一种方法: protected void GridView1_RowCommand(object sender, GridViewCommandEventArg ...

  5. C# 控件双缓冲控制 ControlStyles 枚举详解

    ControlStyles 枚举 .NET Framework 4    指定控件的样式和行为. 此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合. 命名空间:  Sy ...

  6. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  7. 【转】WPF查找子控件和父控件方法

    一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...

  8. WPF查找子控件和父控件方法

    一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...

  9. 除虫记——有关WindowsAPI文件查找函数的一次压力测试

    作者:朱金灿 来源:http://blog.csdn.net/clever101 这里说的除虫是指排除bug的意思.今天排除了一个有意思的bug,其中的场景大致是这样的:现在你要统计一个文件夹下非隐藏 ...

随机推荐

  1. PHP是弱类型语言,自动转换,强制转换

    强制转换: (int) - 转换成整型 (bool) - 转换.成布尔型 (float) - 转换成浮点型 (string) - 转换成字符串 (array) - 转换成数组 (object) - 转 ...

  2. javascript实现九九乘法表

    CSS代码部分: <style type="text/css"> table { width: 800px; height: 300px; border-collaps ...

  3. 一些Unity基础操作的性能测试

    从以前一个文章转移过来的内容,以后会进一步进行测试  内容  毫秒数(Editor)  毫秒数(Build PC) 加减内部变量 4ms  1ms new List<int>() 559m ...

  4. ISAP算法对 Dinic算法的改进

    ISAP算法对 Dinic算法的改进: 在刘汝佳图论的开头引言里面,就指出了,算法的本身细节优化,是比较复杂的,这些高质量的图论算法是无数优秀算法设计师的智慧结晶. 如果一时半会理解不清楚,也是正常的 ...

  5. 安装AdventureWorks2008R2

    在微软的网站,有介绍安装示例数据库AdventureWorks的说明. 你可以在这里下载到压缩包 (AdventureWorks2008R2_Database.zip),解压后会得到两个文件: Adv ...

  6. 【前端】Node.js学习笔记

    module.exports 使用方式: // File Name: hello.js function greet() {/*......*/} // 有下面这两种写法: // 1. module. ...

  7. centos6.6_64位操作系统安装时候出现kernel panic - not syncing: Attempted to kill init 解决办法

    最近在VM上安装centos时候经常被这个问题虐,后来进入单用户模式在   kernel /vmlinuz-XXXXro root=/dev/vogroup00/logvol00 rhgb quie  ...

  8. 25.redis集群搭建笔记

    ###Redis集群### 0.准备 软件: redis-3.0.0.gem redis-3.0.0.tar.gz#源码   1.安装ruby环境 redis基于ruby槽位计算,hash算法技术,k ...

  9. SQL Server优化常用SQL语句

    --所有没有主键的表 select name from sysobjects where xtype='U' and id not in ( select i.parent_obj from syso ...

  10. PHP5.5.13 + Apache2.4.7安装配置流程详解

    ---恢复内容开始--- 自学PHP的这段时间里,真是倍感辛酸,相信广大的菜鸟们应该很我感同身受吧,在查阅了网上和众多数资料后,总结出来想当比较全面的安装方法,拿出来与广大的编程爱好者一起分享哈. 首 ...