Aakash
Aakash

Reputation: 695

How to obtain the Document Interface when Hosting the WebBrowser Control?

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);
}

Upvotes: 4

Views: 857

Answers (1)

Hans Passant
Hans Passant

Reputation: 942000

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>";

Upvotes: 3

Related Questions