[zz]Specify the HTML for a Windows Forms Web Browser Control

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.

出处:http://blogs.msdn.com/kaevans/archive/2003/02/25/2936.aspx

你可能感兴趣的:(windows)