HOW TO: Display HTML Using the WebBrowser Control in a Visual C++/MFC Application

Article translations Article translations
Article ID: 315617 - View products that this article applies to.
This article was previously published under Q315617
Expand all | Collapse all

On This Page


This step-by-step article explains how you can use the WebBrowser control to incorporate HTML displays in a C++ program that uses the Microsoft Foundation Classes (MFC) framework.

You can reuse the WebBrowser component of Internet Explorer 4.0 in programs to display HTML. Microsoft Visual C++ 6.0 contains additional functionality that makes re-use of this component simple.


The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Internet Explorer 4.0 or later
This article assumes that you are a Visual C++ developer who is familiar with the MFC framework.

Create an MFC View Based on HTML

The CHtmlView class provides a view that hosts a WebBrowser control. When you use the AppWizard to create a new MFC single-document or multiple-document program, you can select CHtmlView as the base class instead of the default base class. (This option is not available to dialog-based programs).

To create an MFC view based on HTML, follow these steps:
  1. In Visual Studio 6.0, on the File menu, click New.
  2. In the New dialog box, click the Projects tab, select MFC AppWizard (exe) from the list, enter a suitable project name and location, and then click OK.
  3. In the MFC AppWizard dialog box, click either Single document or Multiple documents, and then click Next.
  4. Follow the instructions in the wizard. In step 6, select the view class from the list, in the Base class list, change the base class to CHtmlView, and then click Finish.
  5. Click OK to confirm your project settings.
  6. If you cannot see the Workspace window, click Workspace on the View menu, and then click the ClassView tab at the bottom of the Workspace window.
  7. Expand the view class, and then double-click OnInitialUpdate.
  8. Verify that the source code for this function appears as follows (this example assumes that your view class is called CHtmlShowView):
    void CHtmlShowView::OnInitialUpdate()
    	// TODO: This code navigates to a popular spot on the Web.
    	// Change the code to go where you want.
In this simple case, the Navigate2 function sets the initial page to the following URL:
You can use Navigate2 (and other methods of the WebBrowser control) to load HTML pages from other URLs, including those based on the http, https and file protocols.

Make the WebBrowser Available to the Program by Using a Dialog Box

The WebBrowser control is an ActiveX control, which means you can use familiar ActiveX techniques to embed the control into a dialog box. When you create MFC applications, make sure that you turn on support for ActiveX controls when you use the AppWizard to create the application.

To make the WebBrowser control available to the program, follow these steps:
  1. On the Project menu, click Add To Project/Components and Controls. The Components and Controls Gallery dialog box appears.
  2. Double-click Registered ActiveX controls, click Microsoft Web Browser, and then click Insert. When the confirmation dialog box appears, click OK.
  3. When the Confirm Classes dialog box appears (which specifies details for an MFC wrapper class called CWebBrowser2), click OK.
  4. Close the Components and Controls Gallery dialog box.

Use the WebBrowser Control in a Dialog Box

  1. View your dialog box in the form designer.
  2. Note that the Toolbox contains an icon for the Microsoft WebBrowser, and then drag the Microsoft WebBrowser control onto your dialog box.
  3. Right-click the dialog box, click ClassWizard on the shortcut menu, select the WM_INITDIALOG message, click Add Function, and then click Edit Function.
  4. Add the following code in this function to set an initial URL of http://localhost (this example assumes that the ID for the WebBrowser control is IDC_EXPLORER1):
    CWebBrowser2 * pBrowse = (CWebBrowser2 *) GetDlgItem(IDC_EXPLORER1);
    COleVariant sLoc("http://localhost");
    pBrowse->Navigate2(sLoc, NULL, NULL, NULL, NULL);
  5. At the top of the .cpp file for your dialog class, locate the existing #include directives, and then add a directive after the existing directives:
    #include "webbrowser2.h"


For more information about the WebBrowser control and how you can reuse it in programs, see the following MSDN Web site:

Reusing the WebBrowser Control


Article ID: 315617 - Last Review: April 21, 2006 - Revision: 2.1
  • Microsoft Visual Studio 6.0 Enterprise Edition
kbhowto kbhowtomaster KB315617

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from