Kirk Evans Blog

.NET From a Markup Perspective

Specify the HTML for a Windows Forms Web Browser Control

Specify the HTML for a Windows Forms Web Browser Control

  • Comments 23

Mike asks: How do I specify the HTML for a web browser control from a string?

The answer is fairly simple, yet I could not find a quick reference link anywhere from a Google search. So, here is the set of steps to use the web browser control and set the HTML for the control using a string.

There is no real magic: you just need 2 different libraries. The WebBrowser's Document property returns an object representing the DOM for a web page. However, this DOM does not exist until a page is loaded. Rather than load a URL from a file, use about:blank for the URL to load a blank page. When you call the Navigate method of the browser, the status text becomes "Opening page about:blank..." When the document is finished loading, the status text changes to "Done". You can leverage this event to know that the browser is finished loading the blank page, at which time the DOM is accessible.

If you are not using Visual Studio .NET, see the Microsoft SDK documentation for instructions on the Windows Forms ActiveX Control Importer. This utility is used to create a Windows Forms control based on the type library information in the ActiveX control.

  • Create a new Windows Form project.
  • Open the WYSIWYG designer for Form1. Right-click the Toolbox and choose Customize Toolbox..., and click the COM Components tab.
  • Browse the SYSTEM32 directory for shdocvw.dll.. Click OK to choose this DLL. Click OK to close the dialog. Your project references now shows a reference to AxShDocVw.dll.
  • Right-click the References tab and choose Add Reference... Click the COM tab. Click the Browse button. Navigate to the SYSTEM32 directory and choose mshtml.tlb. Click OK to close the dialog. Your project references now shows a reference to MSHTML.
  • From the Toolbox pane, drag an Explorer component onto Form1. name this control "browser" in the properties pane. Switch to the code view for the form and enter the following code for the form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using mshtml;

namespace WindowsApplication2
{
	public class Form1 : System.Windows.Forms.Form
	{
		private AxSHDocVw.AxWebBrowser browser;
		private System.ComponentModel.Container components = null;

		public Form1()
		{				
			InitializeComponent();
			string url = "about:blank";
			object o = System.Reflection.Missing.Value;
			browser.Navigate ( url,ref o,ref o,ref o,ref o);
			AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler handler = 
			  new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler 
			  (this.browser_StatusTextChange);
			browser.StatusTextChange += handler;			
		}


		private void browser_StatusTextChange
			(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
		{
			
			mshtml.HTMLDocument doc = (mshtml.HTMLDocument)this.browser.Document;
			doc.body.innerHTML = "<H1>foo</H1>";
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new 
				System.Resources.ResourceManager(typeof(Form1));
			this.browser = new AxSHDocVw.AxWebBrowser();
			((System.ComponentModel.ISupportInitialize)(this.browser)).BeginInit();
			this.SuspendLayout();
			// 
			// browser
			// 
			this.browser.Enabled = true;
			this.browser.Location = new System.Drawing.Point(16, 16);
			this.browser.OcxState = 
				((System.Windows.Forms.AxHost.State)(resources.GetObject("browser.OcxState")));
			this.browser.Size = new System.Drawing.Size(344, 224);
			this.browser.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(392, 302);
			this.Controls.AddRange(new System.Windows.Forms.Control[] { this.browser});
			this.Name = "Form1";
			this.Text = "Form1";
			((System.ComponentModel.ISupportInitialize)(this.browser)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

	}
}
 

That's all there is to it. Once the DOM is available, you have access to the body of the HTMLDocument.

  • Hello,

    I am trying put the Web browser control in an application. I want to load an embedded HTML resource into the web browser control that will contain several link. What I want is to know which link has been clicked and then according to the selected option display the specific windows form etc. How I can do that.

    Best, Nauman
  • It will be your great help if you can e-mail me any information at naumanemails@yahoo.com.

    Thanks, Nauman
  • When attempting to add this form as a child form in a MDI container I don't get an error, but the browser navigation appears to go away and not come back. It runs through the code to set the inner html, but the text never appears. The mouse is using the wait cursor and it just sits there. The application is not locked up, but the browser never shows the content.

    Any ideas?
    [Will look back here for answers]
  • Well done!

    This was very helpful information.
  • I am trying to get the web control to scroll as Asim was but the problem I've found is that the handle returned by browser.Handle is not the correct window handle (as shown by Spy++). If I get the correct handle with Spy++ and use another app to send a message to the browser window, it will scroll correctly. Any ideas on how to get the right handle?
  • can someone hel me ou with this.

    thanks

    mani
Page 2 of 2 (23 items) 12
Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post
Translate This Page
Search
Archive
Archives