




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、.NET中winform與webform互相通訊實例,CS調(diào)用BS頁面的JS函數(shù)時間:2010-05-15 21:38 來源: 作者: 點擊:71次 大家都知道.NET可以開發(fā)winform與webform頁面,有時候在開 發(fā)項目過程中要結(jié)合BS+CS這樣來應(yīng)用,那么本站做一個實例來測試winform與webform互相通訊的實例,下面先看下效果: winform調(diào)用bs頁面的js函數(shù)效果圖 webform頁面發(fā)送信息到winform效果圖 好了,看完大家都知道.NET可以開發(fā)winform與webform頁面,有時候在開發(fā)項目過程中要結(jié)合BS+CS這樣來應(yīng)用,那么本站做一個實例來測試winf
2、orm與webform互相通訊的實例,下面先看下效果: winform調(diào)用bs頁面的js函數(shù)效果圖1 / 15 webform頁面發(fā)送信息到winform效果圖好了,看完上面的效果,下面我們看下如何實現(xiàn)吧。第一、打開VS2008創(chuàng)建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; /這句很關(guān)鍵,主要和頁面的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調(diào)用方法 / </summary
9、> / <param name="strShow"></param> public void JavascriptCall(string strShow) Mess
10、ageBox.Show(strShow); 好,做完winform,下面是http:/localhost:8012/index.htm頁面的做法。 第2、webform的頁面,源碼很簡單,你可以直接復(fù)制源代碼到本地測試就可以了。下面是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調(diào)用JS成功-學(xué)it網(wǎng)
13、 function fn_call() window.external.JavascriptCall("bs發(fā)送信息到winform成功");
14、-> </script> </head> <body> NET中winform與webform互相通訊實例-(學(xué)IT網(wǎng)歡迎你訪問) <input type="button" value="Call Winform Methed" onclick="fn_call()&quo
15、t; /> </body></html>嗯!到現(xiàn)在為此,所有操作都可以了,非常簡單,如果你有興趣來按上面的代碼來測試下在.net中利用webbrowser控件實現(xiàn)WinForm與WebForm交互在.net中的WebBrowser 控件可以讓你裝載Windows Form 應(yīng)用程序中的Web 網(wǎng)頁和其它采用瀏覽器的文件??梢允褂脀ebbrowser 控件將現(xiàn)有的web框架控制項加入至 Windows Form 客戶端應(yīng)用程序。還是直接看代碼吧。WebBrowser 控制項 提供的屬性、方法和事件,可用來實現(xiàn) Internet E
16、xplorer 的控制項 webBrowser1.Navigate(""); /將指定位置處的文件載入至 WebBrowser webBrowser1.GoBack();/上一頁 webBrowser1.GoForward();/下一頁 webBrowser1.Refresh();/刷新 webBrowser1.GoHome();/主頁這里提供了WebBrows
17、er常用的方法, 上面的代碼是將 我們園子的主頁載入到 WebBrowser控件中。如果我們想要在應(yīng)用程式中產(chǎn)生自己的網(wǎng)頁內(nèi)容,可以設(shè)定DocumentText屬性。也可以通過Document屬性來處 理目前的網(wǎng)頁內(nèi)容。如下代碼是使用 DocumentText 屬性,顯示網(wǎng)頁內(nèi)容。并用Document屬性來處理所顯示的網(wǎng)頁。 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生成自己的網(wǎng)頁,那么能不能象使用IE那樣操作這個網(wǎng)頁呢?,答案是肯定的,完全可以像操作Web程序那樣操作 WebBrowser 控制項。比如我們可以加入腳本,CSS。當然,如果你熟悉 HTML 物件對象模型 (DOM),也可以透過 Document 屬性來處理目前的Web網(wǎng)頁內(nèi)容。下面的例子加入了JavaScript腳本來控制網(wǎng)頁。如果要在Winfrom程序中寫大量的Javascriot代 碼,而且這些代碼最終要轉(zhuǎn)換成String型載入到Webbrowser 那將是很痛苦的事情,不過沒有關(guān)系,我們可以創(chuàng)建一個js文件,放入資源中,用的時候
25、只需從資源中載入即可。這里我創(chuàng)建一個名為 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頁面如何實現(xiàn)在WinForm程序下處理呢。例如上例中的 "<font onclick = 'ClickEvent(Name.value)'>Click Here</font>" 。這里的Click事件是通過腳本處理的,如何讓這個Click事件在Winform中處理呢?這里要做一些修改。若要從指令碼存取用戶端應(yīng)用程式,需要 設(shè)定ObjectForScripting 屬性。指令碼可以將您指定的物件當做window.external 物件來存取
34、。 使用ObjectForScripting屬性,可啟用 WebBrowser 控制項所裝載之 Web 網(wǎng)頁與包含 WebBrowser 控制項之應(yīng)用程式間的通訊。這個屬性可讓您整合動態(tài)超文字標記語言 (DHTML) 程式碼與用戶端應(yīng)用程式程式碼。指定給這個屬性的物件可讓 Web 網(wǎng)頁指令碼做為 window.external 物件,這個物件是為了存取主應(yīng)用程式而提供的內(nèi)建 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 屬性設(shè)置為 this。注意:在From1 類的開頭加入了這么一句ComVisible(true), 它在System.Runtime.InteropServices下,預(yù)設(shè)值為 true,指出 Managed 型別對于 COM 為可見的。 ComVisible(true) public partial class Form1 : System.Windows.Forms.Form結(jié)束語:本文簡單的介紹了 WebBrowser 的用法,實現(xiàn)了WinForm
48、程序下與Web頁面的交互使用。關(guān)于在WINFORM中,如果向一個JSP文件提交數(shù)據(jù),緊急求救中。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> / 必需的設(shè)計器變量。 / </summary> private System.ComponentModel.Container components = null; public Form3() / / Windows 窗體設(shè)計器支持所必需的 / InitializeComponent(); / / TODO: 在 Initi
51、alizeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 / / <summary> / 清理所有正在使用的資源。 / </summary> protected override void Dispose( bool disposing ) if( disposing ) if(components != null) components.Dispose(); base.Dis
52、pose( disposing ); #region Windows 窗體設(shè)計器生成的代碼 / <summary> / 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改 / 此方法的內(nèi)容。 / </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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 58 選擇性必修3 第九單元 第49講 植物細胞工程
- 52 選擇性必修2 第八單元 第43講 生態(tài)系統(tǒng)的結(jié)構(gòu)及其穩(wěn)定性
- 出租車公司股權(quán)轉(zhuǎn)讓與車輛更新協(xié)議
- 買文具教學(xué)設(shè)計課件
- 湘美教學(xué)一年級下冊課件
- 2024-2025學(xué)年安徽省蚌埠市高一下學(xué)期第六次聯(lián)考政治試題及答案
- 家用紡織品市場推廣中的情感化營銷策略考核試卷
- 音樂教育中的樂器制作教學(xué)評估體系構(gòu)建考核試卷
- 冷庫泄漏處理考核試卷
- 身韻在舞蹈創(chuàng)作中的運用考核試卷
- 江蘇省南京市六校聯(lián)合體2024-2025學(xué)年高一下學(xué)期期末調(diào)研測試歷史試題(含答案)
- 2025年法律職業(yè)資格考試民法專項練習(xí)卷:合同法真題解析及試題
- 玻尿酸介紹課件
- 2025至2030年中國電子束曝光系統(tǒng)行業(yè)市場研究分析及發(fā)展前景研判報告
- 2025中國心肌病綜合管理指南要點解讀課件
- 技術(shù)中心人員管理制度
- 2025年形勢與政策課程期末考試復(fù)習(xí)試卷及答案
- 財產(chǎn)獨立性專項審計報告模板3(清算審計報告模板)
- 2025年中考英語答題技巧與模式專題11閱讀七選五(學(xué)生版+解析)
- 2024年個人信用報告(個人簡版)樣本(帶水印-可編輯)
- 16J914-1 公用建筑衛(wèi)生間
評論
0/150
提交評論