Chinaunix首页 | 论坛 | 博客
  • 博客访问: 284977
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: C#/.net

2017-01-22 13:31:18


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Runtime.InteropServices; //记得u这俩.
  10. using System.Security.Permissions;//记得u这俩.
  11.  
  12. namespace test
  13. {
  14.  
  15.     [PermissionSet(SecurityAction.Demand, Name ="FullTrust")]
  16.     [ComVisible(true)]//com+可见
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             webBrowser1.Document.InvokeScript("Run", new object[] { "CShareFunction" });
  27.         }
  28.          
  29.         private void Form1_Load(object sender, EventArgs e)
  30.         {
  31.             webBrowser1.ObjectForScripting = this;//具体公开的对象,这里可以公开自定义对象
  32.             webBrowser1.Navigate(Application.StartupPath + "/dom.html");
  33.         }
  34.  
  35.         public void ShowMsg(string msg)
  36.         {
  37.             MessageBox.Show(msg);
  38.             
  39.         }
  40.  
  41.     }
  42. }
下面是Html代码

点击(此处)折叠或打开

  1. <html>
  2.   <head>
  3.     
  4.   </head>
  5.   <body>
  6.   </body>
  7.      
  8.    <script type="text/javascript" charset="utf-8">
  9.     function Run(str)
  10.    {
  11.      
  12.         window.external.ShowMsg(str);
  13.    }
  14.    </script>
  15.    </html>

下面是官方的说法:
执行在 HTML 页面中定义的动态脚本函数。
重载此成员。有关此成员的完整信息(包括语法、用法和示例),请单击重载列表中的相应名称。

HtmlDocument.InvokeScript 方法

  名称 说明
公共方法 执行在 HTML 页面中定义的动态脚本函数。
公共方法 执行在 HTML 页面中定义的动态脚本函数。

点击(此处)折叠或打开

  1. <HTML>
  2. <SCRIPT>
  3. function test(name, address) {
  4. window.alert("Name is " + name + "; address is " + address);
  5. }
  6. </SCRIPT>

  7. <BODY>
  8. </BODY>
  9. </HTML>

C#代码:

点击(此处)折叠或打开

  1. private void InvokeTestMethod(String name, String address)
  2. {
  3.     if (webBrowser1.Document != null)
  4.     {
  5.         Object[] objArray = new Object[2];
  6.         objArray[0] = (Object)name;
  7.         objArray[1] = (Object)address;
  8.         webBrowser1.Document.InvokeScript("test", objArray);
  9.     }
  10. }


阅读(1056) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~