winform与webform互相通讯实例_第1页
winform与webform互相通讯实例_第2页
winform与webform互相通讯实例_第3页
winform与webform互相通讯实例_第4页
winform与webform互相通讯实例_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.NET中winform与webform互相通讯实例,CS调用BS页面的JS函数时间:2010-05-1521:38来源:作者:点击:71次大家都知道.NET可以开发winform与webform页面,有时候在开发项目过程中要结合BS+C范样来应用,那么本站做一个实例来测试winform与webform互相通讯的实例,下面先看下效果:winform调用bs页面的js函数效果图webform页面发送信息到winform效果图好了,看完大家都知道.NET可以开发winform与webform页面,有时候在开发项目过程中要结合BS+CS样来应用,那么本站做一个实例来测试winform与webform

2、互相通讯的实例,下面先看下效果:呵Fomil|回丄迫謝试调用£NET中vinfarmSebfomi互相通讯实傍I”WWW(学IT网欢迎你访|可)CallWinformMethedwinform调用bs页面的js函数效果图珅Forml口”回可测试调用兀NET中winfbnniwebfbrm互相通讯实例-wwwxueitcom(学TT网欢迎你访|可)IICallWinformMethedwebform页面发送信息到winform效果图好了,看完上面的效果,下面我们看下如何实现吧。第一、打开VS2008创建winform项目,之后在MainForm拖入浏览器控件,并命令这个浏览器控件名为:

3、WebContainer下面是全部CS端代码:/官方:*/usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem丄inq;usingSystem.Text;usingSystem.Windows.Forms;*名称:CS与BS互相通讯作者:ccnamespaceTestJSWinSystem.Runtime.InteropServices.ComVisibleAttribute(true)publicpartia

4、lclassMainForm:FormpublicMainForm()InitializeComponent();this.WebContainer.ObjectForScripting=this;/这句很关键,主要和页面的JS互相操作UriuriSale=newSystem.Uri("http:/localhost:8012/index.htm");/浏览器控件默认打开页面WebContainer.Url=uriSale;/<summary>/菜单点击事件/</summary>/<paramname="sender"x/p

5、aram>/<paramname="e"></param>privatevoidjsEventToolStripMenultem_Click(objectsender,EventArgse)WebContainer.Navigate("javascript:fn_test();void(0);");/<summary>/BS调用方法/</summary>/<paramname="strShow"></param>publicvoidJavascriptCal

6、l(stringstrShow)MessageBox.Show(strShow);好,做完winform,下面是http:/localhost:8012/index.htm页面的做法。第2、webform的页面,源码很简单,你可以直接复制源代码到本地测试就可以了。下面是HTML页面源代码:Code<!DOCTYPEHTMLPUBLIC"-/W3C/DTDHTML4.0Transitional/EN"><html><head><title>Testjsevent</title><scriptIanguage=&

7、quot;javascript"type="text/javascript"><!-functionfn_test()alert("Hello,cs调用JS成功-学it网functionfn_call()window.external.JavascriptCall("bs发送信息至Uwinform成功");-></script></head><body>NET中winform与webform互相通讯实例-(学IT网欢迎你访问)<inputtype="button&q

8、uot;value="CallWinformMethed"onclick="fn_call()"/></body></html>嗯!到现在为此,所有操作都可以了,非常简单,如果你有兴趣来按上面的代码来测试下在.net中利用webbrowser控件实现WinForm与WebForm交互在.net中的WebBrowser控件可以让你装载WindowsForm应用程序中的Web网页和其它采用浏览器的文件。可以使用webbrowser控件将现有的web框架控制项加入至WindowsForm客户端应用程序。还是直接看代码吧。WebBr

9、owser控制项提供的属性、方法和事件,可用来实现InternetExplorer的控制项webBrowser1.Navigate("");/将指定位置处的文件载入至WebBrowserwebBrowser1.GoBack();/上一页webBrowser1.GoForward();/下一页webBrowser1.Refresh();/刷新webBrowser1.GoHome();/主页这里提供了WebBrowser常用的方法,上面的代码是将我们园子的主页载入到WebBrowser控件中。如果我们想要在应用程式中产生自己的网页内容,可以设定DocumentText属性。也可

10、以通过Document属性来处理目前的网页内容。如下代码是使用DocumentText属性,显示网页内容。并用Document属性来处理所显示的网页。1privatevoidbtnDocumentText_Click(objectsender,EventArgse)2 3 stringszHtml="4<HTML>5<HEAD>6<TITLE>DocumentText</TITLE>7</HEAD>89<BODY>10 Pleaseenteryourname:<br/>11 <inputtype

11、='text'name='Name'/><br/>12 <ahref=''>SendinputtomethodofFormclass</a>1314</BODY>15</HTML>"17 webBrowser1.DocumentText=szHtml;18192021 privatevoidwebBrowser1_Navigating(objectsender,WebBrowserNavigatingEventArgse)22 23 System.Windows.Form

12、s.HtmlDocumentdocument=this.webBrowser1.Document;2425 if(document!=null&&document.All"Name"!=null&&String.IsNullOrEmpty(document.All"Name".GetAttribute("value")26 27 e.Cancel=true;28 System.Windows.Forms.MessageBox.Show("Youmustenteryournamebeforeyo

13、ucannavigateto"+e.Url.ToString();29 3031既然我们可以通过DocumentText生成自己的网页,那么能不能象使用IE那样操作这个网页呢?,答案是肯定的,完全可以像操作Web程序那样操作WebBrowser控制项。比如我们可以加入脚本,CSS。当然,如果你熟悉HTML物件对象模型(DOM),也可以透过Document属性来处理目前的Web网页内容。下面的例子加入了JavaScript脚本来控制网页。如果要在Winfrom程序中写大量的Javascriot代码,而且这些代码最终要转换成String型载入到Webbrowser那将是很痛苦的事情,不过

14、没有关系,我们可以创建一个js文件,放入资源中,用的时候只需从资源中载入即可。这里我创建一个名为ClientScript.js的文件。1<scriptlanguage="javascript">2functionClickEvent(name)34 alert("Hello:"+name);567functionKeyDown()89 if(event.keyCode=116)10 11 event.keyCode=0;12 event.returnValue=false;13 1415returnfalse;16stringszClient

15、Script=ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");stringszWebBrowserText="<html>"+"<head>"+"<title></title>"+szClientScript+"</head>"+"<bodyonkeydown="KeyDown()"onc

16、ontextmenu="event.returnValue=false">"+"Pleaseenteryourname:<br/>"+"<inputtype='text'name='Name'/><br/>"+"<fontonclick='ClickEvent(Name.value)'>ClickHere</font>"+"</body></html>&quo

17、t;webBrowser1.DocumentText=szWebBrowserText;WebBrowser是System.Windows.Forms下的控制项,也就是意味着它是用在WimForm程序下,那么WebWrower所载入的Web页面如何实现在WinForm程序下处理呢。例如上例中的"<fontonclick='ClickEvent(Name.value)'>ClickHere</font>"。这里的Click事件是通过脚本处理的,如何让这个Click事件在Winform中处理呢?这里要做一些修改。若要从指令码存取用户端应用

18、程式,需要设定ObjectForScripting属性。指令码可以将您指定的物件当做window.external物件来存取。使用ObjectForScripting属性,可启用WebBrowser控制项所装载之Web网页与包含WebBrowser控制项之应用程式间的通讯。这个属性可让您整合动态超文字标记语言(DHTML)程式码与用户端应用程式程式码。指定给这个属性的物件可让Web网页指令码做为window.external物件,这个物件是为了存取主应用程式而提供的内建DOM物件。1privatevoidbtnScriptEvent_Click(objectsender,EventArgse)

19、234/ThisisthehandlerforloadingthescriptintotheWebBrowsercontrolandallowingustointeract5 /betweenthescriptintheBrowsercontrolandthisformclass8 /SettheObjectForScriptingpropertyoftheWebBrowsercontroltopointtothisformclass9 /Thiswillallowustointeractwithmethodsinthisformclassviathewindow.externalproper

20、ty10 webBrowser1.ObjectForScripting=this;1112stringszWebBrowserText="<html>"+13 "<head>"+14 "<title></title>"+15 "</head>"+16 "<bodyonkeydown="KeyDown()"oncontextmenu="event.returnValue=false">"

21、+1718 "Pleaseenteryourname:<br/>"+19 "<inputtype='text'name='Name'/><br/>"+20 "<fontonClick='window.external.ClickEvent(Name.value)'>ClickHere</font>"+21 "</body></html>"222324 webBrowser1.Docum

22、entText=szWebBrowserText;25 26 publicvoidClickEvent(stringuserName)27 28 /SimplyechooutthenamethattheusertypedintheinputboxoftheHTMLpage29 if(System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft=true)30 MessageBox.Show("Hello"+userName,"ManagedWebBrowserSample

23、",MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign|MessageBoxOptions.RtlReading);31 else32 MessageBox.Show("Hello"+userName,"ManagedWebBrowserSample",MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefault

24、Button.Button1);3334这里的ObjectForScripting属性设置为this。注意:在From1类的开头加入了这么一句ComVisible(true),它在System.Runtime.InteropServices下,预设值为true,指出Managed型别对于COM为可见的。ComVisible(true)publicpartialclassForm1:System.Windows.Forms.Form结束语:本文简单的介绍了WebBrowser的用法,实现了WinForm程序下与Web页面的交互使用。Lid关于在WINFOR中,如果向一个JSP文件提交数据,紧急求

25、救中usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;namespacetest/<summary>/Form3的摘要说明。/</summary>publicclassForm3:System.Windows.Forms.FormprivateSystem.Windows.Forms.TextBoxtextBox1;privateSystem.Windows.Forms.TextBoxtextBox2;p

26、rivateSystem.Windows.Forms.Buttonbutton1;privateAxSHDocVw.AxWebBrowseraxWebBrowser1;/<summary>/必需的设计器变量。/</summary>privateSystem.ComponentModel.Containercomponents=null;publicForm3()/Windows窗体设计器支持所必需的/InitializeComponent();/TODO:在InitializeComponent调用后添加任何构造函数代码/<summary>/清理所有正在使用

27、的资源。/</summary>protectedoverridevoidDispose(booldisposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);#regionWindows窗体设计器生成的代码/<summary>设计器支持所需的方法此方法的内容。</summary>不要使用代码编辑器修改privatevoidInitializeComponent()System.Resources.ResourceManagerresourcesg

28、er(typeof(Form3);this.textBox1newSystem.Resources.ResourceMananewthis.textBox2newthis.button1newthis.axWebBrowser1System.Windows.Forms.TextBox();System.Windows.Forms.TextBox();System.Windows.Forms.Button();newAxSHDocVw.AxWebBrowser();(System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).Beg

29、inInit();this.SuspendLayout();/textBox1/this.textBox1.Locationthis.textBox1.NamenewSystem.Drawing.Point(16,8);"textBox1"this.textBox1.TabIndexthis.textBox1.Text=0;"textBox1"/textBox2/this.textBox2.LocationnewSystem.Drawing.Point(128,8);"textBox2"this.textBox2.PasswordCh

30、ar='*'this.textBox2.Namethis.textBox2.TabIndex1;this.textBox2.Text"textBox2"/button1/this.button1.Location=newSystem.Drawing.Point(240,8);this.button1.Name="button1"this.button1.TabIndex=2;this.button1.Text="button1"this.button1.Click+=newSystem.EventHandler(thi

31、s.button1_Click);/axWebBrowser1/this.axWebBrowser1.Enabled=true;this.axWebBrowser1.Location=newSystem.Drawing.Point(8,40);this.axWebBrowser1.OcxState=(System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState");this.axWebBrowser1.Size=newSystem.Drawing.Size(248,16);thi

32、s.axWebBrowser1.TabIndex=3;/Form3/this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);this.ClientSize=newSystem.Drawing.Size(336,69);this.Controls.Add(this.axWebBrowser1);this.Controls.Add(this.button1);this.Controls.Add(this.textBox2);this.Controls.Add(this.textBox1);this.Name="Form3"this.Text="Form3"this.Load+=newSystem.Event

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论