简体   繁体   English

使用IHTMLDocuments 1,2,3和4

[英]Using IHTMLDocuments 1, 2, 3 & 4

I am using a web browser in my current project and currently I'm using it in design mode to make it editable etc. The code I am currently using is: 我在当前项目中使用Web浏览器,目前我在设计模式下使用它以使其可编辑等。我目前使用的代码是:

WebBrowser.Document.DomDocument as IHTMLDocument2

What actually is an IHTMLDocument2, 3 or 4? 究竟什么是IHTMLDocument2,3或4? I have also found that when identifying a current selection range in the document, the range.text.replace method is not working the same way that a string.replace does. 我还发现,在识别文档中的当前选择范围时,range.text.replace方法的工作方式与string.replace的工作方式不同。

Can anybody explain to me the basic functionality of the IHTMLDocuments and the IHTMLTxtRange please? 任何人都可以向我解释IHTMLDocuments和IHTMLTxtRange的基本功能吗?

IHTMLDocument is an interface which is essentially an "unbreakable" contract that represents what the object that implements it will provide. IHTMLDocument是一个接口,它本质上是一个“牢不可破”的契约,它表示实现它的对象将提供什么。

Changing the interface when moving to a new version of the code would break that contract and in turn break the code that is relying on that contract. 移动到新版本的代码时更改界面将破坏该合同,从而打破依赖该合同的代码。

Suppose you create : 假设您创建:

public interface IMyInterface {
      public int Property1 { get;  set; }
}

A year later you need to add Property2 but you cannot change your interface. 一年后,您需要添加Property2,但无法更改界面。 So one way around that is to create: 因此,一种方法是创建:

public interface IMyInterface2 {
    public int Property2 { get;set; }
} 

and then with your old Class that is implementing IMyInterface : 然后使用正在实现IMyInterface的旧类:

public class MyObject : IMyInterface, IMyInterface2 {
    public int Property1 { get {} set {} }
    public int Property2 { get {} set {} }
}

Then you will not break the older contract but can use the new interface in code such as: 然后你不会破坏旧的合同,但可以在代码中使用新的接口,例如:

if (obj is IMyInterface) {
   Console.WriteLine(((IMyInterface)obj).Property1);

   if (obj is IMyInterface2) {
      //more
   }
}

So that is what Microsoft did. 这就是微软所做的。 The mshtml library that IHTMLDocument is in is a COM library and COM rely's heavily on interfaces. IHTMLDocument所在的mshtml库是一个COM库,而COM主要依赖于接口。 So as the library evolved Microsoft added more and more Interfaces to expose the newer functionality/code. 因此,随着库的发展,Microsoft添加了越来越多的接口来公开更新的功能/代码。

IHTMLTxtRange is an interface for the more commonly used TextRange object. IHTMLTxtRange是更常用的TextRange对象的接口。 It exposes a bunch of functionality for parsing text "Fragments" or "Ranges". 它公开了一堆用于解析文本“Fragments”或“Ranges”的功能。

http://www.webreference.com/js/column12/trmethods.html http://www.webreference.com/js/column12/trmethods.html

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

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