使用C#在应用程序之间发送消息.docx
《使用C#在应用程序之间发送消息.docx》由会员分享,可在线阅读,更多相关《使用C#在应用程序之间发送消息.docx(11页珍藏版)》请在冰豆网上搜索。
![使用C#在应用程序之间发送消息.docx](https://file1.bdocx.com/fileroot1/2023-7/9/51ce670d-22b0-4cbf-95ef-46f3f74ca366/51ce670d-22b0-4cbf-95ef-46f3f74ca3661.gif)
使用C#在应用程序之间发送消息
使用C#在应用程序之间发送消息
使用C#在应用程序之间发送消息使用C#在应用程序之间发送消息首先建立两个c#应用程序项目。
第一个项目包含一个windowsform(form1),在form1上有一个button和一个textbox。
第二个项目包含一个windowsform(form1),在form1上有两个button,分别用来测试第一个应用程序中button的click事件和修改第一个应用程序中textbox的值。
第一个应用程序中form的代码如下:
usingsystem;
usingsystem.drawing;
usingsystem.collections;
usingponentmodel;
usingsystem.windows.forms;
publicclassform1:
system.windows.forms.form{
privatesystem.windows.forms.buttonbutton1;
privatesystem.windows.forms.textboxtextbox1;
privateponentmodel.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;
usingponentmodel;
usingsystem.windows.forms;
usingsystem.runtime.interopservices;
publicclasstestform1:
system.windows.forms.form{
privatesystem.windows.forms.buttonbutton1;
privatesystem.windows.forms.buttonbutton2;
privateponentmodel.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);
}
以上代码可以在中编译运行,也可以使用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#应用程序项目。
第一个项目包含一个windowsform(form1),在form1上有一个button和一个textbox。
第二个项目包含一个windowsform(form1),在form1上有两个button,分别用来测试第一个应用程序中button的click事件和修改第一个应用程序中textbox的值。
第一个应用程序中form的代码如下:
usingsystem;
usingsystem.drawing;
usingsystem.collections;
usingponentmodel;
usingsystem.windows.forms;
publicclassform1:
system.windows.forms.form{
privatesystem.windows.forms.buttonbutton1;
privatesystem.windows.forms.textboxtextbox1;
privateponentmodel.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使用C#在应用程序之间发送消息相关内容: