首先介绍基本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. 基于Clang的Source to Source源代码转换(一)

    Clang中包含了非常多的关于抽象语法树(AST)的访问和操作的类和接口.我们程序开发人员可以直接通过继承其中的某些类,重写其中的关键成员方法,从而形成我们自己的对抽象语法树的操作. 那么,首先我们简 ...

  2. fiddler如何修改request header

    在命令行中输入命令:  bpu www.baidu.com   (这种方法只会中断www.baidu.com) 然后刷新网站,在fiddler中点击被打断的网址,点击Inspectors—>Ra ...

  3. POJ 3070 Fibonacci

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. linux内存占用查看命令

    ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' | grep tomcat | sort -nrk5 其中rsz为实际内存,上例实现按内存排序 ...

  5. Web API

    https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api ...

  6. jquery 设置页面元素不可点击、不可编辑、只读(备忘)

    $("input").attr('readonly', true); $("textarea").attr('readonly', true); $(':rad ...

  7. 用Python生成组织机构代码,附源码

    #!/usr/bin/python import random def haoma(): ww = [3,7,9,10,5,8,4,2]#suan fa yin zi cc = [] dd=0 for ...

  8. windows server 2012 r2 远程桌面连接指南

    具体详情请阅览文档  http://pan.baidu.com/s/1jHTCpW6 windows server 2012 r2 远程桌面连接指南 - 作者 rick·bao - 日期 2016-0 ...

  9. latex给表格添加注释

    给表格加注释的确是很多TeX用户不好用的地方,这里提供一个样式和代码,或许对于你的学习使用有所帮助,样式如下: 代码如下: \documentclass[11pt,a4paper,english]{a ...

  10. JavaScript中的各种宽高属性

    转自慕课网:http://www.imooc.com/article/14516   在js中,存在着N多的关于高度和宽度的属性,比如:clientHeight.offsetHeight.scroll ...