C#实现在应用程序间发送消息的方法示例
本文实例讲述了C#实现在应用程序间发送消息的方法。分享给大家供大家参考,具体如下:
首先建立两个C#应用程序项目。
第一个项目包含一个Windows Form(Form1),在Form1上有一个Button和一个TextBox。
第二个项目包含一个Windows Form(Form1),在Form1上有两个Button,分别用来测试第一个应用程序中Button的Click事件和修改第一个应用程序中TextBox的值。
第一个应用程序中Form的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
[STAThread]
static void Main() {
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e) {
MessageBox.Show("This is button1 click!");
}
}
复制代码
第二个应用程序中Form的代码如下:
using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class TestForm1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
[STAThread]
static void Main() {
Application.Run(new TestForm1());
}
public TestForm1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(32, 64);
this.button2.Name = "button2";
this.button2.TabIndex = 0;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// TestForm1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Name = "TestForm1";
this.Text = "TestForm1";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e) {
IntPtr hwnd_win ;
IntPtr hwnd_button ;
hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_button = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.BUTTON.app3","button1");
const int BM_CLICK = 0x00F5;
Message msg = Message.Create(hwnd_button ,BM_CLICK ,new IntPtr(0),new IntPtr(0));
PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam);
}
private void button2_Click(object sender, System.EventArgs e) {
const int WM_CHAR = 0x0102;
IntPtr hwnd_win ;
IntPtr hwnd_textbox ;
hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_textbox = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.EDIT.app3","textBox1");
string strtext = "测试aaa";
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(strtext));
Message msg ;
foreach (char c in chars ) {
msg = Message.Create(hwnd_textbox ,WM_CHAR ,new IntPtr(c),new IntPtr(0));
PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam);
}
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
[DllImport("user32.dll",CharSet=CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);
}
复制代码
以上代码可以在VS.NET中编译运行,也可以使用csc.exe编译,如使用一下命令行:
F:>csc.exe Form1.cs
F:>csc.exe TestForm1.cs
复制代码
编译后生成两个.exe文件。
首先运行第一个程序,显示Form1窗体,然后运行第二个程序,显示TestForm1窗体。
在TestForm1窗体上点击button1按钮(向Form1窗体上的button1发送消息)此时显示对话框提示“This is button1 click!”。
在TestForm1窗体上点击button2按钮(向Form1窗体上的textBox1发送消息)此时在Form1上的textBox1上显示“测试aaa”。
希望本文所述对大家C#程序设计有所帮助。
(编辑:雷林鹏 来源:网络)
C#实现在应用程序间发送消息的方法示例的更多相关文章
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- Delphi实现获取句柄并发送消息的方法(FindWindow、FindWindowEx、EnumChildWindows、SendMessage)
Delphi实现获取句柄并发送消息的方法 本文以实例形式详细说明了Delphi获取句柄并发送消息的方法,具体用法说明如下: 查找另外一个窗口的句柄: handle := FindWindow(nil, ...
- Winform 程序嵌入WPF程序 并发送消息
废话不多说,先看解决方案目录 WindowsFormsDemo是主程序,WpfApp是嵌入的WPF程序,先看WPF程序,程序默认启动的页面是MainWindow.xaml,这里注释掉App.xaml里 ...
- 原创:【微信小程序】发送消息模板教程(后台以PHP示例)
1.本教程对外开放,未经博主同意,禁止转载. 2.准备材料:1)公众号|小程序,添加选择的模板消息,2)在设置>开发设置页面,开通消息模板功能:如: 3.因为调用微信发送模板的接口是:https ...
- Delphi窗体间发送消息或字符串
在Delphi 开发中,常常应用到窗体消息传递,以达成某种操作要求,以下列举一个应用的例子,供大家参考. 自定义过程/函数方法://发送字符串到指字句柄的窗口中 (接收窗体需用发送时的消息常量WM_C ...
- linux上 用户间发送消息 通信
联想:scp命令 1.使用write命令,向指定用户发送信息: 用户可以使用write命令给其他在线用户发送消息.格式: $ write zhangsan hello worl ...
- 增加线程异步发送消息的方法二(Runnable)
//获取当前时间:毫秒 long a = System.currentTimeMillis(); System.out.println("a :" + a); try { //更改 ...
- 用Win32编写发送消息至Notepad++的程序
这次利用Win32编程写一个发送"Win32 Assembly,My First SendMessage Program !" 每个程序要发送消息至另一个程序的时候,通常使用Sen ...
- 如何使用GameObject类发送消息
一.GameObject发送消息的方法 GameObject类有三个方法可以实现发送消息,即SendMessage.BroadcastMessage和SendMessageUpwards.但是它们之间 ...
随机推荐
- 如何将TNJ的源代码添加到eclipse[转]
java编程思想第四版中net.mindview.util的jar包导入 在Java编程思想第四版中需要使用net.mindview.util包,大家可以直接到http://www.mindviewi ...
- iphone6 inline-flex兼容问题
在编写微信端页面时,遇到这样的问题:position属性为flex的导航栏,其li标签在其余设备上显示正常,但在iphone6上浮动错误. 究其原因,是iphone6不支持position属性中的fl ...
- 9大行为导致Java程序员薪资过低, 你有几个?
Java程序员薪水有高有低,有的人一个月可能拿30K.50K,有的人可能只有2K.3K.同样有五年工作经验的Java程序员,可能一个人每月拿20K,一个拿5K.是什么因素导致了这种差异?本文整理导致J ...
- Python入门之软件开发目录规范
本章重点: 理解在开发人标准软件时,如何布局项目目录结构,以及注意开发规范的重要性. 一.为什么要有好的目录结构 二.目录组织的方式 三.关于README的内容 四.关于requirements.tx ...
- Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin)
Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin) 一,查看是否安装jdk: # rpm -qa | grep jdk 或者 #rpm -q jdk 或者 # ...
- Linux 进程学习笔记
1.什么是程序?什么是进程?它们有什么区别? 定义: 程序:程序(Program)是一个静态的命令集合,程序一般放在磁盘中,然后通过用户的执行来触发.触发后程序会加载到内存中成为一个个体,就是进程. ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 2 章 答案
判断对错1.编写程序的好方法是立即键入一些代码,然后调试它,直到它工作.2.可以在不使用编程语言的情况下编写算法.3.程序在写入和调试后不再需要修改.4.Python 标识符必须以字母或下划线开头.5 ...
- sqlite3 的一些整理和补充
一,sqlite3数据库打开时的返回值及其所代表的含义 返回值 描述 返回值 描述 SQLITE_OK=0 返回成功 SQLITE_FULL=13 数据库满,插入失败 SQLITE_ERROR=1 S ...
- python装饰器,其实就是对闭包的使用。
装饰器 理解装饰器要先理解闭包(在闭包中引用函数,可参考上一篇通过例子来理解闭包). 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 装饰器的实质就是对闭包的使用,原函数被 ...
- 高通平台启动log概述(PBL log、sbl1 log、kernel log)【转】
本文转自:https://blog.csdn.net/RadianceBlau/article/details/78416776?utm_source=blogxgwz9 高通平台启动log概述(PB ...