Microsoft KB Archive/315617

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

Article ID: 315617

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft Visual Studio 6.0 Enterprise Edition

-



This article was previously published under Q315617





IN THIS TASK
SUMMARY
 * Requirements
 * Create an MFC View Based on HTML
 * Make the WebBrowser Available to the Program by Using a Dialog Box
 * Use the WebBrowser Control in a Dialog Box

REFERENCES



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

back to the top

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

back to the top

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:  In Visual Studio 6.0, on the File menu, click New. 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. In the MFC AppWizard dialog box, click either Single document or Multiple documents, and then click Next. 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. Click OK to confirm your project settings. 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.</li> Expand the view class, and then double-click OnInitialUpdate.</li>  Verify that the source code for this function appears as follows (this example assumes that your view class is called CHtmlShowView): void CHtmlShowView::OnInitialUpdate {   CHtmlView::OnInitialUpdate;

// TODO: This code navigates to a popular spot on the Web. // Change the code to go where you want. Navigate2(_T(&quot;http://www.microsoft.com/visualc/&quot;),NULL,NULL); }                   </li></ol>

In this simple case, the Navigate2 function sets the initial page to the following URL:

http://www.microsoft.com/visualc

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.

back to the top

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.

back to the top

Use the WebBrowser Control in a Dialog Box
<ol> View your dialog box in the form designer.</li> Note that the Toolbox contains an icon for the Microsoft WebBrowser, and then drag the Microsoft WebBrowser control onto your dialog box.</li> Right-click the dialog box, click ClassWizard on the shortcut menu, select the WM_INITDIALOG message, click Add Function, and then click Edit Function.</li>  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(&quot;http://localhost&quot;); pBrowse->Navigate2(sLoc, NULL, NULL, NULL, NULL); </li>  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: </li></ol>
 * 1) include &quot;webbrowser2.h&quot;

back to the top

<div class="references_section">