Send messages between applications using C #

zhaozj2021-02-16  130

Send messages between applications using C #

Author: kongxx

First build two C # application projects.

The first item contains a Windows Form (Form1) with a Button and a TextBox on Form1.

The second project contains Windows Form (Form1), two Button on Form1, which is used to test the Click event of Button in the first application and modify the value of TextBox in the first application.

The code of the FORM in the first application is as follows:

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 (component! = Null) {components.dispose ();}}}}} #region;} #Region Windows Form Designer Generated Code 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. EventHandle R (this.button1_click); // // textBox1 // this.TextBox1.Location = new system.drawing.point (32, 64); this.textBox1.name = "textbox1"; this.TextBox1.tabindex = 1; THISTBOX1.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!");}} The code of the FORM in the second application is as follows:

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 Form Designer Generated Code 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 =" test 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);} The above code can be compiled in VS.NET, or CSC.exe can be compiled, such as using the command line:

F:> csc.exe form1.cs

F:> csc.exe testform1.cs

Two .exe files are generated after compiling.

First run the first program, display the FORM1 form, then run the second program, display the TestForm1 form.

Click the Button1 button on the TestForm1 form (send message to the Button1 on the Form1) Show dialog prompt "this is button1 click!".

Click the Button2 button on the TestForm1 form (send message to the TEXTBOX1 on the Form1) This time the "Test AAA" is displayed on the textbox1 on Form1.

转载请注明原文地址:https://www.9cbs.com/read-8662.html

New Post(0)