【使用chrome浏览器】使用C#在应用程序之间发送消息
【shitiku.jxxyjl.com--软件水平学习指南】
首先建立两个c#应用程序项目。第一个项目包含一个windows form(form1),在form1上有一个button和一个textbox。
第二个项目包含一个windows form(form1),在form1上有两个button,分别用来测试第一个应用程序中button的click事件和修改第一个应用程序中textbox的值。
第一个应用程序中form的代码如下:
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 windows 窗体设计器生成的代码
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.button1_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 button1_click(object sender, system.eventargs e) {
messagebox.show("this is button1 click!");
}
}
第二个应用程序中form的代码如下:
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 窗体设计器生成的代码
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 = "测试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);
}
以上代码可以在vs.net中编译运行,也可以使用csc.exe编译,如使用一下命令行:
f:>csc.exe form1.cs
f:>csc.exe testform1.cs
编译后生成两个.exe文件。
首先运行第一个程序,显示form1窗体,然后运行第二个程序,显示testform1窗体。
在testform1窗体上点击button1按钮(向form1窗体上的button1发送消息)此时显示对话框提示“this is button1 click!”。
在testform1窗体上点击button2按钮(向form1窗体上的textbox1发送消息)此时在form1上的textbox1上显示“测试aaa”。
首先建立两个c#应用程序项目。
第一个项目包含一个windows form(form1),在form1上有一个button和一个textbox。
第二个项目包含一个windows form(form1),在form1上有两个button,分别用来测试第一个应用程序中button的click事件和修改第一个应用程序中textbox的值。
第一个应用程序中form的代码如下:
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 windows 窗体设计器生成的代码
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.button1_click);
//
// textbox1
本文来源:https://shitiku.jxxyjl.com/ruanjianshuipingxuexizhinan/49055.html
-
445端口常见漏洞|常见端口的作用、漏洞和操作建议(2)详细阅读
21端口:21端口主要用于ftp(file transfer protocol,文件传输协议)服务。 端口说明:21端口主要用于ftp(file transfer protocol,文件传输协议)服务,ftp服务主要是为了在两台计算机之间实现文件的上传与下载,一台计算机作为ftp客户...
-
如何使用打印机_如何使用ADO.NET轻松操纵数据库详细阅读
ado net提供了connection来连接数据库,同时也提供了command对象来查询数据库。同connection对象一样,command也有两种:oledbcommand和sqlcommand 其区别同connection对象。 要操纵数据库,必须先使用connect...
-
软考考前卖答案_软考考前心理调整八种方法详细阅读
随软考的临近,不知道是不是有考生会很紧张,出现以下症状:对考试产生害怕、忧虑、不安等情绪,对考试结果缺乏信心,精神处于高度紧张状态,有时还伴有头痛、失眠、食欲不振、脾气暴躁、焦躁不安、注意力不集中、头脑发木等反应,甚至对考试产生严重的恐惧感。这就是我们常说的考试焦虑。心理学研究表明,...
-
[程序员逻辑思维培养]职业程序员培养之道详细阅读
软件开发是以人为核心的过程,对人的依赖性远高于传统的硬件生产企业,为了保持开发能力的稳定性,一方面需要定义软件过程,以过程为枢纽将人、技术、工具衔接起来,另一方面也要加强人才的培养,使人的工作能力能够稳定、提高人员的自治性。随着社会需求的膨胀,对程序员的需求量、对熟练的程序员的需求量在剧...
-
【指针万用表的使用方法】C++指针使用方法解惑详细阅读
c++指针使用方法解惑 void clearlist(lnode * & hl) 仔细看一下这种声明方式,确实有点让人迷惑。 下面以 void func1( myclass *&pbuildingelement ); 为例来说明这个问题。在某种意义上,*和&是意思相对的两个东西,把它们放在...
-
【ctoc模式什么意思】CtoC++新手指南详细阅读
c++技术固然是很时髦的,许多c用户都想在尽可能短的时间内为自己贴上c++的标签。介绍c++的书很多,但只有那些已经侥幸入门的用户才偶尔去翻翻,仍有不少在c++门口徘徊的流浪汉。 本文只针对c用户,最好是一位很不错的老用户(譬如他在遇到最简单的问题时都尝试着使用指针),通过一些c和更好的c++(本文...
-
线性表的顺序实现_使用C++实现线性表的基本功能详细阅读
include include include typedef int elemtype; 线性表的基本操作void initiallist(elemtype *l); int isempty(elemtype *l);void l...
-
计算机软件水平考试与计算机四级|计算机软件水平考试:《数据结构》是核心详细阅读
从2004年起,计算机与软件考试纳入全国专业技术人员职业资格证书制度的统一规划,报考任何级别的考生都不受学历、资历限制,这使得该考试报考人数骤增。而20%通过率无疑使该考试的竞争非常激烈,怎样能顺利通过考试已成为广大参考人员的热门话题。 为了让考生更好的备考,我们特走访了多位已经通过考试的朋...
-
软考 软考_软考专家谈软考趋势和复习要点详细阅读
2005 年上半年全国计算机技术与软件专业技术资格(水平)考试(简称软考)报考人数最多的专业级别是软件设计师和程序员。这两个级别是软件考试中最经典、考题最稳定和考生最多的级别,同时也是社会认同度最高的两个级别。近年来,随着信息技术的发展,考试内容的重点已作较大调整,考试更能体现出技术的进...
-
[路由器与交换机的主要区别]深入了解路由器与交换机的区别3详细阅读
2 负载集中:交换机之间只能有一条通路,使得信息集中在一条通信链路上,不能进行动态分配,以平衡负载。而路由器的路由协议算法可以避免这一点,ospf路由协议算法不但能产生多条路由,而且能为不同的网络应用选择各自不同的最佳路由。 3 广播控制:交换机只能缩小冲突域,而不能缩小广播域。整个交换...