使用C#在应用程序间发送消息

  1. 首先建立两个C#应用程序项目。
  2. 第一个项目包含一个Windows Form(Form1),在Form1上有一个Button和一个TextBox。
  3. 第二个项目包含一个Windows Form(Form1),在Form1上有两个Button,分别用来测试第一个应用程序中Button的Click事件和修改第一个应用程序中TextBox的值。
  4. 第一个应用程序中Form的代码如下:
  5. using System;
  6. using System.Drawing;
  7. using System.Collections;
  8. using System.ComponentModel;
  9. using System.Windows.Forms;
  10. public class Form1 : System.Windows.Forms.Form {
  11. private System.Windows.Forms.Button button1;
  12. private System.Windows.Forms.TextBox textBox1;
  13. private System.ComponentModel.Container components = null;
  14. [STAThread]
  15. static void Main() {
  16. Application.Run(new Form1());
  17. }
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. protected override void Dispose( bool disposing )
  23. {
  24. if( disposing )
  25. {
  26.    if(components != null)
  27.    {
  28.     components.Dispose();
  29.    }
  30. }
  31. base.Dispose( disposing );
  32. }
  33. #region Windows 窗体设计器生成的代码
  34. private void InitializeComponent()
  35. {
  36. this.button1 = new System.Windows.Forms.Button();
  37. this.textBox1 = new System.Windows.Forms.TextBox();
  38. this.SuspendLayout();
  39. // 
  40. // button1
  41. // 
  42. this.button1.Location = new System.Drawing.Point(32, 24);
  43. this.button1.Name = "button1";
  44. this.button1.TabIndex = 0;
  45. this.button1.Text = "button1";
  46. this.button1.Click += new System.EventHandler(this.button1_Click);
  47. // 
  48. // textBox1
  49. // 
  50. this.textBox1.Location = new System.Drawing.Point(32, 64);
  51. this.textBox1.Name = "textBox1";
  52. this.textBox1.TabIndex = 1;
  53. this.textBox1.Text = "textBox1";
  54. // 
  55. // Form1
  56. // 
  57. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  58. this.ClientSize = new System.Drawing.Size(292, 266);
  59. this.Controls.Add(this.textBox1);
  60. this.Controls.Add(this.button1);
  61. this.Name = "Form1";
  62. this.Text = "Form1"
  63. this.ResumeLayout(false);
  64. }
  65. #endregion
  66. private void button1_Click(object sender, System.EventArgs e) {
  67. MessageBox.Show("This is button1 click!");
  68. }
  69. }
  70. 第二个应用程序中Form的代码如下:
  71. using System;
  72. using System.Text;
  73. using System.Drawing;
  74. using System.Collections;
  75. using System.ComponentModel;
  76. using System.Windows.Forms;
  77. using System.Runtime.InteropServices;
  78. public class TestForm1 : System.Windows.Forms.Form {
  79. private System.Windows.Forms.Button button1;
  80. private System.Windows.Forms.Button button2;
  81. private System.ComponentModel.Container components = null;
  82. [STAThread]
  83. static void Main() {
  84. Application.Run(new TestForm1());
  85. }
  86. public TestForm1()
  87. {
  88. InitializeComponent();
  89. }
  90. protected override void Dispose( bool disposing )
  91. {
  92. if( disposing )
  93. {
  94.    if(components != null)
  95.    {
  96.     components.Dispose();
  97.    }
  98. }
  99. base.Dispose( disposing );
  100. }
  101. #region Windows 窗体设计器生成的代码
  102. private void InitializeComponent()
  103. {
  104. this.button1 = new System.Windows.Forms.Button();
  105. this.button2 = new System.Windows.Forms.Button();
  106. this.SuspendLayout();
  107. // 
  108. // button1
  109. // 
  110. this.button1.Location = new System.Drawing.Point(32, 24);
  111. this.button1.Name = "button1";
  112. this.button1.TabIndex = 0;
  113. this.button1.Text = "button1";
  114. this.button1.Click += new System.EventHandler(this.button1_Click);
  115. // 
  116. // button2
  117. // 
  118. this.button2.Location = new System.Drawing.Point(32, 64);
  119. this.button2.Name = "button2";
  120. this.button2.TabIndex = 0;
  121. this.button2.Text = "button2";
  122. this.button2.Click += new System.EventHandler(this.button2_Click);
  123. // 
  124. // TestForm1
  125. // 
  126. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  127. this.ClientSize = new System.Drawing.Size(292, 266);
  128. this.Controls.Add(this.button1);
  129. this.Controls.Add(this.button2); 
  130. this.Name = "TestForm1";
  131. this.Text = "TestForm1"
  132. this.ResumeLayout(false);
  133. }
  134. #endregion
  135. private void button1_Click(object sender, System.EventArgs e) {
  136. IntPtr hwnd_win ;   
  137. IntPtr hwnd_button ;
  138. hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
  139. hwnd_button = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.BUTTON.app3","button1"); 
  140. const int BM_CLICK = 0x00F5;
  141. Message msg = Message.Create(hwnd_button ,BM_CLICK ,new IntPtr(0),new IntPtr(0));
  142. PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam); 
  143. }
  144. private void button2_Click(object sender, System.EventArgs e) {
  145. const int WM_CHAR = 0x0102;
  146. IntPtr hwnd_win ;
  147. IntPtr hwnd_textbox ;
  148. hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");   
  149. hwnd_textbox = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.EDIT.app3","textBox1");     
  150. string strtext = "测试aaa";
  151. UnicodeEncoding encode = new UnicodeEncoding();
  152. char[] chars = encode.GetChars(encode.GetBytes(strtext));
  153. Message msg ;
  154. foreach (char c in chars ) {
  155.    msg = Message.Create(hwnd_textbox ,WM_CHAR ,new IntPtr(c),new IntPtr(0));
  156.    PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam); 
  157. }
  158. }
  159. [DllImport("user32.dll")]
  160. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  161. [DllImport("user32.dll")]
  162. public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
  163. [DllImport("user32.dll",CharSet=CharSet.Unicode)] 
  164. public static extern IntPtr PostMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);
  165. }
  166. 以上代码可以在VS.NET中编译运行,也可以使用csc.exe编译,如使用一下命令行:
  167. F:>csc.exe Form1.cs
  168. F:>csc.exe TestForm1.cs
  169. 编译后生成两个.exe文件。
  170. 首先运行第一个程序,显示Form1窗体,然后运行第二个程序,显示TestForm1窗体。
  171. 在TestForm1窗体上点击button1按钮(向Form1窗体上的button1发送消息)此时显示对话框提示“This is button1 click!”。
  172. 在TestForm1窗体上点击button2按钮(向Form1窗体上的textBox1发送消息)此时在Form1上的textBox1上显示“测试aaa”。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值