Snippet: Embedding a web control in C# · 27 March 2005, 22:01

Code level support

Often it’s useful to have a web browser control to embed in your application. The Microsoft .NET 1.x platform does not have a built-in browser control but Windows does. This fact can be leveraged to get one that .NET can use.

Internet Explorer’s functionality is contained in the WebBrowser class in the Shell Document Object and Control Library DLL (SHDocVw.dll). It’s exposed as an ActiveX control.

.NET Windows Forms cannot directly host ActiveX controls. Windows Forms need controls to be derived from System.Windows.Forms.Control. Fortunately, Microsoft provides the AxImp.exe, a utility to create an assembly for an ActiveX control. To use it fire up a command shell that has the Visual Studio environment and run the following:

   aximp “SystemRoot\system32\SHDocVw.dll”

This will generate two assemblies: SHDocVw.dll and AxSHDocVw.dll. The CLR proxies for the COM types are in SHDocVw.dll and the Windows Forms proxies for the ActiveX controls are in AxSHDocVw.dll These proxies are wrapped to appear like any .NET control. Their classes are created in a namespace that’s the same name as the assembly: AxSHDocVw. These proxy assembles are called runtime callable wrappers or RCW for short.

Using the WebBrowser is quite simple. Fire up Visual Studio and create a new C# project with Windows Forms support. Add a reference to the assemblies AxSHDocVw.dll and SHDocVw.dll. Then enter the following code:

  using System;
  using System.Drawing;
  using System.Windows.Forms;

  namespace SimpleBrowser 
  {
    public class SimpleBrowser 
    {
      [STAThread]
      public static void Main() 
      { 
        // Create the main window form
        Form simpleBrowserForm = new Form();
        simpleBrowserForm.Text = “Simple Browser”;
        simpleBrowserForm.Size = new Size( 600, 500 );

        // Create the browser control
        AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser();
        browser.Dock = DockStyle.Fill;
        simpleBrowserForm.Controls.Add( browser );

        // Show the window and browse to a place where 
        // you can get to everywhere.
        simpleBrowserForm.Show();
        browser.Navigate( “http://www.google.com” );

        // Main loop
        Application.Run( simpleBrowserForm );
      }
    }
  }

SimpleBrowser.cs


Simple Browser using Internet Explorer


Simple Browser using Mozilla


This code does not use the Visual Studio UI designer to keep it simple for tutorial purposes. If you build and run it you should see something like the image to the left.

The critical piece of code is AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser(); which creates the WebBrowser control. After that you can treat it like any other .NET control.

If you want this functionality but don’t want to use Internet Exploiter^H^H^H^Hrer in your application there’s another solution for you. With a little more work you can use the Mozilla ActiveX Control. This is a project that implements the same COM interfaces as IE but uses Mozilla instead.

The steps are same as for IE but with the following differences:

  1. Install the Mozilla ActiveX Control
  2. Run AxImp on mozctl.dll (located in the directory where you installed the Mozilla ActiveX Control)
  3. Add assembly references to MOZILLACONTROLLib.dll and AxMOZILLACONTROLLib.dll in the project.
  4. Change AxSHDocVw.AxWebBrowser browser = new AxSHDocVw.AxWebBrowser() to AxMOZILLACONTROLLib.AxMozillaBrowser browser = new AxMOZILLACONTROLLib.AxMozillaBrowser(). See SimpleMozBrowser.cs for details.

The downside to this approach is that you’ll need to redistribute the Mozilla ActiveX Control package with your installer. A possible work-around is since they use the some interface you could have a function to determine if the Mozilla ActiveX Control is installed and use it otherwise fall back to the Internet Explorer control.

Download the Visual Studio 2003 solution for this snippet.

Part 2: Adding design time support

* * *