在上一篇随笔: ActiveX(二)Js 监听 ActiveX中的事件  中,已经可以实现 Js 监听 ActiveX中的事件,至此、Js 和 ActiveX 已经可以实现双向通讯了。但是、这样的实现方式,都是站在Js的角度去实现的,那么 ActiveX 能否主动调用 Js 呢?答案无疑是肯定的,在该篇随笔中、我们将逐渐揭开这一层神秘的面纱。

 

  我第一次接触用C#代码调用Js是在四年前,那时候正在实习做Windows应用、需要借用 WebBrowser 控件操作js、完成一些特殊需求。当时的代码大致如下:

        // 执行JS
        private void ExecJs()
        {
            try
            {
                if (this.webBrowser.Document != null)
                {
                    mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)this.webBrowser.Document.DomDocument;
                    if (currentDoc != null)
                    {
                        mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
                        if (win != null)
                        {
                            //调用 函数F、传递一个宽度做参数
                            win.execScript(string.Format("F('{0}')", this.webBrowser.Width), "javascript");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

 

  由上述代码、可以看出,其核心方法为 mshtml.IHTMLWindow2.execScript,注意:我们再仔细看一下方法名,有没有觉得很熟悉?如果你还没想起来,那我们继续看一下 mshtml.IHTMLWindow2 这个类型中还有什么成员吧。

    [Guid("332C4427-26CB-11D0-B483-00C04FD90119")]
    [TypeLibType(4160)]
    public interface IHTMLWindow2 : IHTMLFramesCollection2
    {
        [DispId(1153)]
        dynamic _newEnum { get; }
        [DispId(1161)]
        HTMLNavigator clientInformation { get; }
        [DispId(23)]
        bool closed { get; }
        [DispId(1101)]
        string defaultStatus { get; set; }
        [DispId(1151)]
        IHTMLDocument2 document { get; }
        [DispId(1152)]
        IHTMLEventObj @event { get; }
        [DispId(1169)]
        dynamic external { get; }
        [DispId(1100)]
        FramesCollection frames { get; }
        [DispId(2)]
        HTMLHistory history { get; }
        [DispId(1125)]
        HTMLImageElementFactory Image { get; }
        [DispId(1001)]
        int length { get; }
        [DispId(14)]
        HTMLLocation location { get; }
        [DispId(11)]
        string name { get; set; }
        [DispId(5)]
        HTMLNavigator navigator { get; }
        [DispId(1164)]
        dynamic offscreenBuffering { get; set; }
        [DispId(-2147412073)]
        dynamic onbeforeunload { get; set; }
        [DispId(-2147412097)]
        dynamic onblur { get; set; }
        [DispId(-2147412083)]
        dynamic onerror { get; set; }
        [DispId(-2147412098)]
        dynamic onfocus { get; set; }
        [DispId(-2147412099)]
        dynamic onhelp { get; set; }
        [DispId(-2147412080)]
        dynamic onload { get; set; }
        [DispId(-2147412076)]
        dynamic onresize { get; set; }
        [DispId(-2147412081)]
        dynamic onscroll { get; set; }
        [DispId(-2147412079)]
        dynamic onunload { get; set; }
        [DispId(4)]
        dynamic opener { get; set; }
        [DispId(1157)]
        HTMLOptionElementFactory Option { get; }
        [DispId(12)]
        IHTMLWindow2 parent { get; }
        [DispId(1156)]
        IHTMLScreen screen { get; }
        [DispId(20)]
        IHTMLWindow2 self { get; }
        [DispId(1102)]
        string status { get; set; }
        [DispId(21)]
        IHTMLWindow2 top { get; }
        [DispId(22)]
        IHTMLWindow2 window { get; }

        [DispId(1105)]
        void alert(string message = "");
        [DispId(1159)]
        void blur();
        [DispId(1163)]
        void clearInterval(int timerID);
        [DispId(1104)]
        void clearTimeout(int timerID);
        [DispId(3)]
        void close();
        [DispId(1110)]
        bool confirm(string message = "");
        [DispId(1165)]
        dynamic execScript(string code, string language = "JScript");
        [DispId(1158)]
        void focus();
        [DispId(0)]
        dynamic item(ref object pvarIndex);
        [DispId(7)]
        void moveBy(int x, int y);
        [DispId(6)]
        void moveTo(int x, int y);
        [DispId(25)]
        void navigate(string url);
        [DispId(13)]
        IHTMLWindow2 open(string url = "", string name = "", string features = "", bool replace = false);
        [DispId(1111)]
        dynamic prompt(string message = "", string defstr = "undefined");
        [DispId(8)]
        void resizeBy(int x, int y);
        [DispId(9)]
        void resizeTo(int x, int y);
        [DispId(1160)]
        void scroll(int x, int y);
        [DispId(1167)]
        void scrollBy(int x, int y);
        [DispId(1168)]
        void scrollTo(int x, int y);
        [DispId(1173)]
        int setInterval(string expression, int msec, ref object language = Type.Missing);
        [DispId(1172)]
        int setTimeout(string expression, int msec, ref object language = Type.Missing);
        [DispId(1155)]
        void showHelp(string helpURL, object helpArg = Type.Missing, string features = "");
        [DispId(1154)]
        dynamic showModalDialog(string dialog, ref object varArgIn = Type.Missing, ref object varOptions = Type.Missing);
        [DispId(1166)]
        string toString();
    }
IHTMLWindow2

相关文章:

  • 2022-12-23
  • 2021-07-17
  • 2021-05-18
  • 2021-09-19
  • 2021-11-30
  • 2022-02-25
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2021-05-26
  • 2022-12-23
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
相关资源
相似解决方案