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
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.
- Expand the view class, and then double-click OnInitialUpdate.
- Verify that the source code for this function appears as follows (this example assumes that your view class is called CHtmlShowView):
// 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
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:
- On the Project menu, click Add To Project/Components and Controls. The Components and Controls Gallery dialog box appears.
- Double-click Registered ActiveX controls, click Microsoft Web Browser, and then click Insert. When the confirmation dialog box appears, click OK.
- When the Confirm Classes dialog box appears (which specifies details for an MFC wrapper class called CWebBrowser2), click OK.
- Close the Components and Controls Gallery dialog box.
Use the WebBrowser Control in a Dialog Box
- View your dialog box in the form designer.
- Note that the Toolbox contains an icon for the Microsoft WebBrowser, and then drag the Microsoft WebBrowser control onto your dialog box.
- Right-click the dialog box, click ClassWizard on the shortcut menu, select the WM_INITDIALOG message, click Add Function, and then click Edit Function.
- 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);
pBrowse->Navigate2(sLoc, NULL, NULL, NULL, NULL);
- 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:
For more information about the WebBrowser control and how you can reuse it in programs, see the following MSDN Web site:
Article ID: 315617 - Last Review: April 21, 2006 - Revision: 2.1
- Microsoft Visual Studio 6.0 Enterprise Edition
|kbhowto kbhowtomaster KB315617|