本文主要簡單的記錄winform如何與html文件中的信息如何進行交互,即在winform中加載html界面,從而能夠進行相互調用。javascript
1.新建一個winform項目,若要在winform中加載html,須要一個webBrowser控件。html
2.新建一個html頁面,這裏命名爲「test.htm」.java
3.c#代碼:web
//爲了使網頁可以與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代碼:c#
<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 的函數名", 參數");this
7.c#代碼:直接拖動一個button控件到頁面中。spa
private void button1_Click(object sender, EventArgs e) { this.webBrowser1.Document.InvokeScript("WfToHtml"); }
8.js代碼:code
<script type ="text/javascript"> function WfToHtml() { alert("wf調用html裏面的js函數"); } </script>
9.結果:orm
初學者,內容也比較簡單,準備再加載一個swf,哈哈。。。