博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]C#中捕捉对话框的文本内容 EnumChildWindows
阅读量:7069 次
发布时间:2019-06-28

本文共 2644 字,大约阅读时间需要 8 分钟。

如何找到桌面上报错的窗口,不管是父窗口还是子窗口,而且获得它的出错信息呢? 

主要是利用API函数: 

复制 
保存
[DllImport("user32.dll")]public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,    string lpszClass, string lpszWindow);[DllImport("user32.dll")]public static extern int FindWindow(string strclassName, string strWindowName);[DllImport("user32.dll")]public static extern int GetLastActivePopup(int hWnd);[DllImport("user32.dll")]public static extern int AnyPopup();[DllImport("user32.dll")]public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);[DllImport("user32.dll")]public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);[DllImport("user32.dll")]public static extern int EnumWindows(CallBack lpfn, int lParam);[DllImport("user32.dll")]public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

最关键的是对windows操作系统中窗口本质的认识,使用Spy++工具,查找窗口就可以发现,其实对于给定的对话框窗口,其中的任何控件,如图标、文本、确定、取消按钮等都是它的子窗口,本质上还是窗口,所不同的只是,顶级父窗口查找时,用FindWindow函数,而查找子窗口时用FindWindowEx。 
另外比较有用的是EnumWindows,可以遍历所有的顶级父窗口,而EnumChildWindows则是遍历其子窗口。经过测试,EnumThreadWindows的回调函数无法调用,不知道是什么原因,望高手指教。 
所以问题的解决思路就是使用EnumWindows遍历所有的顶级父窗口,对每个顶级父窗口使用EnumChildWindows遍历它的所有控件,每个控件其实也是窗口,拿到该控件的句柄后,就可以调用GetWindowText来获取文本信息了。 
具体实现时,首先需要定义以上API函数的回调函数代理: 

复制 
保存
/// /// 回调函数代理/// public delegate bool CallBack(int hwnd, int lParam);

然后必须针对每个API函数定义代理的实例函数: 

复制 
保存
/// /// 进程回调处理函数/// /// /// /// 
public static bool ThreadWindowProcess(int hwnd, int lParam){ EnumChildWindows(hwnd, callBackEnumChildWindows, 0); return true;}/// /// 窗口回调处理函数/// /// /// ///
public static bool WindowProcess(int hwnd, int lParam){ EnumChildWindows(hwnd, callBackEnumChildWindows, 0); return true;}/// /// 子窗口回调处理函数/// /// /// ///
public static bool ChildWindowProcess(int hwnd, int lParam){ StringBuilder title = new StringBuilder(200); int len; len = GetWindowText(hwnd, title, 200); if (len > 0) { if (title.ToString().IndexOf(GlobalManager.ErrorMessage) != -1) { FindError = true; } } return true;}

最后要定义回调代理的实例 

复制 
保存
/// /// 进程窗口回调函数代理/// public static CallBack callBackEnumThreadWindows = new CallBack(ThreadWindowProcess);/// /// 窗口回调函数代理/// public static CallBack callBackEnumWindows = new CallBack(WindowProcess);/// /// 子窗口回调函数代理/// public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);

使用的例子: 

复制 
保存
/// /// 客户端是否弹出对话框/// /// 
public bool IsClientPopupWindows(){ bool FindError = false; EnumWindows(callBackEnumWindows, 0); return FindError;}

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/08/08/2628836.html

你可能感兴趣的文章
Java中BigDecimal的应用
查看>>
DataGrid的itemreader中使用outerDocument
查看>>
理解 JavaScript 的函数
查看>>
面试题之总结-大数运算
查看>>
我们为什么要使用NOSQL非关系数据库?
查看>>
【转载】QPS、PV 、RT(响应时间)之间的关系
查看>>
PHPdom操作查找xml标签文本
查看>>
git多个公钥
查看>>
java基础
查看>>
idea在window10 快捷键的坑
查看>>
算法编程题
查看>>
读书笔记(二)Java中值传递、引用传递理解
查看>>
多级联动代码示例-数据为数组
查看>>
Java媒体框架(JMF),个人很欣赏.... (转)
查看>>
将一个php的一个查询代码改成go语言
查看>>
[转]Java大数据量导出Excel的问题
查看>>
学习emoji
查看>>
服务器端物理实现(三)
查看>>
HDFS和本地文件系统的关系
查看>>
mac下多个php版本快速切换的方法
查看>>