4

Please have a look at the following excellent article available on MSDN

I am in process of creating a IE toolbar with the help of BandObjects

I have access to WebBrowser Control but not able to instantiate the HTMLDocument which is require to modify the DOM.

Here is an excerpt of my code:

// Registering for DocumentComplete envent 
Explorer.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(Explorer_DocumentComplete);

// I am not sure about the following function. 
// I am trying to do something as suggested in this MSDN article - 
// http://msdn.microsoft.com/en-us/library/aa752047%28v=vs.85%29.aspx#Document

void Explorer_DocumentComplete(object pDisp, ref object URL)
{  
        IPersistStream pStream;
        string htmlText = "<html><h1>Stream Test</h1><p>This HTML content is being loaded from a stream.</html>";

        HTMLDocumentClass document = (HTMLDocumentClass)this.Explorer.IWebBrowser_Document;
        document.clear();
        document.write(htmlText);
        IHTMLDocument2 document2 = (IHTMLDocument2)pDisp;

        pStream = (IPersistStream)document2.queryCommandValue("IID_IHTMLDocument2");
        HtmlDocument objdec = webBroswer.Document;
        objdec.Write(htmlText);
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Aakash
  • 695
  • 3
  • 10
  • 25
  • 1
    I'm not sure you need to instantiate it. I think the webbrowser just has a Document property that points to the currently loaded document. Using the webbrowser's methods you can load (navigate to) a different document – GolezTrol Jul 22 '13 at 10:32
  • That's correct. For this question to be useful, you need to provide more information (e.g. your source) and details on what failure(s) occur. – EricLaw Jul 22 '13 at 15:52
  • @GolezTrol True,webbrowser has a document property.But the property doesn't let me to set a new Document object to current webbrowser object. My requirement is not to navigate away to different page. I just want to create a new DOM locally and load that in the webbroswer Document property so that the changes can be shown on browser. – Aakash Jul 23 '13 at 02:56
  • @EricLaw I'll post the source code shortly. – Aakash Jul 23 '13 at 02:57
  • You say you want to create a new DOM locally, but I don't understand why. You can replace the contents of the loaded document or navigate to another document. I think there are ways to do this without loading an actual file or url, but I don't think it is posisble to actually replace the document object, which is the container object for the document. When you put new HTML in it, it will parse it and create a new DOM for you. I think that is what your function does. – GolezTrol Jul 23 '13 at 07:30
  • But do you get any errors when executing that document? All you said is "Here is a snippet I found, and now I don't know what to do". Have you even tried to execute it? – GolezTrol Jul 23 '13 at 07:31
  • Let me point you to a MSDN article - http://msdn.microsoft.com/en-us/library/aa752047%28v=vs.85%29.aspx#Document. Basically this is what I am trying to achieve here. Unfortunately, the example source code is all written in VC++, which is not my playground. I am unable to get the reference to IDispatch, IPersistentInit and QueryInterface methods. Also, the code that I have posted in my Question is not working. But I am able to handle the Document_Complete event and it was invoked successfully. – Aakash Jul 23 '13 at 11:08
  • The requirement is to create this solution in C# which I know may have performance issues. – Aakash Jul 24 '13 at 04:55

1 Answers1

3

You just got a bit lost in the native IE object model. Which does indeed makes it a bit painful to load your own HTML into the browser.

That's not a problem if you actually use the WebBrowser control. The glue is hidden in the .NET wrapper. It is super-simple, you just assign the DocumentText property. The property setter takes care of the hoopla. You don't need the DocumentCompleted event handler, it all collapses to a single line of code you can put anywhere:

   webBrowser1.DocumentText = "<h1>Hello world</h1>";
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • +1 Very nice! I got so invested in the `IHTMLDocument2` route, that it never occurred to me that the control might make it that easy... – Lynn Crumbling Jul 29 '13 at 20:43
  • @Hans Thanks for input. Actually, that is the first thing I tried without success. It seems very straight forward and was hoping that this should work but it didn't. Please have a look at [this article at MSDN](http://msdn.microsoft.com/en-us/library/aa752047%28v=vs.85%29.aspx#Document). It says _["The DHTML Object Model is used to access and manipulate the contents of an HTML page and is not available until the page is loaded. Your application determines that a page is loaded by handling the DWebBrowserEvents2::DocumentComplete event of the WebBrowser control."]_ – Aakash Jul 31 '13 at 06:20
  • Yes, that's accurate. It applies when you want to make changes to a web page after it was loaded. You didn't make changes in your snippet, you completely replaced it so it is just much simpler to use the DocumentText property. The event is already available, it is WebBrowser.DocumentCompleted. No need to use the native event. The IHtmlDocument2 interface is also wrapped, it is the Document property. It isn't impossible to use the native COM interfaces, you can, it is just much, much easier to use the friendly .NET wrappers. – Hans Passant Jul 31 '13 at 06:46
  • The "WebBrowserClass" in Assembly "Interop.SHDocVw.dll" doesn't have property "DocumentText". Am I missing something here? Do I need a reference to System.Windows.Forms.WebBrowser? – Aakash Jul 31 '13 at 10:11
  • When you say "WebBrowser control" in your question then everybody will indeed assume you use that control. This is getting to be a very painful question. – Hans Passant Jul 31 '13 at 11:53