在上一篇随笔 ActiveX(三)ActiveX 调用 Js 中,我们已经可以获得js中window对象的强类型接口、即 mshtml.IHTMLWindow2 ,通过该接口、我们可以调用js函数。那么我们再试一试其他的方法呢,看看结果是否符合预期:
private void btnAlert_Click(object sender, EventArgs e) { if (this.window2 != null) { this.window2.alert("C#CodeAlert:" + this.txtPwd.Text); } } private void btnLocation_Click(object sender, EventArgs e) { if (this.window2 != null && this.window2.location != null) { this.window2.alert(this.window2.location.toString()); } }
哈哈,试了一下,没有问题。 但是经过使用发现 location 属性是只读的,并且、如果通过反射强制设置 location 属性,则会抛出异常哦。
现在知道了 window , 那 document 呢? mshtml 命名空间中有没有提供相应的接口呢? 经过查找发现、document实现了如下的两个接口:
[Guid("332C4425-26CB-11D0-B483-00C04FD90119")] [TypeLibType(4160)] public interface IHTMLDocument2 : IHTMLDocument { [DispId(1005)] IHTMLElement activeElement { get; } [DispId(1022)] dynamic alinkColor { get; set; } [DispId(1003)] IHTMLElementCollection all { get; } [DispId(1007)] IHTMLElementCollection anchors { get; } [DispId(1008)] IHTMLElementCollection applets { get; } [DispId(-501)] dynamic bgColor { get; set; } [DispId(1004)] IHTMLElement body { get; } [DispId(1032)] string charset { get; set; } [DispId(1030)] string cookie { get; set; } [DispId(1033)] string defaultCharset { get; set; } [DispId(1014)] string designMode { get; set; } [DispId(1029)] string domain { get; set; } [DispId(1015)] IHTMLElementCollection embeds { get; } [DispId(1031)] bool expando { get; set; } [DispId(-2147413110)] dynamic fgColor { get; set; } [DispId(1043)] string fileCreatedDate { get; } [DispId(1044)] string fileModifiedDate { get; } [DispId(1042)] string fileSize { get; } [DispId(1045)] string fileUpdatedDate { get; } [DispId(1010)] IHTMLElementCollection forms { get; } [DispId(1019)] FramesCollection frames { get; } [DispId(1011)] IHTMLElementCollection images { get; } [DispId(1028)] string lastModified { get; } [DispId(1024)] dynamic linkColor { get; set; } [DispId(1009)] IHTMLElementCollection links { get; } [DispId(1026)] HTMLLocation location { get; } [DispId(1041)] string mimeType { get; } [DispId(1048)] string nameProp { get; } [DispId(-2147412090)] dynamic onafterupdate { get; set; } [DispId(-2147412091)] dynamic onbeforeupdate { get; set; } [DispId(-2147412104)] dynamic onclick { get; set; } [DispId(-2147412103)] dynamic ondblclick { get; set; } [DispId(-2147412077)] dynamic ondragstart { get; set; } [DispId(-2147412074)] dynamic onerrorupdate { get; set; } [DispId(-2147412099)] dynamic onhelp { get; set; } [DispId(-2147412107)] dynamic onkeydown { get; set; } [DispId(-2147412105)] dynamic onkeypress { get; set; } [DispId(-2147412106)] dynamic onkeyup { get; set; } [DispId(-2147412110)] dynamic onmousedown { get; set; } [DispId(-2147412108)] dynamic onmousemove { get; set; } [DispId(-2147412111)] dynamic onmouseout { get; set; } [DispId(-2147412112)] dynamic onmouseover { get; set; } [DispId(-2147412109)] dynamic onmouseup { get; set; } [DispId(-2147412087)] dynamic onreadystatechange { get; set; } [DispId(-2147412093)] dynamic onrowenter { get; set; } [DispId(-2147412094)] dynamic onrowexit { get; set; } [DispId(-2147412075)] dynamic onselectstart { get; set; } [DispId(1034)] IHTMLWindow2 parentWindow { get; } [DispId(1021)] IHTMLElementCollection plugins { get; } [DispId(1047)] string protocol { get; } [DispId(1018)] string readyState { get; } [DispId(1027)] string referrer { get; } [DispId(1001)] dynamic Script { get; } [DispId(1013)] IHTMLElementCollection scripts { get; } [DispId(1046)] string security { get; } [DispId(1017)] IHTMLSelectionObject selection { get; } [DispId(1069)] HTMLStyleSheetsCollection styleSheets { get; } [DispId(1012)] string title { get; set; } [DispId(1025)] string url { get; set; } [DispId(1023)] dynamic vlinkColor { get; set; } [DispId(1058)] void clear(); [DispId(1057)] void close(); [DispId(1067)] IHTMLElement createElement(string eTag); [DispId(1071)] IHTMLStyleSheet createStyleSheet(string bstrHref = "", int lIndex = -1); [DispId(1068)] IHTMLElement elementFromPoint(int x, int y); [DispId(1065)] bool execCommand(string cmdID, bool showUI = false, object value = Type.Missing); [DispId(1066)] bool execCommandShowHelp(string cmdID); [DispId(1056)] dynamic open(string url = "text/html", object name = Type.Missing, object features = Type.Missing, object replace = Type.Missing); [DispId(1060)] bool queryCommandEnabled(string cmdID); [DispId(1062)] bool queryCommandIndeterm(string cmdID); [DispId(1061)] bool queryCommandState(string cmdID); [DispId(1059)] bool queryCommandSupported(string cmdID); [DispId(1063)] string queryCommandText(string cmdID); [DispId(1064)] dynamic queryCommandValue(string cmdID); [DispId(1070)] string toString(); [DispId(1054)] void write(params object[] psarray); [DispId(1055)] void writeln(params object[] psarray); }