在上一篇随笔 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); }
IHTMLDocument2
[Guid("3050F485-98B5-11CF-BB82-00AA00BDCE0B")] [TypeLibType(4160)] public interface IHTMLDocument3 { [DispId(1080)] string baseUrl { get; set; } [DispId(-2147417063)] dynamic childNodes { get; } [DispId(-2147412995)] string dir { get; set; } [DispId(1075)] IHTMLElement documentElement { get; } [DispId(1079)] bool enableDownload { get; set; } [DispId(1082)] bool inheritStyleSheets { get; set; } [DispId(-2147412043)] dynamic onbeforeeditfocus { get; set; } [DispId(-2147412048)] dynamic oncellchange { get; set; } [DispId(-2147412047)] dynamic oncontextmenu { get; set; } [DispId(-2147412071)] dynamic ondataavailable { get; set; } [DispId(-2147412072)] dynamic ondatasetchanged { get; set; } [DispId(-2147412070)] dynamic ondatasetcomplete { get; set; } [DispId(-2147412065)] dynamic onpropertychange { get; set; } [DispId(-2147412050)] dynamic onrowsdelete { get; set; } [DispId(-2147412049)] dynamic onrowsinserted { get; set; } [DispId(-2147412044)] dynamic onstop { get; set; } [DispId(1078)] IHTMLDocument2 parentDocument { get; } [DispId(1077)] string uniqueID { get; } [DispId(-2147417605)] bool attachEvent(string @event, object pdisp); [DispId(1076)] IHTMLDocument2 createDocumentFragment(); [DispId(1074)] IHTMLDOMNode createTextNode(string text); [DispId(-2147417604)] void detachEvent(string @event, object pdisp); [DispId(1088)] IHTMLElement getElementById(string v); [DispId(1086)] IHTMLElementCollection getElementsByName(string v); [DispId(1087)] IHTMLElementCollection getElementsByTagName(string v); [DispId(1073)] void recalc(bool fForce = false); [DispId(1072)] void releaseCapture(); }
IHTMLDocument3
在 IHTMLDocument3 中 发现的比较重要的几个方法,是 IHTMLDocument2 中没有的。
[DispId(1088)] IHTMLElement getElementById(string v); [DispId(1086)] IHTMLElementCollection getElementsByName(string v); [DispId(1087)] IHTMLElementCollection getElementsByTagName(string v);
document 有了,那 element 呢? element 似乎有点多、一共有四个,常用的有两个、分别为:IHTMLElement、IHTMLElement2。不做过多解释。
如果把 element 再具体一些、像 Frame、 Form、input 等等 都有相对应的接口的,由此可以看出 mshtml 命名空间强大之处。
附一个简单应用demo,通过id获取element,然后得到按钮的显示文本。
private void btnId_Click(object sender, EventArgs e) { if (this.document3 != null) { IHTMLElement ele = this.document3.getElementById("btnId"); if (ele != null) { var attri = ele.getAttribute("value") as object; if (attri != null) { MessageBox.Show(attri.ToString()); } } } }
完整的测试项目demo: TestActiveX.zip
(暂完,如有需要后续补充)
时间: 2024-10-27 12:26:23