首先建立两个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

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

F:>csc.exe TestForm1.cs

编译后生成两个.exe文件。

首先运行第一个程序,显示Form1窗体,然后运行第二个程序,显示TestForm1窗体。

在TestForm1窗体上点击button1按钮(向Form1窗体上的button1发送消息)此时显示对话框提示“This is button1 click!”。

在TestForm1窗体上点击button2按钮(向Form1窗体上的textBox1发送消息)此时在Form1上的textBox1上显示“测试aaa”。

使用C#在应用程序间发送消息相关推荐

  1. 微信 php post json,微信企业号:如何POST JSON数据发送消息给企业号成员

    根据企业号开发者文档的发送消息接口消息数据格式,要想向企业号成员发送消息必须用POST方式将JSON数据发送到指定的包含ACCESS_TOKEN的URL. 我想实现的是每隔一段时间间隔,查询数据库之后 ...

  2. 小程序向webview传参_微信小程序(1)——web-view和小程序间传递参数、发送消息...

    小程序向web-view发送消息 在组件中有一个属性src(src是webview 指向网页的链接.可打开关联的公众号的文章,其它网页需登录小程序管理后台配置业务域名.) 通过设置src中GET参数即 ...

  3. 2022.8.31 进程中无名管道的特点,无名管道的创建,为何无名管道只能能够实现具有亲缘关系的进程间的通信,以及实现利用无名管道父进程给子进程发送消息的完整代码。

    无名管道通信 无名管道特点: (1):只能用于具有亲缘关系的进程之间的通信.(父子进程或兄弟进程) (2):是一个半双工的通信模式,具有固定的读端和写端.(fd[0]固定为读端,fd[1]固定为写端) ...

  4. 微信小程序接入NFC,使用HCE模拟主机卡完成NFC刷卡发送消息

    NFC相信大家都很熟悉,现实中经常使用的门禁卡,公交卡,地铁卡,饭卡等都是采用NFC功能,那么你知道吗,NFC也可以用微信小程序来实现.使用微信小程序可以读取/写入让手机成为一个刷卡器,也可以使用微信 ...

  5. VB SendMessage向其他程序窗口发送字符串消息实例

    以下通过程序的标题来获取该窗口的句柄,然后通过SendMessage函数给该窗口句柄发送WM_SETTEXT消息. 代码如下: '先创建一个文本文档,命名为: 1.txt ' 然后打开测试... Pr ...

  6. 应用程序委托协议栈发送消息

    数据收发操作概览 知道了IP地址之后,就可以委托操作系统内部的协议栈向这个目标IP地址,也就是我们要访问的Web服务器发送消息了. 和向DNS服务器查询IP地址的操作一样,这里也需要使用Socket库 ...

  7. 微信小程序订阅消息定时发送消息

    微信小程序订阅消息定时发送消息 本人专注使用云开发,实现一个前端可以做后端以及整个项目的部署与上线. 如果觉得我讲的好就可以给我点个赞.也可以加我微信了解详情. 1.我们先要了解什么是订阅消息 而现在 ...

  8. 微信小程序之发送通知消息(通过openid推送消息给用户)

    微信小程序之发送通知消息(通过openid推送消息给用户) 一.获取access_token access_token是接口调用的凭证,目前有效期为两个小时,需要定时刷新,重复获取将导致上次获取的ac ...

  9. 关于 使用python向qq好友发送消息(对爬虫的作用----当程序执行完毕或者报错无限给自己qq发送消息,直到关闭)...

    以前看到网上一些小程序,在处理完事物后会自动发送qq消息,但是一直搞不懂是说明原理.也在网上找过一些python登陆qq发送消息的文字,但是都太复杂了.今天偶然看到一篇文章,是用python调用win ...

最新文章

  1. 设计模式之_工厂系列_01
  2. “约见”面试官系列之常见面试题之第八十六篇之nexttick(建议收藏)
  3. 通过怒气系统的hongjin2
  4. 构造函数必须是public吗_谈谈 constructor 的private和public
  5. 联想x100e linux,进化之道!ThinkPad X100e全球首发测试
  6. JS获取HTML video标签视频第一帧
  7. Activity的Launch mode详解 singleTask正解
  8. 小马Win7永久激活工具—OemY3.1 NT6通用完美激活
  9. Chrom安装Axure插件浏览原型图
  10. AdventureWorks安装问题总结 exe安装
  11. coverity java_coverity检测不到代码 | 学步园
  12. Springboot毕设项目基于批示的督查督办管理系统c6m0djava+VUE+Mybatis+Maven+Mysql+sprnig)
  13. NAIPC2016 I. Tourists【LCA】
  14. QT实现OPC_UA客户端程序以及与OPC_UA服务器通信
  15. php mcv,swolle http mcv设计问题
  16. A3A1自助申请PHP版搭建,微软全局子号-微软全局订阅A3桌面版office账号自助申请程序下载php版-西西软件下载...
  17. pwm调速流程图小车_51单片机 小车 L298N pwm调速 串口控制 按键控制
  18. NHANES数据库的介绍及使用(一)
  19. YDOOK: Python3 IPC 进程间通信方法分类总结
  20. 情绪调节的自适应_自我情绪调节的几种方法

热门文章

  1. 78oa mysql_78OA系统安装后无法打开解决方案
  2. 前端人最需要的学习资料有哪些?我现在免费送给你!
  3. 前端大屏幕项目的一点思考
  4. babel需要这样配置
  5. 14.最长公共前缀-LeetCode
  6. 无SSH工具部署网站到火腿云
  7. centos 访问网页重启php_php项目上线基于docker运行php+源码编译实现Nginx+阿里云RDS连接实现...
  8. img之间出现缝隙的原因_神马情况?美缝剂施工出现脱胶是什么原因?
  9. 20210801:AXI-Lite总线逻辑与关键源码分析
  10. 机场精细化管理_全国首家!西安咸阳国际机场通过民航局安全管理体系专项审核...