




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.NET中winform与webform互相通讯实例,CS调用BS页面的JS函数时间:2010-05-15 21:38 来源: 作者: 点击:71次 大家都知道.NET可以开发winform与webform页面,有时候在开 发项目过程中要结合BS+CS这样来应用,那么本站做一个实例来测试winform与webform互相通讯的实例,下面先看下效果: winform调用bs页面的js函数效果图 webform页面发送信息到winform效果图 好了,看完大家都知道.NET可以开发winform与webform页面,有时候在开发项目过程中要结合BS+CS这样来应用,那么本站做一个实例来测试winf
2、orm与webform互相通讯的实例,下面先看下效果: winform调用bs页面的js函数效果图1 / 15 webform页面发送信息到winform效果图好了,看完上面的效果,下面我们看下如何实现吧。第一、打开VS2008创建winform项目,之后在MainForm拖入浏览器控件,并命令这个浏览器控件名为:WebContainer下面是全部CS端代码: /* * * 名称:CS与BS互相通讯 * 作者:cc * 官方: */ using System;using System.Co
3、llections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TestJSWin System.Runtime.InteropServices.ComVisibleAttribute(true) public partial class MainForm : Form
4、60; public MainForm() InitializeComponent(); this.WebContainer.ObjectF
5、orScripting = this; /这句很关键,主要和页面的JS互相操作 Uri uriSale = new System.Uri("http:/localhost:8012/index.htm"); /浏览器控件默认打开页面 WebContainer.Url = uriSale;
6、160; / <summary> / 菜单点击事件 / </summary> / <param name="sender"></
7、param> / <param name="e"></param> private void jsEventToolStripMenuItem_Click(object sender, EventArgs e) &
8、#160; WebContainer.Navigate("javascript:fn_test();void(0);"); / <summary> / BS调用方法 / </summary
9、> / <param name="strShow"></param> public void JavascriptCall(string strShow) Mess
10、ageBox.Show(strShow); 好,做完winform,下面是http:/localhost:8012/index.htm页面的做法。 第2、webform的页面,源码很简单,你可以直接复制源代码到本地测试就可以了。下面是HTML页面源代码: Code <!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.0 Transitional/EN"><html>
11、60; <head> <title>Test js event</title> <script language="javascript" type="text/javascript"> <!-
12、 function fn_test() alert("Hello, cs调用JS成功-学it网
13、 function fn_call() window.external.JavascriptCall("bs发送信息到winform成功");
14、-> </script> </head> <body> NET中winform与webform互相通讯实例-(学IT网欢迎你访问) <input type="button" value="Call Winform Methed" onclick="fn_call()&quo
15、t; /> </body></html>嗯!到现在为此,所有操作都可以了,非常简单,如果你有兴趣来按上面的代码来测试下在.net中利用webbrowser控件实现WinForm与WebForm交互在.net中的WebBrowser 控件可以让你装载Windows Form 应用程序中的Web 网页和其它采用浏览器的文件。可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Form 客户端应用程序。还是直接看代码吧。WebBrowser 控制项 提供的属性、方法和事件,可用来实现 Internet E
16、xplorer 的控制项 webBrowser1.Navigate(""); /将指定位置处的文件载入至 WebBrowser webBrowser1.GoBack();/上一页 webBrowser1.GoForward();/下一页 webBrowser1.Refresh();/刷新 webBrowser1.GoHome();/主页这里提供了WebBrows
17、er常用的方法, 上面的代码是将 我们园子的主页载入到 WebBrowser控件中。如果我们想要在应用程式中产生自己的网页内容,可以设定DocumentText属性。也可以通过Document属性来处 理目前的网页内容。如下代码是使用 DocumentText 属性,显示网页内容。并用Document属性来处理所显示的网页。 1private void btnDocumentText_Click(object sender, EventArgs e) 2
18、 3 string szHtml = " 4<HTML> 5<HEAD> 6<TITLE> DocumentText </TITLE> 7</HEAD> 8 9<BODY>10 Please enter your name:<br/>11
19、60; <input type='text' name='Name'/><br/>12 <a href='' >Send input to method of Form class</a>13 14</BODY>15</HTML>"17
20、60; webBrowser1.DocumentText = szHtml;18 19 2021 private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)22
21、0; 23 System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;2425 if (document != null && document.All"Name" != null
22、 && String.IsNullOrEmpty(document.All"Name".GetAttribute("value")26 27 e.Cancel = true;28
23、60; System.Windows.Forms.MessageBox.Show("You must enter your name before you can navigate to " + e.Url.ToString();29 3031
24、 既 然我们可以通过DocumentText生成自己的网页,那么能不能象使用IE那样操作这个网页呢?,答案是肯定的,完全可以像操作Web程序那样操作 WebBrowser 控制项。比如我们可以加入脚本,CSS。当然,如果你熟悉 HTML 物件对象模型 (DOM),也可以透过 Document 属性来处理目前的Web网页内容。下面的例子加入了JavaScript脚本来控制网页。如果要在Winfrom程序中写大量的Javascriot代 码,而且这些代码最终要转换成String型载入到Webbrowser 那将是很痛苦的事情,不过没有关系,我们可以创建一个js文件,放入资源中,用的时候
25、只需从资源中载入即可。这里我创建一个名为 ClientScript.js 的文件。 1<script language = "javascript"> 2function ClickEvent(name) 3 4 alert("Hello: " +name); 5 6 7function KeyDown() 8 9 if (event.keyCode=116)10
26、60; 11 event.keyCode=0;12 event.returnValue=false;13 14 15 return false;16string szClientScript = ManagedWebBrowser.Properties.Resources.Resourc
27、eManager.GetString("ClientScript"); string szWebBrowserText = "<html>" + "<head>" + &
28、#160; "<title></title>"+ &
29、#160; szClientScript + "</head>" + "<body onkeydown="KeyDown()" oncontextm
30、enu="event.returnValue=false">"+ "Please enter your name:<br/>"+
31、60; "<input type='text' name='Name'/><br/>"+ "<font onclick = 'ClickEvent(Name.value)'>
32、Click Here</font>"+ "</body></html>" webBrowser1.DocumentText = szWebBrowserText;WebBrowser 是 System.Windows.Fo
33、rms 下的控制项,也就是意味着它是用在WimForm程序下,那么WebWrower所载入的Web页面如何实现在WinForm程序下处理呢。例如上例中的 "<font onclick = 'ClickEvent(Name.value)'>Click Here</font>" 。这里的Click事件是通过脚本处理的,如何让这个Click事件在Winform中处理呢?这里要做一些修改。若要从指令码存取用户端应用程式,需要 设定ObjectForScripting 属性。指令码可以将您指定的物件当做window.external 物件来存取
34、。 使用ObjectForScripting属性,可启用 WebBrowser 控制项所装载之 Web 网页与包含 WebBrowser 控制项之应用程式间的通讯。这个属性可让您整合动态超文字标记语言 (DHTML) 程式码与用户端应用程式程式码。指定给这个属性的物件可让 Web 网页指令码做为 window.external 物件,这个物件是为了存取主应用程式而提供的内建 DOM 物件。 1 private void btnScriptEvent_Click(object sender, EventArgs e) 2
35、0; 3 4 / This is the handler for loading the script into the Web Browser control and allowing us to interact 5 / between the script in
36、 the Browser control and this form class 8 / Set the ObjectForScripting property of the Web Browser control to point to this form class 9 / This will allow us
37、 to interact with methods in this form class via the window.external property 10 webBrowser1.ObjectForScripting = this;1112 string szWebBrowserText = "<html>
38、" +13 "<head>" +14 "<title></title>"+
39、0; 15 "</head>" +16 "<bo
40、dy onkeydown="KeyDown()" oncontextmenu="event.returnValue=false">"+17 18 "Please enter your name:&l
41、t;br/>"+19 "<input type='text' name='Name'/><br/>"+20 "&l
42、t;font onClick='window.external.ClickEvent(Name.value)'>Click Here</font>"+21 "</body></html>"222324
43、 webBrowser1.DocumentText = szWebBrowserText;25 26 public void ClickEvent(string userName)27 28 / Simply ech
44、o out the name that the user typed in the input box of the HTML page29 if (System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft = true)30
45、 MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);31
46、 else32 MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);3334
47、0; 这里的ObjectForScripting 属性设置为 this。注意:在From1 类的开头加入了这么一句ComVisible(true), 它在System.Runtime.InteropServices下,预设值为 true,指出 Managed 型别对于 COM 为可见的。 ComVisible(true) public partial class Form1 : System.Windows.Forms.Form结束语:本文简单的介绍了 WebBrowser 的用法,实现了WinForm
48、程序下与Web页面的交互使用。关于在WINFORM中,如果向一个JSP文件提交数据,紧急求救中。using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace test / <summary> / Form3 的摘要说明。 / </summary&
49、gt; public class Form3 : System.Windows.Forms.Form private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; private AxSHDocVw.AxWebBrowser
50、160; axWebBrowser1; / <summary> / 必需的设计器变量。 / </summary> private System.ComponentModel.Container components = null; public Form3() / / Windows 窗体设计器支持所必需的 / InitializeComponent(); / / TODO: 在 Initi
51、alizeComponent 调用后添加任何构造函数代码 / / <summary> / 清理所有正在使用的资源。 / </summary> protected override void Dispose( bool disposing ) if( disposing ) if(components != null) components.Dispose(); base.Dis
52、pose( disposing ); #region Windows 窗体设计器生成的代码 / <summary> / 设计器支持所需的方法 - 不要使用代码编辑器修改 / 此方法的内容。 / </summary> private void InitializeComponent() System.Resources.ResourceManager resources =
53、160; new System.Resources.ResourceManager(typeof(Form3); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.axWebBrowser1
54、 = new AxSHDocVw.AxWebBrowser(); (System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).BeginInit(); this.SuspendLayout(); / / textBox1 / this.textBox1.Location = new System.Drawing.Point(16, 8); this.textBox1.Name =
55、 "textBox1 " this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1 " / / textBox2 / this.textBox2.Location = new System.Drawing.Point(128, 8); this.textBox2.Name = "textBox
56、2 " this.textBox2.PasswordChar = '* ' this.textBox2.TabIndex = 1; this.textBox2.Text = "textBox2 " / / button1 / this.button1.Location = new System.Drawing.Point(240, 8); this.button1.Na
57、me = "button1 " this.button1.TabIndex = 2; this.button1.Text = "button1 " this.button1.Click += new System.EventHandler(this.button1_Click); / / axWebBrowser1 / this.axWebBrowser1.Enabled = true; this.axWebBrowser1.Location = new System.Drawing.Point(8, 40); this.axWebBrowser1.OcxState = (System.Windows.Forms.AxHost.State)(resources.GetObject( "axWebBrowser1.OcxState "); this.axWebBrowser1.Size = new
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电镀工时间节点把控考核试卷及答案
- 钽电解电容器赋能、被膜工节能降耗执行考核试卷及答案
- 粮食作物栽培工岗位合规化技术规程
- 公司平地机操作工岗位设备技术规程
- 2025个人之间的房屋租赁合同
- 2025铝材购销合同书
- 2026届山西省吕梁地区文水县数学七年级第一学期期末综合测试模拟试题含解析
- 2025年是否可以随时解除委托合同
- 2025物流企业船舶委托经营合同
- 2025石油购销合同正式样式
- 2025贵州盐业(集团)遵义有限责任公司招聘15人笔试备考试题及答案解析
- EMS供应商对比方案报告
- 神奇的加密术教学设计-2025-2026学年初中数学北师大版2024八年级上册-北师大版2024
- 价格波动对利润影响分析-洞察及研究
- 广西检测协会试题(钢结构检测综合)判断题(1-375)
- 茶评员职业技能竞赛考试题库-下(多选、判断题部分)
- 2025年重庆文化旅游集团有限公司招聘笔试参考题库含答案解析
- 北京中医药大学宣讲
- 2025春江苏开放大学大学英语(B)(1)060051过程性考核作业3参考答案
- DB37-T 5310-2025《城镇排水管渠养护维修服务规范》
- 三年级下册32《每天自省五分钟》心理健康教学设计
评论
0/150
提交评论