在webBrowser使用过程中为了C#和JS通讯,webBrowser必须设置ObjectForScripting的属性,它是一个object,这个object可以提供给webBrowser控件载入的网页上的script访问。

    在设置过webBrowser控件的ObjectForScripting属性后,还需要设置应用程序对com可见,不然会抛出一个异常 (ObjectForScripting 的类必须对 COM 可见。请确认该对象是公共的,或考虑向您的类添加 ComVisible 属性。),可做如下设置:

[System.Runtime.InteropServices.ComVisible(true)]

例如:

复制代码
[ComVisible(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            webBrowser1.ObjectForScripting = this;
            webBrowser1.Navigate("http://www.cnblogs.com/winzheng");
            base.OnLoad(e);
        }
    }
复制代码

如果要和JS通讯,使用webBrowser的Document对象的InvokeScript方法即可。

该方法签名如下:

InvokeScript(String spriteName,object[] args);

例如:

复制代码
<scripttype="text/javascript">     //提供给c#调用的方法    
 function test(n,s){         
    alert(n+"/"+s);    
 }     //调用C#的方法     
function callCSharp(){         //这里可以看到window.external被设置成了什么        
 alert(window.external);        
 window.external.Test("hello",15);     
} 
</script>
<button onclick="callCSharp()">call c#</button>

调用:web.Document.InvokeScript("test", new Object[] {1,"hello"});
复制代码

http://www.lwolf.cn/blog/article/code/WebBrowser-js-call-csharp.htm

http://blog.udnz.com/Article/Invoke_Members_Of_Winform_On_Webbrowser.aspx

posted @ 2015-04-02 10:54 星释天狼 阅读(1006) 评论(0) 推荐(0) 编辑
摘要: 原文地址:开源大数据利器汇总 作者:Aquester 大数据利器.xls 类别 名称 官网 备注 查询引擎 Phoenix http://phoenix.incubator.apache.org/ Sale... 阅读全文
posted @ 2015-03-05 09:21 星释天狼 阅读(443) 评论(0) 推荐(0) 编辑
摘要: 解决软件问题的关键 阅读全文
posted @ 2011-10-02 03:03 星释天狼 阅读(248) 评论(0) 推荐(0) 编辑
摘要: [转]http://blog.csdn.net/GeoLiMao/archive/2010/10/15/5943605.aspx 首先,下载Boost及编译工具Boost Jam。 1:下载Boost。文件为boost_1_44_0.zip。http://www.boost.org/ 2:下载Boost Jam。文件为boost-jam-3.1.18-1-ntx86.zip。http://www.boost.org/ 其次,编译Boost。 1:解压Boost和Boost Jam。将”(解压目录)\boost-jam-3.1.18-1-ntx86\bjam.exe“拷贝到”(解压目录)\bo 阅读全文
posted @ 2010-12-11 12:22 星释天狼 阅读(681) 评论(0) 推荐(0) 编辑
摘要: #include<iostream>using namespace std;int f1(){ return 0;} int f2(){ return 0;} int f3(){ return 0;} int f4(){ return 0;} int main(){ int (*a[])()={&f1,&f2,&f3,&f4}; for(int i=0... 阅读全文
posted @ 2010-08-19 22:11 星释天狼 阅读(677) 评论(0) 推荐(0) 编辑
该文被密码保护。 阅读全文
posted @ 2010-03-01 16:41 星释天狼 阅读(4) 评论(0) 推荐(0) 编辑
摘要: windows 程序设计:http://blog.csdn.net/villager/archive/2003/04/30/14615.aspx 阅读全文
posted @ 2010-02-22 16:42 星释天狼 阅读(230) 评论(0) 推荐(0) 编辑
摘要: 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起C# 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。办法一把vs2005菜单的 调试->异常->Managed Debuggin Assistants->LoaderLock... 阅读全文
posted @ 2010-02-03 13:40 星释天狼 阅读(602) 评论(0) 推荐(0) 编辑
摘要: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Drawing.Imaging;using System.Text;using System.Windows.F... 阅读全文
posted @ 2010-01-23 11:55 星释天狼 阅读(672) 评论(0) 推荐(0) 编辑
摘要: C#中使用GDI+实现饼状图和柱状图跟数据库联接显示数据的方法http://developer.51cto.com/art/200908/148464.htmhttp://www.cnblogs.com/todd/articles/1241308.htmlhttp://hi.baidu.com/417533880/blog/item/c740b00824f5183ae8248823.htmlhtt... 阅读全文
posted @ 2010-01-08 10:53 星释天狼 阅读(481) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示