Adding HTML Content to the WebBrowser Control Dynamically

A common problem in dealing with the Microsoft’s WebBrowser control seems to be making it display HTML content.  The usual way of doing this is creating a HTML file, and using the IWebBrowser2::Navigate( ) or IWebBrowser2::Navigate2( ) methods.  It works perfectly, but it has some limitations.  You need to specify a URL or path to the HTML file.  It means that you should either have an external file, and distribute it with your application (which is not desirable, because the user would be able to directly edit it), or putting the HTML file into a resource in your module.  The downside with this is that you can’t make the WebBrowser control show dynamic data, which is generated by your application at runtime.  To overcome this problem, some people try to create a temporary HTML file, and Navigate( )-ing to that file, and some other people might develop a COM object and use it so that the application could talk to the web page, and vice versa, and hence transfer dynamic data.

Both of the above methods work, but are not perfect solutions, because they would have a lot of overhead (creating a file, doing unnecessary I/O, writing a COM object, and appropriate script in the page itself, and so on).  You would wish that you create a normal string containing some HTML code, and getting the WebBrowser to show that content.  I’ll show you how to do this here.  Also, for some applications (to name one, Yahoo Messenger), you would need to add some HTML content to the document currently loaded at runtime, without changing the old stuff that are being displayed.  This is also possible.

In the ZIP file found at the bottom of the article, you’ll find two files HTMLWriter.h and HTMLWriter.cpp which define the class CHTMLWriter as well as two sample applications.  Here is the description of the CHTMLWriter public member functions:

  • CHTMLWriter::CHTMLWriter( IUnknown * pUnk );
    CHTMLWriter::CHTMLWriter( IWebBrowser2 * pWB );

    The first constructor is used when you want to use this class with a ClassWizard-generated CWebBrowser2 class (for example, when you insert a WebBrowser control in a MFC dialog application).  The second constructor is used when you want to use this class with a MFC CHtmlView-derived class.  For the first case, you always pass CWnd::GetControlUnknown( ) to the constructor, and for the second case, you should pass m_pBrowserApp to the constructor.  (See the sample code to see how to do this).
  • CHTMLWriter::Write( LPCTSTR pszHTMLContent );
    This method writes the HTML content specified by pszHTMLContent to the WebBrowser control.  The pszHTMLContent should be a string containing raw HTML code, like "<html><head></head><body>My HTML file</body></html>".
  • CHTMLWriter::Add( LPCTSTR pszHTMLContent );
    This method appends the HTML content specified by pszHTMLContent to the WebBrowser control, leaving the rest of the document being displayed unchanged.  The pszHTMLContent should be a string containing raw HTML code, like "<p>New HTML content</p>".

Using the class couldn’t be any simpler.  You just construct an instance on the stack with the appropriate constructor, and call the Write( ) or Add( ) method, passing the string containing the HTML content to them.  The sample application MFCWebBrowserDemo demonstrates how to use this class in a dialog-based MFC application in which I’ve inserted the WebBrowser control from the Registered ActiveX Controls accessible from Project > Add To Project > Components and Controls menu item.  The sample application DocVwWebBrowserDemo demonstrates how to use this class in a MFC document/view application whose view class is derived from CHtmlView.

If you look at the source code for the class, you’ll figure out how I’ve done it, and then you can add your own methods to the class to customize it further.

 Download source code for the article


Posted in Visual C++

Leave a Reply


