




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.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这样来应用,那么本站做一个实例来测试winform与webform互相通讯的实例,下面先看下效果:winform调用bs页面的js函数效果图webform页面发送信息到winform效果图好了,看完上面的效果,下面我们看下如何实现吧。第一、打开VS2008创建winform项目,之后在MainForm拖入浏览器控件,并命令这个浏览器控件名为:WebContainer下面是全部CS端代码:/* * 名称:CS与BS互相通讯* 作者:cc* 官方:/chjun2000/*/ using System;using System.Collections.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 public MainForm() InitializeComponent(); this.WebContainer.ObjectForScripting = this; /这句很关键,主要和页面的JS互相操作 Uri uriSale = new System.Uri(http:/localhost:8012/index.htm); /浏览器控件默认打开页面 WebContainer.Url = uriSale; / / 菜单点击事件 / / / private void jsEventToolStripMenuItem_Click(object sender, EventArgs e) WebContainer.Navigate(javascript:fn_test();void(0);); / / BS调用方法 / / public void JavascriptCall(string strShow) MessageBox.Show(strShow); 好,做完winform,下面是http:/localhost:8012/index.htm页面的做法。第2、webform的页面,源码很简单,你可以直接复制源代码到本地测试就可以了。下面是HTML页面源代码:Code Test js event NET中winform与webform互相通讯实例-(学IT网欢迎你访问) 嗯!到现在为此,所有操作都可以了,非常简单,如果你有兴趣来按上面的代码来测试下在.net中利用webbrowser控件实现WinForm与WebForm交互在.net中的WebBrowser 控件可以让你装载Windows Form 应用程序中的Web 网页和其它采用浏览器的文件。可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Form 客户端应用程序。还是直接看代码吧。WebBrowser 控制项 提供的属性、方法和事件,可用来实现 Internet Explorer 的控制项 webBrowser1.Navigate(); /将指定位置处的文件载入至 WebBrowser webBrowser1.GoBack();/上一页 webBrowser1.GoForward();/下一页 webBrowser1.Refresh();/刷新 webBrowser1.GoHome();/主页这里提供了WebBrowser常用的方法, 上面的代码是将 我们园子的主页载入到 WebBrowser控件中。如果我们想要在应用程式中产生自己的网页内容,可以设定DocumentText属性。也可以通过Document属性来处 理目前的网页内容。如下代码是使用 DocumentText 属性,显示网页内容。并用Document属性来处理所显示的网页。1private void btnDocumentText_Click(object sender, EventArgs e)2 3 string szHtml = 456 DocumentText 78910 Please enter your name:11 12 Send input to method of Form class13 1415;17 webBrowser1.DocumentText = szHtml;18 19 2021 private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)22 23 System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;2425 if (document != null & document.AllName != null & String.IsNullOrEmpty(document.AllName.GetAttribute(value)26 27 e.Cancel = true;28 System.Windows.Forms.MessageBox.Show(You must enter your name before you can navigate to + e.Url.ToString();29 3031 既 然我们可以通过DocumentText生成自己的网页,那么能不能象使用IE那样操作这个网页呢?,答案是肯定的,完全可以像操作Web程序那样操作 WebBrowser 控制项。比如我们可以加入脚本,CSS。当然,如果你熟悉 HTML 物件对象模型 (DOM),也可以透过 Document 属性来处理目前的Web网页内容。下面的例子加入了JavaScript脚本来控制网页。如果要在Winfrom程序中写大量的Javascriot代 码,而且这些代码最终要转换成String型载入到Webbrowser 那将是很痛苦的事情,不过没有关系,我们可以创建一个js文件,放入资源中,用的时候只需从资源中载入即可。这里我创建一个名为 ClientScript.js 的文件。12function ClickEvent(name)34 alert(Hello: +name);567function KeyDown()8 9 if (event.keyCode=116)10 11 event.keyCode=0;12 event.returnValue=false;13 14 15 return false;16string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString(ClientScript); string szWebBrowserText = + + + szClientScript + + + Please enter your name:+ + Click Here+ ; webBrowser1.DocumentText = szWebBrowserText;WebBrowser 是 System.Windows.Forms 下的控制项,也就是意味着它是用在WimForm程序下,那么WebWrower所载入的Web页面如何实现在WinForm程序下处理呢。例如上例中的 Click Here 。这里的Click事件是通过脚本处理的,如何让这个Click事件在Winform中处理呢?这里要做一些修改。若要从指令码存取用户端应用程式,需要 设定ObjectForScripting 属性。指令码可以将您指定的物件当做window.external 物件来存取。 使用ObjectForScripting属性,可启用 WebBrowser 控制项所装载之 Web 网页与包含 WebBrowser 控制项之应用程式间的通讯。这个属性可让您整合动态超文字标记语言 (DHTML) 程式码与用户端应用程式程式码。指定给这个属性的物件可让 Web 网页指令码做为 window.external 物件,这个物件是为了存取主应用程式而提供的内建 DOM 物件。1 private void btnScriptEvent_Click(object sender, EventArgs e)2 34 / This is the handler for loading the script into the Web Browser control and allowing us to interact5 / between the script in the Browser control and this form class8 / Set the ObjectForScripting property of the Web Browser control to point to this form class9 / This will allow us to interact with methods in this form class via the window.external property 10 webBrowser1.ObjectForScripting = this;1112 string szWebBrowserText = +13 +14 + 15 +16 +17 18 Please enter your name:+19 +20 Click Here+21 ;222324 webBrowser1.DocumentText = szWebBrowserText;25 26 public void ClickEvent(string userName)27 28 / Simply echo 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 MessageBox.Show(Hello + userName, Managed Web Browser Sample, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);31 else32 MessageBox.Show(Hello + userName, Managed Web Browser Sample, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);3334 这里的ObjectForScripting 属性设置为 this。注意:在From1 类的开头加入了这么一句ComVisible(true), 它在System.Runtime.InteropServices下,预设值为 true,指出 Managed 型别对于 COM 为可见的。ComVisible(true)public partial class Form1 : System.Windows.Forms.Form结束语:本文简单的介绍了 WebBrowser 的用法,实现了WinForm程序下与Web页面的交互使用。关于在WINFORM中,如果向一个JSP文件提交数据,紧急求救中。using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace test / / Form3 的摘要说明。 / 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 axWebBrowser1; / / 必需的设计器变量。 / private System.ComponentModel.Container components = null; public Form3() / / Windows 窗体设计器支持所必需的 / InitializeComponent(); / / TODO: 在 InitializeComponent 调用后添加任何构造函数代码 / / / 清理所有正在使用的资源。 / protected override void Dispose( bool disposing ) if( disposing ) if(components != null) components.Dispose(); base.Dispose( disposing ); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要使用代码编辑器修改 / 此方法的内容。 / private void InitializeComponent() System.Resources.ResourceManager resources = 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 = new AxSHDocVw.AxWebBrowser(); (System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).BeginInit(); this.SuspendLayout(); / / textBox1 / this.textBox1.Location = new System.Drawing.Point(16, 8); this.textBox1.Name = textBox1 ; this.textBox1.TabIndex = 0; this.textBox1.Text = textBox1 ; / / textBox2 / this.textBox2.Location = new System.Drawing.Point(128, 8); this.textBox2.Name = textBox2 ; this.textBox2.PasswordChar = * ; this.textBox2.TabIndex = 1; this.textBox2.Text = textBox2 ; / / button1 / this.button1.Location = new System.Drawing.Point(240, 8); this.button1.Name = 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 System.Drawing.Size(248, 16); this.axWebBrowser1.TabIndex = 3; / / Form3 / this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.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 += new System.EventHandler(this.Form3_Load); (System.ComponentModel.ISupportInitialize)(this.axWebBrowser1).EndInit(); this.ResumeLayout(false); #endregion private void Form3_Load(object se
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年机械安全操作规范考试题库安全操作技能评估试题
- 2025年大学警卫学专业题库- 国际维和行动中的警卫合作模式
- 2025年大学移民管理专业题库- 移民汽车生产与交通运输管理
- 2025年大学华文教育专业题库- 华文教育中的情感管理实践
- 2025年中学教师资格考试《综合素质》教育案例模拟试题及解析
- 2025年大学科学教育专业题库- 科学教育与可持续发展教育的联系
- 2025年大学警卫学专业题库- 火灾逃生演练及应急处理
- 医保政策考试题库及答案解析2025年(信息化应用篇)
- 2025年大学劳动教育专业题库- 劳动教育对学生生活态度的影响
- 2025年大学华文教育专业题库- 学生学习策略与教师指导
- 脑器质性精神障碍患者的护理查房
- (高清版)TDT 1013-2013 土地整治项目验收规程
- 初中数学分层作业设计举例-有理数
- 西方经济学简史
- 给小学生科普化学
- 信息管理系统的设计与实现
- 新闻报道与舆论导向
- 局放实验操作规程
- 透明土实验技术的研究进展
- 戴海崎心理与教育测量第4版课后习题答案
- 某火电厂输煤系统土建工程监理细则
评论
0/150
提交评论