最近项目中需要做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消息的更多相关文章

  1. c#让窗体永在最前 调用windows api 将窗体设为topmost

    有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...

  2. 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 ...

  3. 1.java soap api操作和发送soap消息

    转自:https://blog.csdn.net/lbinzhang/article/details/84721359 1. /** * soap请求 * * @return * @throws Ex ...

  4. c# 判断窗体是否永在最前(TopMost),调用windows API

    许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...

  5. WINDOWS API 大全(二)

    9. API之设备场景函数 CombineRgn 将两个区域组合为一个新区域CombineTransform 驱动世界转换.它相当于依顺序进行两次转换CreateCompatibleDC 创建一个与特 ...

  6. 在VBA中使用Windows API

    VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...

  7. WinSpy涉及的windows api

    WinSpy涉及的windows api WinSpy是仿造微软Spy++的开源项目,但只涉及Spy++的窗口句柄.窗口的属性.styles.类名子窗口.进程线程信息等查找功能.功能虽然不算强大,但涉 ...

  8. Windows API的消息处理机制

    上个学期找实习失利,让我觉得自己基础打得不够牢固,所以在华为实习的这三个月里,每天下班都在复习理论课的知识,顺便刷了一个月的 LeetCode.本来以为找工作是势在必得了,结果这个学期秋季校招的坑爹经 ...

  9. C# Windows Api的一些方法 封装 以及 常用参数

    using System;using System.Collections.Generic;using System.Drawing;using System.Diagnostics;using Sy ...

随机推荐

  1. 通过数据库绑定的dropdownlist,如何让其第一条默认显示"--请选择--"

    第一种方法 DropDownList1.Items.Insert(0,"请选择XXX"); 第二种方法 在第一个位置插入一个项就可以 DropDownList1.Items.Ins ...

  2. VS2015编译Boost1.64

    一.下载并解压:boost1.64.0:http://www.boost.org/users/history/version_1_64_0.html 二.以管理员权限运行VS2015命令行工具 三.c ...

  3. linux 和 windows 安装composer

    在 Linux 和 Mac OS X 中可以运行如下命令: curl -sS https://getcomposer.org/installer | phpmv composer.phar /usr/ ...

  4. windows下python2.7版本numpy,Scipy,matplotlib,sklearn安装

    系统是windows32位,安装了python2.7.13. 安装顺序就是numpy,Scipy,matplotlib,sklearn. 首先是更新一下pip (确保pip能使用) 然后将setupt ...

  5. nodejs记录2——一行代码实现文件下载

    主要使用fs模块的pipe方法,简单粗暴: import fs from "fs"; import path from 'path'; import request from 'r ...

  6. C#制作手机网站

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> //在 ...

  7. [编程] C语言Linux系统编程-等待终止的子进程(僵死进程)

    1.等待终止的子进程(僵死进程): 如果一个子进程在父进程之前结束,内核会把子进程设置为一个特殊的状态,处于这种状态的进程称为僵死进程 当父进程获取了子进程的信息后,子进程才会消失. pid_t wa ...

  8. [javaEE] 控制浏览器缓存资源

    浏览器有默认的缓存机制,不同的浏览器,缓存头是不一样的 设置编码,调用setContentType()方法,参数:”text/html;charset=utf-8” 关闭缓存,调用setHeader( ...

  9. [javaSE] GUI(图形用户界面)

    java为gui提供的对象都在java.Awt和javax.Swing包中 Awt:抽象窗口工具包,依赖平台,调用系统的本地方法 Swing:完全由java实现的轻量级工具库 eclipse官方封装了 ...

  10. 九、双端队列LinkedBlockDeque

    一.简介 JDK通过BlockQueue阻塞队列实现了生产者-消费者模式,生产者向队列添加数据,消费者从队列里面消费数据. 但是在有些场景里面,我们是无法区分生产者消费者的,或者说既是生产者,也是消费 ...