Windows API 查找窗体,发送Windows消息
最近项目中需要做Windows消息截获操作,在网上找了一些资料。
public class WindowsAPI
{
/// <summary>
/// 回调函数代理
/// </summary>
public delegate bool CallBack(int hwnd, int lParam);
public const int WM_GETTEXT = 0x000D;
public const int WM_SETTEXT = 0x000C;
/// <summary>
/// 鼠标click事件
/// </summary>
public const int WM_CLICK = 0x00F5; public const int WM_KEYUP = 0x0101; #region
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); [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); /// <summary>
/// 获取活动窗体句柄
/// </summary>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam); [DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam); #endregion /// <summary>
/// 获取枚举描述
/// </summary>
/// <param name="enumValue">枚举值</param>
/// <returns>描述信息</returns>
public static string GetEnumDescription(Enum enumValue)
{
string str = enumValue.ToString();
System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (objs == null || objs.Length == ) return str;
System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[];
return da.Description; }
}
至于发送的消息可以通过“Spy++”工具抓取,然后模拟发送。
var pHand = WindowsAPI.FindWindow(null, windowsName);//查找进程窗体
//查找子窗体
var tabControl = WindowsAPI.FindWindowEx(pHand, (IntPtr), "SysTabControl32", ""); //多页选择框,其中第三个参数是第几页,如0,1,2,3,4...
//4912会点击按钮,并切换到对应页。4876只会点按钮,不会切换页 var r = WindowsAPI.SendMessage(tabControl, , , );//// //发送点击事件
int clickPrintDoneOk = WindowsAPI.SendMessage(childHandPrintDoneOk, WindowsAPI.WM_CLICK, , );
Windows API 查找窗体,发送Windows消息的更多相关文章
- c#让窗体永在最前 调用windows api 将窗体设为topmost
有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...
- VB用windows API激活子窗体
http://files.cnblogs.com/files/liuzhaoyzz/%E6%BF%80%E6%B4%BB%E5%AD%90%E7%AA%97%E4%BD%93.rar setforeg ...
- 1.java soap api操作和发送soap消息
转自:https://blog.csdn.net/lbinzhang/article/details/84721359 1. /** * soap请求 * * @return * @throws Ex ...
- c# 判断窗体是否永在最前(TopMost),调用windows API
许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...
- WINDOWS API 大全(二)
9. API之设备场景函数 CombineRgn 将两个区域组合为一个新区域CombineTransform 驱动世界转换.它相当于依顺序进行两次转换CreateCompatibleDC 创建一个与特 ...
- 在VBA中使用Windows API
VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...
- WinSpy涉及的windows api
WinSpy涉及的windows api WinSpy是仿造微软Spy++的开源项目,但只涉及Spy++的窗口句柄.窗口的属性.styles.类名子窗口.进程线程信息等查找功能.功能虽然不算强大,但涉 ...
- Windows API的消息处理机制
上个学期找实习失利,让我觉得自己基础打得不够牢固,所以在华为实习的这三个月里,每天下班都在复习理论课的知识,顺便刷了一个月的 LeetCode.本来以为找工作是势在必得了,结果这个学期秋季校招的坑爹经 ...
- C# Windows Api的一些方法 封装 以及 常用参数
using System;using System.Collections.Generic;using System.Drawing;using System.Diagnostics;using Sy ...
随机推荐
- 1.TypeError: must be str, not bytes
1.TypeError: must be str, not bytes错误: 解答: 写文件处 open(filename, 'w').write 应该写为 open(filename, 'wb'). ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Javac的命令(-Xlint)
在OptionName类中的枚举定义如下: XLINT("-Xlint"), XLINT_CUSTOM("-Xlint:"), -Xlint Enabl ...
- 设置spacevim字体显示乱码问题
https://github.com/powerline/fonts clone powerline fonts 仓库 执行项目中的 install.sh 安装字体 修改终端配置中使用的字体为 xxx ...
- orcale 之pl/sql例外
orcale 中的例外我们可以看作是其他编程语言中的异常,是为了增强语言的健壮性和容错性. 在orcale中常见的有以下几种: No_data_found 很容易理解就是没有数据返回. Too_man ...
- svn 改英文
- [转]使用ASP.NET Web API 2创建OData v4 终结点
本文转自:http://www.cnblogs.com/farb/p/ODataAspNetWebAPI.html 开放数据协议(Open Data Protocol[简称OData])是用于Web的 ...
- 根据Time Protocol从NIST Internet Time Servers获取准确时间
Time Protocol(RFC-868)是一种非常简单的应用层协议:它返回一个32位的二进制数字,这个数字描述了从1900年1月1日0时0分0秒到现在的秒数,服务器在TCP的37号端口监听时间协议 ...
- css 样式加载次序
一般而言,所有的样式会根据下面的规则层叠于一个新的虚拟样式表中,其中数字 4 拥有最高的优先权(本人理解为:先加载1的样式,然后用2的样式覆盖1中的样式,3.4同理) 浏览器缺省设置(浏览器默认的样式 ...
- js类的笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...