C#实现在应用程序间发送消息的方法示例
发布时间 - 2026-01-11 02:01:06 点击率:次本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《WinForm控件用法总结》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#常见控件用法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。
# C#
# 应用程序
# 发送消息
# C#微信公众号开发之接收事件推送与消息排重的方法
# C#微信接口之推送模板消息功能示例
# C#推送信息到APNs的方法
# C#实现百度ping推送功能的方法
# C#微信开发之发送模板消息
# C#开发之微信小程序发送模板消息功能
# C#向无窗口的进程发送消息
# C#实现推送钉钉消息的方法示例
# 第一个
# 第二个
# 程序设计
# 上有
# 相关内容
# 感兴趣
# 数据结构
# 给大家
# 可以使用
# 更多关于
# 所述
# 对话框
# 命令行
# 使用技巧
# 面向对象
# 操作技巧
# 讲述了
# SuspendLayout
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
网站优化排名时,需要考虑哪些问题呢?
Java解压缩zip - 解压缩多个文件或文件夹实例
Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】
长沙企业网站制作哪家好,长沙水业集团官方网站?
jquery插件bootstrapValidator表单验证详解
在线制作视频网站免费,都有哪些好的动漫网站?
Laravel如何保护应用免受CSRF攻击?(原理和示例)
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控
JavaScript如何实现倒计时_时间函数如何精确控制
javascript基本数据类型及类型检测常用方法小结
如何在万网主机上快速搭建网站?
Laravel如何创建自定义Artisan命令?(代码示例)
如何快速搭建安全的FTP站点?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
Laravel怎么使用Collection集合方法_Laravel数组操作高级函数pluck与map【手册】
Python文件操作最佳实践_稳定性说明【指导】
Android中AutoCompleteTextView自动提示
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
Laravel如何为API编写文档_Laravel API文档生成与维护方法
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
Laravel怎么上传文件_Laravel图片上传及存储配置
实例解析Array和String方法
如何彻底删除建站之星生成的Banner?
Laravel怎么使用Session存储数据_Laravel会话管理与自定义驱动配置【详解】
Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】
网站制作企业,网站的banner和导航栏是指什么?
Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】
如何打造高效商业网站?建站目的决定转化率
香港服务器租用每月最低只需15元?
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
Laravel如何实现本地化和多语言支持?(i18n教程)
佛山企业网站制作公司有哪些,沟通100网上服务官网?
Windows11怎样设置电源计划_Windows11电源计划调整攻略【指南】
免费网站制作appp,免费制作app哪个平台好?
如何用5美元大硬盘VPS安全高效搭建个人网站?
JavaScript中的标签模板是什么_它如何扩展字符串功能
如何快速登录WAP自助建站平台?
在centOS 7安装mysql 5.7的详细教程
在Oracle关闭情况下如何修改spfile的参数
Laravel如何使用Seeder填充数据_Laravel模型工厂Factory批量生成测试数据【方法】
Laravel定时任务怎么设置_Laravel Crontab调度器配置
如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)
iOS中将个别页面强制横屏其他页面竖屏
大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
如何在不使用负向后查找的情况下匹配特定条件前的换行符
Swift开发中switch语句值绑定模式
php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】
百度输入法ai组件怎么删除 百度输入法ai组件移除工具

