Using MFC to Host a WebBrowser Control

Dec 16, 2017 20:20

  • Add a WebBrowser control.You now have a skeleton application. Because this example uses a dialog-based application, a dialog box with OK and CANCELbuttons appears in the Dialog Editor. Follow these steps to add an ActiveX control to the dialog box.
  1. Right-click the Dialog Editor.
  2. Select Insert ActiveX Control from the menu.
  3. Select Microsoft Web Browser.
  4. Click OK.
  5. Position and size the WebBrowser control in the Dialog Editor.
  6. Delete the default OK and CANCEL buttons if your application does not require them.

Add a WebBrowser class and a member variable.

When you insert a WebBrowser control, an identifier for the control is automatically assigned, but you must provide a member variable to access the control. To add a variable:

  • Right-click the WebBrowser control.
  • Select ClassWizard.
  • Click the Member Variables tab to display the control identifiers.
  • Select IDC_EXPLORER1.

Click Add Variable, and the following dialog box appears:



  • Click OK to display a Confirm Classes dialog box.
  • Click OK again to add a CWebBrowser2 class to your project.
  • Enter a name for the control variable.

You now have an application containing a browser. However, if you compile the code generated by Visual C++ and run the executable file, a browser does not appear.


  • Display the WebBrowser control.To display the WebBrowser control, your application must navigate to a URL. The following example uses the IWebBrowser2::Navigate method to open the Microsoft home page in a WebBrowser control.
    //CWebBrowser2 m_browser - member variable m_browser.Navigate("www.microsoft.com", NULL, NULL, NULL, NULL);  

iwebbrowser2, dialog, webbrowser control, mfc

Previous post Next post
Up