winform 与 html 交互 简单案例
来源:互联网 发布:从0到简单小游戏java 编辑:程序博客网 时间:2024/06/07 02:47
本文主要简单的记录winform如何与html文件中的信息如何进行交互,即在winform中加载html界面,从而可以进行相互调用。
1.新建一个winform项目,若要在winform中加载html,需要一个webBrowser控件。
2.新建一个html页面,这里命名为“test.htm”.
3.c#代码:
//为了使网页能够与winform交互 将com的可访问性设置为真 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute(true)]public void Hello(){ MessageBox.Show("OK,html在调用wf中的函数");}private void Form1_Load(object sender, EventArgs e){ this.webBrowser1.ObjectForScripting = this; string path = Application.StartupPath + @"\test.htm"; //MessageBox.Show(path); //this.webBrowser1.Navigate(path); this.webBrowser1.Url = new System.Uri(path, System.UriKind.Absolute);}
4.html代码:
<html> <head> <title>this is a test</title> <script type ="text/javascript"> function Hello() { window.external.Hello();//getDebugPath()为c#方法 //alert("hello"); } </script> </head> <body> <button id="btn" onclick="Hello()">hello</button> </body></html>
5.结果:这里算是简单的完成了在winform中加载html,并在js中调用了c#中的信息。
6.为了方便,直接在上面的基础上实现在winform中调用html中的js函数。关键点:this.webBrowser1.Document.InvokeScript("js 的函数名", 参数");
7.c#代码:直接拖动一个button控件到页面中。
private void button1_Click(object sender, EventArgs e){ this.webBrowser1.Document.InvokeScript("WfToHtml");}
8.js代码:
<script type ="text/javascript"> function WfToHtml() { alert("wf调用html里面的js函数"); }</script>
9.结果:
初学者,内容也比较简单,准备再加载一个swf,哈哈。。。
0 0
- winform 与 html 交互 简单案例
- html和winform交互
- C#winForm程序与html JS交互调用
- html网页与iOS的简单交互
- WinForm与Javascript交互
- WinForm与Javascript交互
- WinForm与Javascript交互
- [Winform]js与webbrowser交互
- php与html简单的json交互示例
- WebBroWebBrowser控件上的代码(Javascript、Html Dom)与Winform控件本身的交互
- Silverlight与Html交互
- jsp与html交互
- PHP与HTML交互
- android与html交互
- html与后台交互
- android与html交互
- mongodb与python交互案例
- Highchartsjs前后台交互的简单案例
- Mathematics for computer graphics----Numbers
- 如何使用CFileDialog
- IOS开发之----NSDictionary,JSON和XML互相转换
- express路由链和错误处理
- MySQL数据库备份和还原的常用命令
- winform 与 html 交互 简单案例
- WPF 有关输入法的BUG 有人有解决方案吗?
- 为DualWan WR-525W增加OpenWrt支持。
- 有关Linux系统负载的一些说明
- Linux设备驱动程序学习笔记08:自旋锁和信号量
- 黑马程序员————银行业务调度系统(第十篇)
- Python的字符串操作
- apache httpclient 使用例子
- writeexcel gem 中的合并格式的问题的解决