C#实现在应用程序间发送消息的方法示例
本文实例讲述了C#实现在应用程序间发送消息的方法。分享给大家供大家参考,具体如下:
首先建立两个C#应用程序项目。
第一个项目包含一个WindowsForm(Form1),在Form1上有一个Button和一个TextBox。
第二个项目包含一个WindowsForm(Form1),在Form1上有两个Button,分别用来测试第一个应用程序中Button的Click事件和修改第一个应用程序中TextBox的值。
第一个应用程序中Form的代码如下:
usingSystem; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; publicclassForm1:System.Windows.Forms.Form{ privateSystem.Windows.Forms.Buttonbutton1; privateSystem.Windows.Forms.TextBoxtextBox1; privateSystem.ComponentModel.Containercomponents=null; [STAThread] staticvoidMain(){ Application.Run(newForm1()); } publicForm1() { InitializeComponent(); } protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } #regionWindows窗体设计器生成的代码 privatevoidInitializeComponent() { this.button1=newSystem.Windows.Forms.Button(); this.textBox1=newSystem.Windows.Forms.TextBox(); this.SuspendLayout(); // //button1 // this.button1.Location=newSystem.Drawing.Point(32,24); this.button1.Name="button1"; this.button1.TabIndex=0; this.button1.Text="button1"; this.button1.Click+=newSystem.EventHandler(this.button1_Click); // //textBox1 // this.textBox1.Location=newSystem.Drawing.Point(32,64); this.textBox1.Name="textBox1"; this.textBox1.TabIndex=1; this.textBox1.Text="textBox1"; // //Form1 // this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14); this.ClientSize=newSystem.Drawing.Size(292,266); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name="Form1"; this.Text="Form1"; this.ResumeLayout(false); } #endregion privatevoidbutton1_Click(objectsender,System.EventArgse){ MessageBox.Show("Thisisbutton1click!"); } }
第二个应用程序中Form的代码如下:
usingSystem; usingSystem.Text; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Runtime.InteropServices; publicclassTestForm1:System.Windows.Forms.Form{ privateSystem.Windows.Forms.Buttonbutton1; privateSystem.Windows.Forms.Buttonbutton2; privateSystem.ComponentModel.Containercomponents=null; [STAThread] staticvoidMain(){ Application.Run(newTestForm1()); } publicTestForm1() { InitializeComponent(); } protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } #regionWindows窗体设计器生成的代码 privatevoidInitializeComponent() { this.button1=newSystem.Windows.Forms.Button(); this.button2=newSystem.Windows.Forms.Button(); this.SuspendLayout(); // //button1 // this.button1.Location=newSystem.Drawing.Point(32,24); this.button1.Name="button1"; this.button1.TabIndex=0; this.button1.Text="button1"; this.button1.Click+=newSystem.EventHandler(this.button1_Click); // //button2 // this.button2.Location=newSystem.Drawing.Point(32,64); this.button2.Name="button2"; this.button2.TabIndex=0; this.button2.Text="button2"; this.button2.Click+=newSystem.EventHandler(this.button2_Click); // //TestForm1 // this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14); this.ClientSize=newSystem.Drawing.Size(292,266); this.Controls.Add(this.button1); this.Controls.Add(this.button2); this.Name="TestForm1"; this.Text="TestForm1"; this.ResumeLayout(false); } #endregion privatevoidbutton1_Click(objectsender,System.EventArgse){ IntPtrhwnd_win; IntPtrhwnd_button; hwnd_win=FindWindow("WindowsForms10.Window.8.app3","Form1"); hwnd_button=FindWindowEx(hwnd_win,newIntPtr(0),"WindowsForms10.BUTTON.app3","button1"); constintBM_CLICK=0x00F5; Messagemsg=Message.Create(hwnd_button,BM_CLICK,newIntPtr(0),newIntPtr(0)); PostMessage(msg.HWnd,msg.Msg,msg.WParam,msg.LParam); } privatevoidbutton2_Click(objectsender,System.EventArgse){ constintWM_CHAR=0x0102; IntPtrhwnd_win; IntPtrhwnd_textbox; hwnd_win=FindWindow("WindowsForms10.Window.8.app3","Form1"); hwnd_textbox=FindWindowEx(hwnd_win,newIntPtr(0),"WindowsForms10.EDIT.app3","textBox1"); stringstrtext="测试aaa"; UnicodeEncodingencode=newUnicodeEncoding(); char[]chars=encode.GetChars(encode.GetBytes(strtext)); Messagemsg; foreach(charcinchars){ msg=Message.Create(hwnd_textbox,WM_CHAR,newIntPtr(c),newIntPtr(0)); PostMessage(msg.HWnd,msg.Msg,msg.WParam,msg.LParam); } } [DllImport("user32.dll")] publicstaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName); [DllImport("user32.dll")] publicstaticexternIntPtrFindWindowEx(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass,stringlpszWindow); [DllImport("user32.dll",CharSet=CharSet.Unicode)] publicstaticexternIntPtrPostMessage(IntPtrhwnd,intwMsg,IntPtrwParam,IntPtrlParam); }
以上代码可以在VS.NET中编译运行,也可以使用csc.exe编译,如使用一下命令行:
F:>csc.exeForm1.cs F:>csc.exeTestForm1.cs
编译后生成两个.exe文件。
首先运行第一个程序,显示Form1窗体,然后运行第二个程序,显示TestForm1窗体。
在TestForm1窗体上点击button1按钮(向Form1窗体上的button1发送消息)此时显示对话框提示“Thisisbutton1click!”。
在TestForm1窗体上点击button2按钮(向Form1窗体上的textBox1发送消息)此时在Form1上的textBox1上显示“测试aaa”。
更多关于C#相关内容感兴趣的读者可查看本站专题:《WinForm控件用法总结》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#常见控件用法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。