繁体   English   中英

如何在C#中从ActiveX或DLL调用Java脚本函数

[英]how to call java script function from activex or dll in c#

我有用多线程的c#编写的active-x类。 我需要从我的activeX调用javascript。 我通过Microsoft.mshtml尝试过。

/*JS

function do_Print() {
    control.setPage(this);
    control.scriptPrint();
}

function report_back(report){
    alert("Report:"+report);
}


C#

    public void setPage(mshtml.HTMLWindow2Class JSFile) {
                window = JSFile;
    }
    public void scriptPrint(){
                window.execScript("report_back('Printing complete!')", "JScript");
    }
    */

但是它抛出异常

“无法将类型为“ mshtml.HTMLWindow2Class”的COM对象转换为接口类型为“ mshtml.DispHTMLWindow2”

还有另一种方法。 我可以从Java脚本中调用active-x函数,反之亦然。 关于多线程c#active-x调用javascript函数的任何想法?

您可以像这样访问html

private void MyControl_Load(object sender, EventArgs e)
        {
            if (this.Site != null)
            {
                htmldoc = (HtmlDocument)this.Site.GetService(typeof(HtmlDocument));

            }


        }

然后在任何按钮上单击我们的C#控件invoke方法以单击html按钮

 HtmlElement setCLIButton = htmldoc.GetElementById("searchButton");
                setCLIButton.InvokeMember("onClick");

通过这种方式,您可以调用javacsript函数,希望它能对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM