winform與webform互相通訊實(shí)例_第1頁(yè)
winform與webform互相通訊實(shí)例_第2頁(yè)
winform與webform互相通訊實(shí)例_第3頁(yè)
winform與webform互相通訊實(shí)例_第4頁(yè)
winform與webform互相通訊實(shí)例_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、.NET中winform與webform互相通訊實(shí)例,CS調(diào)用BS頁(yè)面的JS函數(shù)時(shí)間:2010-05-1521:38來(lái)源:作者:點(diǎn)擊:71次大家都知道.NET可以開(kāi)發(fā)winform與webform頁(yè)面,有時(shí)候在開(kāi)發(fā)項(xiàng)目過(guò)程中要結(jié)合BS+C范樣來(lái)應(yīng)用,那么本站做一個(gè)實(shí)例來(lái)測(cè)試winform與webform互相通訊的實(shí)例,下面先看下效果:winform調(diào)用bs頁(yè)面的js函數(shù)效果圖webform頁(yè)面發(fā)送信息到winform效果圖好了,看完大家都知道.NET可以開(kāi)發(fā)winform與webform頁(yè)面,有時(shí)候在開(kāi)發(fā)項(xiàng)目過(guò)程中要結(jié)合BS+CS樣來(lái)應(yīng)用,那么本站做一個(gè)實(shí)例來(lái)測(cè)試winform與webform

2、互相通訊的實(shí)例,下面先看下效果:呵Fomil|回丄迫謝試調(diào)用£NET中vinfarmSebfomi互相通訊實(shí)傍I”WWW(學(xué)IT網(wǎng)歡迎你訪|可)CallWinformMethedwinform調(diào)用bs頁(yè)面的js函數(shù)效果圖珅Forml口”回可測(cè)試調(diào)用兀NET中winfbnniwebfbrm互相通訊實(shí)例-wwwxueitcom(學(xué)TT網(wǎng)歡迎你訪|可)IICallWinformMethedwebform頁(yè)面發(fā)送信息到winform效果圖好了,看完上面的效果,下面我們看下如何實(shí)現(xiàn)吧。第一、打開(kāi)VS2008創(chuàng)建winform項(xiàng)目,之后在MainForm拖入瀏覽器控件,并命令這個(gè)瀏覽器控件名為:

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;/這句很關(guān)鍵,主要和頁(yè)面的JS互相操作UriuriSale=newSystem.Uri("http:/localhost:8012/index.htm");/瀏覽器控件默認(rèn)打開(kāi)頁(yè)面WebContainer.Url=uriSale;/<summary>/菜單點(diǎn)擊事件/</summary>/<paramname="sender"x/p

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

6、l(stringstrShow)MessageBox.Show(strShow);好,做完winform,下面是http:/localhost:8012/index.htm頁(yè)面的做法。第2、webform的頁(yè)面,源碼很簡(jiǎn)單,你可以直接復(fù)制源代碼到本地測(cè)試就可以了。下面是HTML頁(yè)面源代碼:Code<!DOCTYPEHTMLPUBLIC"-/W3C/DTDHTML4.0Transitional/EN"><html><head><title>Testjsevent</title><scriptIanguage=&

7、quot;javascript"type="text/javascript"><!-functionfn_test()alert("Hello,cs調(diào)用JS成功-學(xué)it網(wǎng)functionfn_call()window.external.JavascriptCall("bs發(fā)送信息至Uwinform成功");-></script></head><body>NET中winform與webform互相通訊實(shí)例-(學(xué)IT網(wǎng)歡迎你訪問(wèn))<inputtype="button&q

8、uot;value="CallWinformMethed"onclick="fn_call()"/></body></html>嗯!到現(xiàn)在為此,所有操作都可以了,非常簡(jiǎn)單,如果你有興趣來(lái)按上面的代碼來(lái)測(cè)試下在.net中利用webbrowser控件實(shí)現(xiàn)WinForm與WebForm交互在.net中的WebBrowser控件可以讓你裝載WindowsForm應(yīng)用程序中的Web網(wǎng)頁(yè)和其它采用瀏覽器的文件??梢允褂脀ebbrowser控件將現(xiàn)有的web框架控制項(xiàng)加入至WindowsForm客戶端應(yīng)用程序。還是直接看代碼吧。WebBr

9、owser控制項(xiàng)提供的屬性、方法和事件,可用來(lái)實(shí)現(xiàn)InternetExplorer的控制項(xiàng)webBrowser1.Navigate("");/將指定位置處的文件載入至WebBrowserwebBrowser1.GoBack();/上一頁(yè)webBrowser1.GoForward();/下一頁(yè)webBrowser1.Refresh();/刷新webBrowser1.GoHome();/主頁(yè)這里提供了WebBrowser常用的方法,上面的代碼是將我們園子的主頁(yè)載入到WebBrowser控件中。如果我們想要在應(yīng)用程式中產(chǎn)生自己的網(wǎng)頁(yè)內(nèi)容,可以設(shè)定DocumentText屬性。也可

10、以通過(guò)Document屬性來(lái)處理目前的網(wǎng)頁(yè)內(nèi)容。如下代碼是使用DocumentText屬性,顯示網(wǎng)頁(yè)內(nèi)容。并用Document屬性來(lái)處理所顯示的網(wǎng)頁(yè)。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既然我們可以通過(guò)DocumentText生成自己的網(wǎng)頁(yè),那么能不能象使用IE那樣操作這個(gè)網(wǎng)頁(yè)呢?,答案是肯定的,完全可以像操作Web程序那樣操作WebBrowser控制項(xiàng)。比如我們可以加入腳本,CSS。當(dāng)然,如果你熟悉HTML物件對(duì)象模型(DOM),也可以透過(guò)Document屬性來(lái)處理目前的Web網(wǎng)頁(yè)內(nèi)容。下面的例子加入了JavaScript腳本來(lái)控制網(wǎng)頁(yè)。如果要在Winfrom程序中寫(xiě)大量的Javascriot代碼,而且這些代碼最終要轉(zhuǎn)換成String型載入到Webbrowser那將是很痛苦的事情,不過(guò)

14、沒(méi)有關(guān)系,我們可以創(chuàng)建一個(gè)js文件,放入資源中,用的時(shí)候只需從資源中載入即可。這里我創(chuàng)建一個(gè)名為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下的控制項(xiàng),也就是意味著它是用在WimForm程序下,那么WebWrower所載入的Web頁(yè)面如何實(shí)現(xiàn)在WinForm程序下處理呢。例如上例中的"<fontonclick='ClickEvent(Name.value)'>ClickHere</font>"。這里的Click事件是通過(guò)腳本處理的,如何讓這個(gè)Click事件在Winform中處理呢?這里要做一些修改。若要從指令碼存取用戶端應(yīng)用

18、程式,需要設(shè)定ObjectForScripting屬性。指令碼可以將您指定的物件當(dāng)做window.external物件來(lái)存取。使用ObjectForScripting屬性,可啟用WebBrowser控制項(xiàng)所裝載之Web網(wǎng)頁(yè)與包含WebBrowser控制項(xiàng)之應(yīng)用程式間的通訊。這個(gè)屬性可讓您整合動(dòng)態(tài)超文字標(biāo)記語(yǔ)言(DHTML)程式碼與用戶端應(yīng)用程式程式碼。指定給這個(gè)屬性的物件可讓W(xué)eb網(wǎng)頁(yè)指令碼做為window.external物件,這個(gè)物件是為了存取主應(yīng)用程式而提供的內(nèi)建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屬性設(shè)置為this。注意:在From1類(lèi)的開(kāi)頭加入了這么一句ComVisible(true),它在System.Runtime.InteropServices下,預(yù)設(shè)值為true,指出Managed型別對(duì)于COM為可見(jiàn)的。ComVisible(true)publicpartialclassForm1:System.Windows.Forms.Form結(jié)束語(yǔ):本文簡(jiǎn)單的介紹了WebBrowser的用法,實(shí)現(xiàn)了WinForm程序下與Web頁(yè)面的交互使用。Lid關(guān)于在WINFOR中,如果向一個(gè)JSP文件提交數(shù)據(jù),緊急求

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

26、rivateSystem.Windows.Forms.Buttonbutton1;privateAxSHDocVw.AxWebBrowseraxWebBrowser1;/<summary>/必需的設(shè)計(jì)器變量。/</summary>privateSystem.ComponentModel.Containercomponents=null;publicForm3()/Windows窗體設(shè)計(jì)器支持所必需的/InitializeComponent();/TODO:在InitializeComponent調(diào)用后添加任何構(gòu)造函數(shù)代碼/<summary>/清理所有正在使用

27、的資源。/</summary>protectedoverridevoidDispose(booldisposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);#regionWindows窗體設(shè)計(jì)器生成的代碼/<summary>設(shè)計(jì)器支持所需的方法此方法的內(nèi)容。</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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論