Go to content Go to navigation Go to search
Snippet: Design time support for a web control in C# · 2005-03-28 08:42

Part 1: Embedding a web control in C#

Design time support

Although it’s nice to be able to create controls by hand, in most cases developers want controls to be available at design-time. For Visual Studio users this capability is almost built-in. Visual Studio will even automatically wrap ActiveX controls for you (eliminating needing to call AxImp.exe by hand).

To take advantage of this right-click Windows Forms Toolbox and select “Add/Remove Items…”. In the “Customize ToolBox” dialog select the COM Components tab. Then scroll through the list and put a check next to “Microsoft Web Browser” and “MozillaBrowser Class” if you have the Mozilla ActiveX Control installed. Then press OK and you should see two entries added to the toolbox.

You can now drag these onto your Windows Forms application and go to town with them. If you look in the output directory you’ll find the ActiveX wrapper assemblies (runtime callable wrapper or RCW assemblies).

Now, even though this seems simple there are some problems with this technique. Probably the biggest problem is a problem with all ActiveX and other COM controls: keeping track of them. It’s very easy to use third-party controls, even by accident. Since they are early-bound, if they are not present on the user’s machine the program using them will not even load. So keeping track of what controls are installed on your machine and testing on a cleanly-installed system are essential. VMWare Workstation is a life-saver for this.

Second, discovery is a pain. Knowing that Microsoft or a third-party has provided the functionality that you want is hard enough but can usually be found by searching documentation or the web. However, even when it’s installed on your machine they can be a chore to find. The “Customize Toolbox” is terrible. It’s not sizeable (all browsers should be sizeable) and it desperately needs at least a search filter to pare down the list of hundreds of items. Since it lacks these basic features and I usually know the name of the DLL and where it’s installed I usually sort on the “Path” column and use that to find the control I’m looking for.

Snippet: Embedding a web control in C# · 2005-03-27 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