1、相关声明函数,SendMessage可定义两种格式。

[DllImport("User32.DLL", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("User32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
[DllImport("User32.DLL")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

2、相关消息

public const uint WM_SETTEXT = 0x000C;

public const uint WM_CHAR = 0x0102;

public const uint EM_SETSEL = 0x00B1;

3、
用spy++观察是哪个窗口处理消息,然后用FindWindow找到窗口句柄
IntPtr main = FindWindow("TFrmMain", null);


自己启动,比如记事本
Process vProcess = Process.Start("notepad.exe");
while (vProcess.MainWindowHandle == IntPtr.Zero) vProcess.Refresh();
注:启动之后可以直接发键值
SendKeys.Send("01234/n");SendKeys.Send("56789/n");


枚举所有进程
Process[] vP2 = Process.GetProcesses();

用FindWindowEx找子窗口句柄,一层层找下去。
IntPtr panel1 = FindWindowEx(main, IntPtr.Zero, "TPanel", null);
IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "TPanel", null);

如果有多个同类子窗口,FindWindowEx第二个参数,控制从哪个子窗口开始找何开始找。
IntPtr edit1 = FindWindowEx(panel2, (IntPtr)null, "TEdit", null);
IntPtr edit2 = FindWindowEx(panel2, edit1, "TEdit", null);

逐个发消息
SendMessage(edit1, WM_CHAR, (uint)Keys.F3, 0);

用循环发
string end="hello";
for(int i=0;i<5;i++)
  SendMessage(edit1, WM_CHAR, (uint)end[i], 0);

发字符串
SendMessage(vHandle, WM_SETTEXT, IntPtr.Zero, "测试/r/n换行");

选中7个文本
SendMessage(vHandle, EM_SETSEL, 0, 7);

C#给其他程序发消息的更多相关文章

  1. 1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory

    1.低权限的程序向高权限的程序发消息 2.慎用setcurrentdirectory

  2. VC++实现Vista和Win7系统低权限程序向高权限程序发消息

    Windows 7已经隆重发布,但是很多程序员已经通过RTM等版本尝到了Windows 7的甜处.那么在Windows 7下用户界面特权隔离,将是本文我们介绍的重点. 我们介绍了操作系统服务的Sess ...

  3. windows服务怎么向应用程序发消息(部署在同一台机,非SCOKET)

    命名管道:NamedPipeClientStream & NamedPipeClientStream 参考实例:http://msdn.microsoft.com/zh-cn/library/ ...

  4. 通过程序调用微信公众号发消息api返回48001

    自己的订阅号,尝试通过写程序来给用户发消息.结果呢,接口返回报错:errcode=48001,errmsg = api unauthorized hint: [ZlPULa02942276!] 去微信 ...

  5. ActiveMQ发消息和收消息

    来自:http://blog.163.com/chengwei_1104/blog/static/53645274201382315625329/ ActiveMQ 是Apache出品,最流行的,能力 ...

  6. Handler详解系列(四)——利用Handler在主线程与子线程之间互发消息,handler详解

    MainActivity如下: package cc.c; import android.app.Activity; import android.os.Bundle; import android. ...

  7. 一行代码设置TForm颜色的前世今生(属性赋值引起函数调用,然后发消息实现改变显示效果),TForm的初始颜色在dfm中设置了clBtnFace色

    来自万一的帖子:http://www.cnblogs.com/del/archive/2008/04/27/1173658.html的确做到了一行代码设置TForm控件的颜色(一点感想:Delphi程 ...

  8. Android应用程序线程消息循环模型分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6905587 我们知道,Android应用程序是 ...

  9. JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题

    部分博客中描述的使用拦截器怎么用EJB公布的WebService加入缓存,这样能够提高WebService的响应效率.但是即使是这样做,还是要经历网络的传输的.于是决定在调用WebService的程序 ...

随机推荐

  1. [WPF系列]-数据邦定之DataTemplate简介

    引言 WPF 数据模板化模型为定义数据的表示形式提供了很大的灵活性.WPF 控件有支持自定义数据表示形式的内置功能.首先介绍下如何定义Datatemplate,然后再介绍其他数据模板化功能,例如根据自 ...

  2. Bootstrap 按钮

    本章将通过实例讲解如何使用 Bootstrap 按钮.任何带有 class .btn 的元素都会继承圆角灰色按钮的默认外观.但是 Bootstrap 提供了一些选项来定义按钮的样式,具体如下表所示: ...

  3. Learn Python The Hard Way ex41中的程序

    import random from urllib import urlopen import sys WORD_URL = "http://learncodethehardway.org/ ...

  4. CentOS安装MySQL

    好记性不如烂笔头,记录一下 yum list installed | grep mysql #检查是否安装了mysql yum -y remove mysql-libs.x86_64 #卸载已经安装的 ...

  5. 使用python实现appium的屏幕滑动

    前些日子写一个滑动手机页面的小脚本,看到大家给的内容都是swipe方法,这里对swipe方法做一个小介绍: Swipe(int start x,int start y,int end x,int y, ...

  6. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  7. nodejs express 静态文件的路径

    当express 设置为静态文件服务器的时候.可以通过2种方式进行方位: 1,通过设置app.use('路径1','../a/b/image') express 路径的形式,如 src="路 ...

  8. VirtualBox装ghost XP

    在win7 professional 64上安装了virtualBox4.3.14 r95030 版本,之所以要安装这个vb,是因为刚升级的vm 打开之后很占用cpu, 网上又说vb不是很占用cpu而 ...

  9. 基于Calabash-andriod的UI自动化测试(1)-环境和原理

    有时候,一些覆盖happy path的UI自动化还是很有用的.它的一些作用如下: 1.可以迅速实现端到端的功能回归,能够覆盖接口测试覆盖不到的一些地方,如GUI层和接口层的交互产生的问题. 2.非码农 ...

  10. 【腾讯GAD暑期训练营游戏程序班】游戏中的物理系统作业说明文档

    一.需求分析• 添加一辆新NPC车,可以让其与主角车碰撞:• 添加一些新物件,能够与车互动,在其触发事件将其移除:• 添加一些无法撞动的事件:• 添加NPC车的自动移动逻辑:• 在课上赛车的示例上添加 ...