winform與webform互相通訊實例_第1頁
winform與webform互相通訊實例_第2頁
winform與webform互相通訊實例_第3頁
winform與webform互相通訊實例_第4頁
winform與webform互相通訊實例_第5頁
已閱讀5頁,還剩6頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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拖入瀏覽器控件,并命令這個瀏覽器控件名為:WebCon

3、tainer下面是全部CS端代碼:/官方:/chjun2000/*/usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem丄inq;usingSystem.Text;usingSystem.Windows.Forms;*名稱:CS與BS互相通訊作者:ccnamespaceTestJSWinSystem.Runtime.InteropServices.ComVisibleAttribute(true)publicp

4、artialclassMainForm:FormpublicMainForm()InitializeComponent();this.WebContainer.ObjectForScripting=this;/這句很關鍵,主要和頁面的JS互相操作UriuriSale=newSystem.Uri(http:/localhost:8012/index.htm);/瀏覽器控件默認打開頁面WebContainer.Url=uriSale;/菜單點擊事件/privatevoidjsEventToolStripMenultem_Click(objectsender,EventArgse)WebContai

5、ner.Navigate(javascript:fn_test();void(0););/BS調用方法/publicvoidJavascriptCall(stringstrShow)MessageBox.Show(strShow);好,做完winform,下面是HYPERLINK http:/localhost:8012/index.htmhttp:/localhost:8012/index.htm頁面的做法。第2、webform的頁面,源碼很簡單,你可以直接復制源代碼到本地測試就可以了。下面是HTML頁面源代碼:CodeHYPERLINK TestjseventNET中winform與web

6、form互相通訊實例-(學IT網歡迎你訪問)嗯!到現在為此,所有操作都可以了,非常簡單,如果你有興趣來按上面的代碼來測試下在.net中利用webbrowser控件實現WinForm與WebForm交互在.net中的WebBrowser控件可以讓你裝載WindowsForm應用程序中的Web網頁和其它采用瀏覽器的文件。可以使用webbrowser控件將現有的web框架控制項加入至WindowsForm客戶端應用程序。還是直接看代碼吧。WebBrowser控制項提供的屬性、方法和事件,可用來實現InternetExplorer的控制項webBrowser1.Navigate(HYPERLINK )

7、;/將指定位置處的文件載入至WebBrowserwebBrowser1.GoBack();/上一頁webBrowser1.GoForward();/下一頁webBrowser1.Refresh();/刷新webBrowser1.GoHome();/主頁這里提供了WebBrowser常用的方法,上面的代碼是將我們園子的主頁載入到WebBrowser控件中。如果我們想要在應用程式中產生自己的網頁內容,可以設定DocumentText屬性。也可以通過Document屬性來處理目前的網頁內容。如下代碼是使用DocumentText屬性,顯示網頁內容。并用Document屬性來處理所顯示的網頁。1pri

8、vatevoidbtnDocumentText_Click(objectsender,EventArgse)stringszHtml=456DocumentText789Pleaseenteryourname:SendinputtomethodofFormclass131415;webBrowser1.DocumentText=szHtml;181920privatevoidwebBrowser1_Navigating(objectsender,WebBrowserNavigatingEventArgse)System.Windows.Forms.HtmlDocumentdocument=th

9、is.webBrowser1.Document;24if(document!=null&document.AllName!=null&String.IsNullOrEmpty(document.AllName.GetAttribute(value)e.Cancel=true;System.Windows.Forms.MessageBox.Show(Youmustenteryournamebeforeyoucannavigateto+e.Url.ToString();3031既然我們可以通過DocumentText生成自己的網頁,那么能不能象使用IE那樣操作這個網頁呢?,答案是肯定的,完全可以像

10、操作Web程序那樣操作WebBrowser控制項。比如我們可以加入腳本,CSS。當然,如果你熟悉HTML物件對象模型(DOM),也可以透過Document屬性來處理目前的Web網頁內容。下面的例子加入了JavaScript腳本來控制網頁。如果要在Winfrom程序中寫大量的Javascriot代碼,而且這些代碼最終要轉換成String型載入到Webbrowser那將是很痛苦的事情,不過沒有關系,我們可以創建一個js文件,放入資源中,用的時候只需從資源中載入即可。這里我創建一個名為ClientScript.js的文件。12functionClickEvent(name)3alert(Hello:

11、+name);567functionKeyDown()8if(event.keyCode=116)event.keyCode=0;event.returnValue=false;1415returnfalse;16stringszClientScript=ManagedWebBrowser.Properties.Resources.ResourceManager.GetString(ClientScript);stringszWebBrowserText=+szClientScript+Pleaseenteryourname:+ClickHere+;webBrowser1.DocumentTe

12、xt=szWebBrowserText;WebBrowser是System.Windows.Forms下的控制項,也就是意味著它是用在WimForm程序下,那么WebWrower所載入的Web頁面如何實現在WinForm程序下處理呢。例如上例中的ClickHere。這里的Click事件是通過腳本處理的,如何讓這個Click事件在Winform中處理呢?這里要做一些修改。若要從指令碼存取用戶端應用程式,需要設定ObjectForScripting屬性。指令碼可以將您指定的物件當做window.external物件來存取。使用ObjectForScripting屬性,可啟用WebBrowser控制

13、項所裝載之Web網頁與包含WebBrowser控制項之應用程式間的通訊。這個屬性可讓您整合動態超文字標記語言(DHTML)程式碼與用戶端應用程式程式碼。指定給這個屬性的物件可讓Web網頁指令碼做為window.external物件,這個物件是為了存取主應用程式而提供的內建DOM物件。1privatevoidbtnScriptEvent_Click(objectsender,EventArgse)234/ThisisthehandlerforloadingthescriptintotheWebBrowsercontrolandallowingustointeract/betweenthescri

14、ptintheBrowsercontrolandthisformclass/SettheObjectForScriptingpropertyoftheWebBrowsercontroltopointtothisformclass/Thiswillallowustointeractwithmethodsinthisformclassviathewindow.externalpropertywebBrowser1.ObjectForScripting=this;1112stringszWebBrowserText=+17Pleaseenteryourname:+ClickHere+;2223web

15、Browser1.DocumentText=szWebBrowserText;publicvoidClickEvent(stringuserName)/SimplyechooutthenamethattheusertypedintheinputboxoftheHTMLpageif(System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft=true)MessageBox.Show(Hello+userName,ManagedWebBrowserSample,MessageBoxButtons.OK,

16、MessageBoxIcon.Information,MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign|MessageBoxOptions.RtlReading);elseMessageBox.Show(Hello+userName,ManagedWebBrowserSample,MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);3334這里的ObjectForScripting屬性設置為this。注意:在Fro

17、m1類的開頭加入了這么一句ComVisible(true),它在System.Runtime.InteropServices下,預設值為true,指出Managed型別對于COM為可見的。ComVisible(true)publicpartialclassForm1:System.Windows.Forms.Form結束語:本文簡單的介紹了WebBrowser的用法,實現了WinForm程序下與Web頁面的交互使用。Lid關于在WINFOR中,如果向一個JSP文件提交數據,緊急求救中usingSystem;usingSystem.Drawing;usingSystem.Collections;

18、usingSystem.ComponentModel;usingSystem.Windows.Forms;namespacetest/Form3的摘要說明。/publicclassForm3:System.Windows.Forms.FormprivateSystem.Windows.Forms.TextBoxtextBox1;privateSystem.Windows.Forms.TextBoxtextBox2;privateSystem.Windows.Forms.Buttonbutton1;privateAxSHDocVw.AxWebBrowseraxWebBrowser1;/必需的設計

19、器變量。/privateSystem.ComponentModel.Containercomponents=null;publicForm3()/Windows窗體設計器支持所必需的/InitializeComponent();/TODO:在InitializeComponent調用后添加任何構造函數代碼/清理所有正在使用的資源。/protectedoverridevoidDispose(booldisposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);#regionWindo

20、ws窗體設計器生成的代碼/設計器支持所需的方法此方法的內容。不要使用代碼編輯器修改privatevoidInitializeComponent()System.Resources.ResourceManagerresourcesger(typeof(Form3);this.textBox1newSystem.Resources.ResourceMananewthis.textBox2newthis.button1newthis.axWebBrowser1System.Windows.Forms.TextBox();System.Windows.Forms.TextBox();System.Wi

21、ndows.Forms.Button();newAxSHDocVw.AxWebBrowser();(System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).BeginInit();this.SuspendLayout();/textBox1/this.textBox1.Locationthis.textBox1.NamenewSystem.Drawing.Point(16,8);textBox1;this.textBox1.TabIndexthis.textBox1.Text=0;textBox1;/textBox2/this

22、.textBox2.LocationnewSystem.Drawing.Point(128,8);textBox2;this.textBox2.PasswordChar=*;this.textBox2.Namethis.textBox2.TabIndex1;this.textBox2.TexttextBox2;/button1/this.button1.Location=newSystem.Drawing.Point(240,8);this.button1.Name=button1;this.button1.TabIndex=2;this.button1.Text=button1;this.b

23、utton1.Click+=newSystem.EventHandler(this.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=newSys

24、tem.Drawing.Size(248,16);this.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.EventHandler(this.Form3_Load);(System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).EndInit();this.ResumeLayout(false);#endregionprivatevoidForm3_Load(objectsender,System.E

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論