Use C # To send messages between applications

Source: Internet
Author: User

First, create two C # application projects.

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

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

The code for 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 (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
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. button#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 button#click (object sender, System. EventArgs e ){
MessageBox. Show ("This is button1 click! ");
}
}

In the second application, the Form code 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 code generated by Windows Form Designer
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. button#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 button#click (object sender, System. EventArgs e ){
IntPtr hwnd_win;
IntPtr hwnd_button;

Hwnd_win = FindWindow ("WindowsForms10.Window. 8. app3", "Form1 ");
Hwnd_button = find1_wex (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 = find1_wex (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 find1_wex (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 and run in vs.net, or compiled using csc.exe, as shown in the following command line:

F:> csc.exe Form1.cs
F:> csc.exe TestForm1.cs

Generate two. EXE files after compilation.

Run the first program, display the Form1 form, and then run the second program to display the TestForm1 form.

Click the button1 button on the TestForm1 form (send a message to the button1 on the Form1 form). The "This is button1 click!" dialog box is displayed !".

On the TestForm1 form, click the button2 button (send a message to textBox1 on the Form1 form). Then, "test aaa" is displayed on textBox1 on the Form1 form ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.