Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

WebBrowserControl for the .NET Framework 1.1

Rate me:
Please Sign up or sign in to vote.
3.06/5 (11 votes)
15 Apr 2010CPOL2 min read 98.6K   1.6K   21  
An implementation of a web browser control for the .NET Framework 1.1.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using Pajocomo.Windows.Forms;

namespace WebBrowserTests
{
	/// <summary>
	/// Summary description for WebBrowserForm.
	/// </summary>
	public class WebBrowserForm : System.Windows.Forms.Form
	{
		private static int instances = 0;

		[System.Runtime.InteropServices.ComVisible(true)]
			public class ScriptObject
		{
			public string Host
			{
				get
				{
					return this.GetType().FullName;
				}
			}

			private string local;
			public string LocalSet(string variable, string value)
			{
				this.local = value;
				return this.local;
			}

			public string LocalGet(string variable)
			{
				return this.local;
			}

			private string teste;

			public string Teste
			{
				get
				{
					return teste;
				}
				set
				{
					teste = value;
				}
			}
		}

		private WebBrowserControl webBrowserControl;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.TextBox addressText;
		private System.Windows.Forms.Button goButton;
		private System.Windows.Forms.Label statusLabel;
		private System.Windows.Forms.Button refreshButton;
		private System.Windows.Forms.ProgressBar progressBar;
		private System.Windows.Forms.Panel controlPanel;
		private System.Windows.Forms.Panel statusPanel;
		private System.Windows.Forms.PropertyGrid webBrowserPropertyGrid;
		private System.Windows.Forms.Panel webBrowserPanel;
		private readonly int instance;
		private System.Windows.Forms.ToolTip toolTip;
		private System.Windows.Forms.Splitter controlSplitter;
		private System.Windows.Forms.Splitter statusSplitter;
		private System.Windows.Forms.Button stopButton;
		private System.Windows.Forms.Button forwardButton;
		private System.Windows.Forms.CheckBox propertiesCheckBox;
		private System.Windows.Forms.Button commandButton;
		private System.Windows.Forms.Button backButton;
		private System.Windows.Forms.Label privacyLabel;
		private System.Windows.Forms.Button printPreviewButton;
		private System.Windows.Forms.Label encryptionLabel;

		public WebBrowserForm()
		{
			this.instance = ++instances;

			InitializeComponent();

			string s = WebBrowserControl.DefaultUserAgent;

//			this.webBrowserPropertyGrid.SelectedObject = this.webBrowserControl;

			this.webBrowserControl.ProcessUrlAction += new ProcessUrlActionEventHandler(webBrowserControl_ProcessUrlAction);
			this.webBrowserControl.ShowMessage += new ShowMessageEventHandler(webBrowserControl_ShowMessage);
			this.webBrowserControl.WindowSizeChanged += new WindowSizeChangedEventHandler(this.webBrowserControl_WindowSizeChanged);
			this.webBrowserControl.WindowLocationChanged += new WindowLocationChangedEventHandler(this.webBrowserControl_WindowLocationChanged);
			this.webBrowserControl.NewWindow += new WebBrowserNewWindowEventHandler(webBrowserControl_NewWindow);
			this.webBrowserControl.Navigated += new WebBrowserNavigatedEventHandler(webBrowserControl_Navigated);
			this.webBrowserControl.Navigating += new WebBrowserNavigatingEventHandler(webBrowserControl_Navigating);
			this.webBrowserControl.NavigateError += new WebBrowserNavigateErrorEventHandler(webBrowserControl_NavigateError);
			this.webBrowserControl.CommandStateChanged += new WebBrowserCommandStateChangedEventHandler(webBrowserControl_CommandStateChanged);
			this.webBrowserControl.DocumentCompleted += new WebBrowserNavigatedEventHandler(webBrowserControl_DocumentCompleted);
			this.webBrowserControl.Downloading += new EventHandler(webBrowserControl_Downloading);
			this.webBrowserControl.Downloaded += new EventHandler(webBrowserControl_Downloaded);
			this.webBrowserControl.FileDownloading += new WebBrowserFileDownloadingEventHandler(webBrowserControl_FileDownloading);
			this.webBrowserControl.StatusTextChanged += new WebBrowserStatusTextChangedEventHandler(webBrowserControl_StatusTextChanged);
			this.webBrowserControl.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowserControl_ProgressChanged);
			this.webBrowserControl.LocationNameChanged += new WebBrowserLocationNameChangedEventHandler(webBrowserControl_LocationNameChanged);
			this.webBrowserControl.Closing += new WebBrowserWindowClosingEventHandler(webBrowserControl_Closing);
			this.webBrowserControl.KeyPress += new KeyPressEventHandler(webBrowserControl_KeyPress);
			this.webBrowserControl.KeyUp += new KeyEventHandler(webBrowserControl_KeyUp);
			this.webBrowserControl.KeyDown += new KeyEventHandler(webBrowserControl_KeyDown);
			this.webBrowserControl.PrivacyImpactedStateChanged += new WebBrowserPrivacyImpactedStateChangedEventHandler(webBrowserControl_PrivacyImpactedStateChanged);
			this.webBrowserControl.EncryptionLevelChanged += new WebBrowserEncryptionLevelChangedEventHandler(webBrowserControl_EncryptionLevelChanged);

			this.webBrowserControl.ObjectForScripting = new ScriptObject();

			this.addressText.Text = Application.StartupPath + @"\HTMLPage.htm";

			this.Text = "WebBrowser[" + this.instance.ToString() + "]";
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WebBrowserForm));
			this.webBrowserControl = new Pajocomo.Windows.Forms.WebBrowserControl();
			this.addressText = new System.Windows.Forms.TextBox();
			this.goButton = new System.Windows.Forms.Button();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.statusLabel = new System.Windows.Forms.Label();
			this.refreshButton = new System.Windows.Forms.Button();
			this.statusPanel = new System.Windows.Forms.Panel();
			this.encryptionLabel = new System.Windows.Forms.Label();
			this.privacyLabel = new System.Windows.Forms.Label();
			this.controlPanel = new System.Windows.Forms.Panel();
			this.propertiesCheckBox = new System.Windows.Forms.CheckBox();
			this.stopButton = new System.Windows.Forms.Button();
			this.backButton = new System.Windows.Forms.Button();
			this.forwardButton = new System.Windows.Forms.Button();
			this.commandButton = new System.Windows.Forms.Button();
			this.printPreviewButton = new System.Windows.Forms.Button();
			this.webBrowserPanel = new System.Windows.Forms.Panel();
			this.webBrowserPropertyGrid = new System.Windows.Forms.PropertyGrid();
			this.toolTip = new System.Windows.Forms.ToolTip(this.components);
			this.controlSplitter = new System.Windows.Forms.Splitter();
			this.statusSplitter = new System.Windows.Forms.Splitter();
			((System.ComponentModel.ISupportInitialize)(this.webBrowserControl)).BeginInit();
			this.statusPanel.SuspendLayout();
			this.controlPanel.SuspendLayout();
			this.webBrowserPanel.SuspendLayout();
			this.SuspendLayout();
			// 
			// webBrowserControl
			// 
			this.webBrowserControl.ContainingControl = this;
			this.webBrowserControl.Dock = System.Windows.Forms.DockStyle.Fill;
			this.webBrowserControl.Enabled = true;
			this.webBrowserControl.Location = new System.Drawing.Point(0, 0);
			this.webBrowserControl.Name = "webBrowserControl";
			this.webBrowserControl.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("webBrowserControl.OcxState")));
			this.webBrowserControl.Size = new System.Drawing.Size(792, 514);
			this.webBrowserControl.TabIndex = 7;
			// 
			// addressText
			// 
			this.addressText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.addressText.Location = new System.Drawing.Point(68, 4);
			this.addressText.Name = "addressText";
			this.addressText.Size = new System.Drawing.Size(616, 20);
			this.addressText.TabIndex = 1;
			this.addressText.Text = "";
			this.toolTip.SetToolTip(this.addressText, "Address");
			// 
			// goButton
			// 
			this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.goButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.goButton.Location = new System.Drawing.Point(688, 4);
			this.goButton.Name = "goButton";
			this.goButton.Size = new System.Drawing.Size(20, 20);
			this.goButton.TabIndex = 3;
			this.goButton.Text = ">";
			this.toolTip.SetToolTip(this.goButton, "Go");
			this.goButton.Click += new System.EventHandler(this.goButton_Click);
			// 
			// progressBar
			// 
			this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.progressBar.Location = new System.Drawing.Point(670, 4);
			this.progressBar.Name = "progressBar";
			this.progressBar.Size = new System.Drawing.Size(120, 20);
			this.progressBar.TabIndex = 5;
			this.toolTip.SetToolTip(this.progressBar, "Progress");
			this.progressBar.Visible = false;
			// 
			// statusLabel
			// 
			this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.statusLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.statusLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.statusLabel.Location = new System.Drawing.Point(4, 4);
			this.statusLabel.Name = "statusLabel";
			this.statusLabel.Size = new System.Drawing.Size(612, 20);
			this.statusLabel.TabIndex = 6;
			this.statusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// refreshButton
			// 
			this.refreshButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.refreshButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.refreshButton.Location = new System.Drawing.Point(708, 4);
			this.refreshButton.Name = "refreshButton";
			this.refreshButton.Size = new System.Drawing.Size(20, 20);
			this.refreshButton.TabIndex = 3;
			this.refreshButton.Text = "R";
			this.toolTip.SetToolTip(this.refreshButton, "Refresh");
			this.refreshButton.Click += new System.EventHandler(this.refreshButton_Click);
			// 
			// statusPanel
			// 
			this.statusPanel.Controls.Add(this.encryptionLabel);
			this.statusPanel.Controls.Add(this.privacyLabel);
			this.statusPanel.Controls.Add(this.statusLabel);
			this.statusPanel.Controls.Add(this.progressBar);
			this.statusPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.statusPanel.Location = new System.Drawing.Point(0, 544);
			this.statusPanel.Name = "statusPanel";
			this.statusPanel.Size = new System.Drawing.Size(792, 28);
			this.statusPanel.TabIndex = 5;
			// 
			// encryptionLabel
			// 
			this.encryptionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.encryptionLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.encryptionLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.encryptionLabel.Location = new System.Drawing.Point(620, 4);
			this.encryptionLabel.Name = "encryptionLabel";
			this.encryptionLabel.Size = new System.Drawing.Size(20, 20);
			this.encryptionLabel.TabIndex = 8;
			this.encryptionLabel.Text = "E";
			this.encryptionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip.SetToolTip(this.encryptionLabel, "Encryption Level");
			this.encryptionLabel.Visible = false;
			// 
			// privacyLabel
			// 
			this.privacyLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.privacyLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.privacyLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.privacyLabel.Location = new System.Drawing.Point(644, 4);
			this.privacyLabel.Name = "privacyLabel";
			this.privacyLabel.Size = new System.Drawing.Size(20, 20);
			this.privacyLabel.TabIndex = 7;
			this.privacyLabel.Text = "P";
			this.privacyLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip.SetToolTip(this.privacyLabel, "Privacy Impacted");
			this.privacyLabel.Visible = false;
			// 
			// controlPanel
			// 
			this.controlPanel.Controls.Add(this.propertiesCheckBox);
			this.controlPanel.Controls.Add(this.goButton);
			this.controlPanel.Controls.Add(this.addressText);
			this.controlPanel.Controls.Add(this.refreshButton);
			this.controlPanel.Controls.Add(this.stopButton);
			this.controlPanel.Controls.Add(this.backButton);
			this.controlPanel.Controls.Add(this.forwardButton);
			this.controlPanel.Controls.Add(this.commandButton);
			this.controlPanel.Controls.Add(this.printPreviewButton);
			this.controlPanel.Dock = System.Windows.Forms.DockStyle.Top;
			this.controlPanel.Location = new System.Drawing.Point(0, 0);
			this.controlPanel.Name = "controlPanel";
			this.controlPanel.Size = new System.Drawing.Size(792, 28);
			this.controlPanel.TabIndex = 0;
			// 
			// propertiesCheckBox
			// 
			this.propertiesCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.propertiesCheckBox.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.propertiesCheckBox.Location = new System.Drawing.Point(768, 4);
			this.propertiesCheckBox.Name = "propertiesCheckBox";
			this.propertiesCheckBox.Size = new System.Drawing.Size(20, 20);
			this.propertiesCheckBox.TabIndex = 4;
			this.propertiesCheckBox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip.SetToolTip(this.propertiesCheckBox, "Properties");
			this.propertiesCheckBox.CheckedChanged += new System.EventHandler(this.propertiesCheckBox_CheckedChanged);
			// 
			// stopButton
			// 
			this.stopButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.stopButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.stopButton.Location = new System.Drawing.Point(728, 4);
			this.stopButton.Name = "stopButton";
			this.stopButton.Size = new System.Drawing.Size(20, 20);
			this.stopButton.TabIndex = 3;
			this.stopButton.Text = "S";
			this.toolTip.SetToolTip(this.stopButton, "Stop");
			this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
			// 
			// backButton
			// 
			this.backButton.Enabled = false;
			this.backButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.backButton.Location = new System.Drawing.Point(24, 4);
			this.backButton.Name = "backButton";
			this.backButton.Size = new System.Drawing.Size(20, 20);
			this.backButton.TabIndex = 3;
			this.backButton.Text = "<";
			this.toolTip.SetToolTip(this.backButton, "Back");
			this.backButton.Click += new System.EventHandler(this.backButton_Click);
			// 
			// forwardButton
			// 
			this.forwardButton.Enabled = false;
			this.forwardButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.forwardButton.Location = new System.Drawing.Point(44, 4);
			this.forwardButton.Name = "forwardButton";
			this.forwardButton.Size = new System.Drawing.Size(20, 20);
			this.forwardButton.TabIndex = 3;
			this.forwardButton.Text = ">";
			this.toolTip.SetToolTip(this.forwardButton, "Forward");
			this.forwardButton.Click += new System.EventHandler(this.forwardButton_Click);
			// 
			// commandButton
			// 
			this.commandButton.Enabled = false;
			this.commandButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.commandButton.Location = new System.Drawing.Point(4, 4);
			this.commandButton.Name = "commandButton";
			this.commandButton.Size = new System.Drawing.Size(20, 20);
			this.commandButton.TabIndex = 3;
			this.toolTip.SetToolTip(this.commandButton, "Commands");
			// 
			// printPreviewButton
			// 
			this.printPreviewButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.printPreviewButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.printPreviewButton.Location = new System.Drawing.Point(748, 4);
			this.printPreviewButton.Name = "printPreviewButton";
			this.printPreviewButton.Size = new System.Drawing.Size(20, 20);
			this.printPreviewButton.TabIndex = 3;
			this.printPreviewButton.Text = "P";
			this.toolTip.SetToolTip(this.printPreviewButton, "Print Preview Dialog");
			this.printPreviewButton.Click += new System.EventHandler(this.printPreviewButton_Click);
			// 
			// webBrowserPanel
			// 
			this.webBrowserPanel.Controls.Add(this.webBrowserPropertyGrid);
			this.webBrowserPanel.Controls.Add(this.webBrowserControl);
			this.webBrowserPanel.Dock = System.Windows.Forms.DockStyle.Fill;
			this.webBrowserPanel.Location = new System.Drawing.Point(0, 29);
			this.webBrowserPanel.Name = "webBrowserPanel";
			this.webBrowserPanel.Size = new System.Drawing.Size(792, 514);
			this.webBrowserPanel.TabIndex = 1;
			// 
			// webBrowserPropertyGrid
			// 
			this.webBrowserPropertyGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.webBrowserPropertyGrid.CommandsVisibleIfAvailable = true;
			this.webBrowserPropertyGrid.LargeButtons = false;
			this.webBrowserPropertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
			this.webBrowserPropertyGrid.Location = new System.Drawing.Point(592, 0);
			this.webBrowserPropertyGrid.Name = "webBrowserPropertyGrid";
			this.webBrowserPropertyGrid.Size = new System.Drawing.Size(200, 514);
			this.webBrowserPropertyGrid.TabIndex = 5;
			this.webBrowserPropertyGrid.Text = "PropertyGrid";
			this.webBrowserPropertyGrid.ViewBackColor = System.Drawing.SystemColors.Window;
			this.webBrowserPropertyGrid.ViewForeColor = System.Drawing.SystemColors.WindowText;
			this.webBrowserPropertyGrid.Visible = false;
			// 
			// controlSplitter
			// 
			this.controlSplitter.Dock = System.Windows.Forms.DockStyle.Top;
			this.controlSplitter.Enabled = false;
			this.controlSplitter.Location = new System.Drawing.Point(0, 28);
			this.controlSplitter.Name = "controlSplitter";
			this.controlSplitter.Size = new System.Drawing.Size(792, 1);
			this.controlSplitter.TabIndex = 4;
			this.controlSplitter.TabStop = false;
			// 
			// statusSplitter
			// 
			this.statusSplitter.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.statusSplitter.Enabled = false;
			this.statusSplitter.Location = new System.Drawing.Point(0, 543);
			this.statusSplitter.Name = "statusSplitter";
			this.statusSplitter.Size = new System.Drawing.Size(792, 1);
			this.statusSplitter.TabIndex = 7;
			this.statusSplitter.TabStop = false;
			// 
			// WebBrowserForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 572);
			this.Controls.Add(this.webBrowserPanel);
			this.Controls.Add(this.controlSplitter);
			this.Controls.Add(this.controlPanel);
			this.Controls.Add(this.statusSplitter);
			this.Controls.Add(this.statusPanel);
			this.Name = "WebBrowserForm";
			this.Text = "WebBrowser";
			((System.ComponentModel.ISupportInitialize)(this.webBrowserControl)).EndInit();
			this.statusPanel.ResumeLayout(false);
			this.controlPanel.ResumeLayout(false);
			this.webBrowserPanel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			try
			{
				Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
				Application.EnableVisualStyles();
				Application.DoEvents();
				Application.Run(new WebBrowserForm());
			}
			catch (Exception ex)
			{
				ReportException(ex);
			}
		}

		int windowPosChanging = 0;

		protected override void WndProc(ref Message m)
		{
			const int WM_WINDOWPOSCHANGED = 0x0047;
//			Trace.WriteLine(">> Form::WndProc Msg="+m.Msg.ToString("X"));
//			Trace.Indent();
			switch (m.Msg)
			{
				case WM_WINDOWPOSCHANGED:
					this.windowPosChanging++;
					break;
			}
			base.WndProc (ref m);
			switch (m.Msg)
			{
				case WM_WINDOWPOSCHANGED:
					this.windowPosChanging--;
					break;
			}
//			Trace.Unindent();
//			Trace.WriteLine("<< Form::WndProc Msg="+m.Msg.ToString("X"));
		}

		private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
		{
			ReportException(e.Exception);
		}

		private static void ReportException(Exception exception)
		{
			Trace.WriteLine(exception.ToString());
		}

		private void goButton_Click(object sender, System.EventArgs e)
		{
			this.webBrowserControl.Navigate(this.addressText.Text);
		}

		private void refreshButton_Click(object sender, System.EventArgs e)
		{
			this.webBrowserControl.Refresh();
		}

		private void backButton_Click(object sender, System.EventArgs e)
		{
			this.webBrowserControl.GoBack();
		}

		private void forwardButton_Click(object sender, System.EventArgs e)
		{
			this.webBrowserControl.GoForward();
		}

		private void stopButton_Click(object sender, System.EventArgs e)
		{
			this.webBrowserControl.Stop();
		}

		private void webBrowserControl_Closing(object sender, WebBrowserWindowClosingEventArgs e)
		{
			Trace.WriteLine(string.Format("IsChildWindow={1}, Cancel={0}", e.Cancel, e.IsChildWindow), "[" + this.instance.ToString() + "] WindowClosing");
			this.Close();
		}

		private void webBrowserControl_NewWindow(object sender, WebBrowserNewWindowEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\", UrlContext=\"{1}\", Flags={2})", e.Url, e.UrlContext, e.Flags), "[" + this.instance.ToString() + "] NewWindowEx");
			WebBrowserForm form = new WebBrowserForm();
			form.Show();
			e.WebBrowser = form.webBrowserControl.WebBrowser;
		}

		private void webBrowserControl_WindowSizeChanged(object sender, WindowSizeChangedEventArgs e)
		{
			if (this.windowPosChanging == 0)
			{
				Trace.WriteLine("WindowSizeChanged: " + e.WindowSize.ToString());
				this.ClientSize = new Size(e.WindowSize.Width, e.WindowSize.Height + this.controlPanel.Height + this.controlSplitter.Height + this.statusPanel.Height + this.statusSplitter.Height);
			}
		}

		private void webBrowserControl_WindowLocationChanged(object sender, WindowLocationChangedEventArgs e)
		{
			if (this.windowPosChanging == 0)
			{
				if (e.WindowLocation.X >= 0)
				{
					Trace.WriteLine("WindowLocationChanged: X=" + e.WindowLocation.X.ToString());
					this.Left = e.WindowLocation.X;
				}
				if (e.WindowLocation.Y >= 0)
				{
					Trace.WriteLine("WindowLocationChanged: Y=" + e.WindowLocation.Y.ToString());
					this.Top = e.WindowLocation.Y;
				}
			}
		}

		private void webBrowserControl_ShowMessage(object sender, ShowMessageEventArgs e)
		{
			e.Handled = true;
			Trace.WriteLine(string.Format("Caption=\"{0}\", Text=\"{1}\", Buttons={2}, Icon={3})", e.Caption, e.Text, e.Buttons, e.Icon), ">> [" + this.instance.ToString() + "] ShowMessage");
			e.Result = MessageBox.Show(this, e.Text, "WebBrowserControl", e.Buttons, e.Icon);
			Trace.WriteLine(string.Format("Caption=\"{0}\", Text=\"{1}\", Buttons={2}, Icon={3}, Result={4})", e.Caption, e.Text, e.Buttons, e.Icon, e.Result), "<< [" + this.instance.ToString() + "] ShowMessage");
		}

		private void webBrowserControl_ProcessUrlAction(object sender, ProcessUrlActionEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\", Context=\"{1}\", UrlAction={2}, UrlPolicy={3}, Flags={4})", e.Url, ((e.HasContext) ? e.Context.ToString() : string.Empty), e.UrlAction, e.UrlPolicy, e.Flags), "[" + this.instance.ToString() + "] ProcessUrlAction");
		}

		private void webBrowserControl_StatusTextChanged(object sender, WebBrowserStatusTextChangedEventArgs e)
		{
			this.statusLabel.Text = e.StatusText;
			Trace.WriteLine(string.Format("StatusText=\"{0}\"", e.StatusText), "[" + this.instance.ToString() + "] StatusTextChanged");
		}

		private void webBrowserControl_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
		{
			Trace.WriteLine(string.Format("Progress={0}, ProgressMax={1}", e.Progress, e.ProgressMax), "[" + this.instance.ToString() + "] ProgressChanged");
			try
			{
				if (e.Progress < e.ProgressMax && e.ProgressMax > 0 && e.Progress > 0)
				{
					this.progressBar.Visible = true;
					if (e.Progress > this.progressBar.Maximum)
					{
						this.progressBar.Maximum = e.ProgressMax;
						this.progressBar.Value = e.Progress;
					}
					else
					{
						this.progressBar.Value = e.Progress;
						this.progressBar.Maximum = e.ProgressMax;
					}
				}
				else
				{
					this.progressBar.Visible = false;
					this.progressBar.Value = 0;
					this.progressBar.Maximum = 0;
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString(), "[" + this.instance.ToString() + "] ProgressChanged");
			}
		}

		private void webBrowserControl_LocationNameChanged(object sender, WebBrowserLocationNameChangedEventArgs e)
		{
			this.Text = "WebBrowser[" + this.instance.ToString() + "] - " + e.LocationName;
		}

		private void webBrowserControl_KeyPress(object sender, KeyPressEventArgs e)
		{
			Trace.WriteLine(string.Format("KeyChar={0}", e.KeyChar), "[" + this.instance.ToString() + "] KeyPress");
		}

		private void webBrowserControl_KeyUp(object sender, KeyEventArgs e)
		{
			Trace.WriteLine(string.Format("KeyCode={0}, KeyData={1}, KeyValue={2}, Modifiers={3}", e.KeyCode, e.KeyData, e.KeyValue, e.Modifiers), "[" + this.instance.ToString() + "] KeyUp");
		}

		private void webBrowserControl_KeyDown(object sender, KeyEventArgs e)
		{
			Trace.WriteLine(string.Format("KeyCode={0}, KeyData={1}, KeyValue={2}, Modifiers={3}", e.KeyCode, e.KeyData, e.KeyValue, e.Modifiers), "[" + this.instance.ToString() + "] KeyDown");
		}

		private void webBrowserControl_CommandStateChanged(object sender, WebBrowserCommandStateChangedEventArgs e)
		{
			Trace.WriteLine(string.Format("Command={0}, Enable={1}", e.Command, e.Enable), "[" + this.instance.ToString() + "] CommandStateChanged");

			switch (e.Command)
			{
				case WebBrowserCommandStateChangeFlags.UpdateCommands:
					this.commandButton.Enabled = e.Enable;
					break;
				case WebBrowserCommandStateChangeFlags.NavigateBack:
					this.backButton.Enabled = e.Enable;
					break;
				case WebBrowserCommandStateChangeFlags.NavigateForward:
					this.forwardButton.Enabled = e.Enable;
					break;
			}
		}

		private void webBrowserControl_Navigated(object sender, WebBrowserNavigatedEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\"", e.Url), "[" + this.instance.ToString() + "] Navigated");
			if (e.WebBrowser.Equals(this.webBrowserControl.WebBrowser))
			{
				this.addressText.Text = e.Url;
			}
		}

		private void webBrowserControl_Navigating(object sender, WebBrowserNavigatingEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\", TargetFrameName=\"{1}\", PostData=\"{1}\", Headers=\"{2}\"", e.Url, e.TargetFrameName, Convert.ToString(e.PostData), e.Headers), "[" + this.instance.ToString() + "] Navigating");
		}

		private void webBrowserControl_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\", TargetFrameName=\"{1}\", StatusCode={2}", e.Url, e.TargetFrameName, e.StatusCode), "[" + this.instance.ToString() + "] NavigateError");
		}

		private void webBrowserControl_DocumentCompleted(object sender, WebBrowserNavigatedEventArgs e)
		{
			Trace.WriteLine(string.Format("Url=\"{0}\"", e.Url), "[" + this.instance.ToString() + "] DocumentCompleted");
		}

		private void webBrowserControl_Downloading(object sender, EventArgs e)
		{
			Trace.WriteLine(string.Empty, "[" + this.instance.ToString() + "] Downloading");
		}

		private void webBrowserControl_Downloaded(object sender, EventArgs e)
		{
			Trace.WriteLine(string.Empty, "[" + this.instance.ToString() + "] Downloaded");
		}

		private void webBrowserControl_FileDownloading(object sender, WebBrowserFileDownloadingEventArgs e)
		{
			Trace.WriteLine(string.Empty, "[" + this.instance.ToString() + "] FileDownloading");
		}

		private void webBrowserControl_EncryptionLevelChanged(object sender, WebBrowserEncryptionLevelChangedEventArgs e)
		{
			if (e.EncryptionLevel != WebBrowserEncryptionLevels.Unsecure)
			{
				this.toolTip.SetToolTip(this.encryptionLabel, "Encryption Level: " + e.EncryptionLevel.ToString());
				this.encryptionLabel.Visible = true;
			}
			else
			{
				this.encryptionLabel.Visible = false;
			}
		}

		private void webBrowserControl_PrivacyImpactedStateChanged(object sender, WebBrowserPrivacyImpactedStateChangedEventArgs e)
		{
			this.privacyLabel.Visible = e.Impacted;
		}

		private void propertiesCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.webBrowserPropertyGrid.Visible = this.propertiesCheckBox.Checked)
			{
				this.webBrowserPropertyGrid.SelectedObject = this.webBrowserControl;
				this.webBrowserPropertyGrid.BringToFront();
			}
			else
			{
				this.webBrowserPropertyGrid.SelectedObject = null;
			}
		}

		private void printPreviewButton_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (this.webBrowserControl.CanShowPrintPreviewDialog())
				{
					this.webBrowserControl.ShowPrintPreviewDialog();
				}
			}
			catch (Exception ex)
			{
				System.Diagnostics.Trace.WriteLine(ex);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions