简体   繁体   中英

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:

WebBrowser.Document.DomDocument as IHTMLDocument2

What actually is an IHTMLDocument2, 3 or 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.

Can anybody explain to me the basic functionality of the IHTMLDocuments and the IHTMLTxtRange please?

IHTMLDocument is an interface which is essentially an "unbreakable" contract that represents what the object that implements it will provide.

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. 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 :

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. So as the library evolved Microsoft added more and more Interfaces to expose the newer functionality/code.

IHTMLTxtRange is an interface for the more commonly used TextRange object. It exposes a bunch of functionality for parsing text "Fragments" or "Ranges".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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