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 ...
随机推荐
- (转)Mysql数据库主从心得整理
Mysql数据库主从心得整理 原文:http://blog.sae.sina.com.cn/archives/4666 管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本 ...
- 当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法
当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法 主要用在Controller层 @ExceptionHandler(Run ...
- javac的命令(-Xbootclasspath、-classpath与-sourcepath等)
当编译源文件时,编译器常常需要识别出类型的有关信息.对于源文件中使用.扩展或实现的每个类或接口,编译器都需要其类型信息.这包括在源文件中没有明确提及.但通过继承提供信息的类和接口. 例如,当扩展 ja ...
- mock时忽略不必要的初始化
PowerMockito.mockStatic时忽略加载类的静态field,比如想PowerMockito.mockStatic(ClassA.class),ClassA如下 public class ...
- OSI及TCP/IP的概念和区别
什么是TCP/IP协议 TCP/IP协议(Transfer Controln Protocol/Internet Protocol)叫做传输控制/网际协议,又叫网络通讯协议,这个协议是Internet ...
- ora-01747:因为表中存在关键字造成的
ORCLE报错解决(ora-01747:无效的用户.表.列,表.列) 一.ora-01747:无效的用户.表.列,表.列 这个问题出现是因为表中存在关键字造成的,如果想新增数据直接用sql语句,查询 ...
- 【angular5项目积累总结】列表多选样式框(2)
view code list.css :host { display: flex; width: 100%; border-left: 1px solid #ccc; font: normal 12p ...
- mac terminal中快捷移动光标 持续更新。。。
1.option + ←/→ 以单词为单位快速移动 2.ctrl + A 移动到行首 3.ctrl + B 移动到行尾 4.ctrl + K 删除光标后至行尾的内容
- 小白学习之Code First(一)
1.根据自己的理解,Code First :通过实体类和相关配置生成对应的数据库,实现实体和数据库的映射关系,或通过实体类和相关配置与已经生成的实体与已经存在的数据库搭建映射关系 例: 实体类:Stu ...
- js控制表格隔行变色
只是加载时候隔行变一个颜色,鼠标滑动上去时候没有变化 <table width="800" border="0" cellpadding="0& ...