Click here to Skip to main content
15,891,828 members
Articles / Desktop Programming / Windows Forms

Zeta HTML Edit Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (60 votes)
9 Jul 2013CPOL3 min read 213K   20.8K   183  
A small wrapper class around the Windows Forms 2.0 WebBrowser control
namespace ZetaHtmlEditControl
{
	using System;
	using System.Diagnostics;
	using System.IO;
	using System.Reflection;
	using System.Security;
	using System.Threading;
	using Microsoft.Win32;

	public static class WebBrowserHelper
	{
		public static void SafeSwitchToHighestInternetExplorerVersionAsync()
		{
			ThreadPool.QueueUserWorkItem(delegate { SafeSwitchToHighestInternetExplorerVersion(); });
		}

		private static void SafeSwitchToHighestInternetExplorerVersion()
		{
			try
			{
				var iv = getInstalledInternetExplorerMajorVersion();

				if (iv >= 10)
				{
					switchWebBrowserToInternetExplorer10();
				}
				if (iv >= 9)
				{
					switchWebBrowserToInternetExplorer9();
				}
				else if (iv >= 8)
				{
					switchWebBrowserToInternetExplorer8();
				}
				else if (iv >= 7)
				{
					switchWebBrowserToInternetExplorer7();
				}
			}
			catch (SecurityException x)
			{
				// Ignore and eat silently.
				Trace.TraceError(
					string.Format(
						@"Ignoring registry exception while setting Internet Explorer mode: {0}", 
						x.Message));
			}
		}

		private static int getInstalledInternetExplorerMajorVersion()
		{
			var key = Registry.LocalMachine.OpenSubKey(
				@"SOFTWARE\Microsoft\Internet Explorer\",
				false);

			if (key == null)
			{
				return 7;
			}
			else
			{
				var raw = key.GetValue(@"Version");
				if (raw == null)
				{
					return 7;
				}
				else
				{
					var raws = raw.ToString().Trim();
					var i = raws.IndexOf('.');
					return i > 0 ? Convert.ToInt32(raws.Substring(0, i)) : 7;
				}
			}
		}

		private static void switchWebBrowserToInternetExplorer7()
		{
			doSwitchWebBrowser(7000);
		}

		private static void switchWebBrowserToInternetExplorer8()
		{
			doSwitchWebBrowser(8000);
		}

		private static void switchWebBrowserToInternetExplorer9()
		{
			doSwitchWebBrowser(9000);
		}

		private static void switchWebBrowserToInternetExplorer10()
		{
			doSwitchWebBrowser(10000);
		}

		private static void doSwitchWebBrowser(uint mode)
		{
			// http://stackoverflow.com/questions/4612255/regarding-ie9-webbrowser-control

			var key = Registry.CurrentUser.OpenSubKey(
				@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
				true);

			if (key != null)
			{
				try
				{
					var name = Path.GetFileName(Assembly.GetEntryAssembly().Location);

					// Check whether exists.
					var old = key.GetValue(name);
					if (old == null || Convert.ToUInt32(old) != mode)
					{
						key.SetValue(
							name,
							mode,
							RegistryValueKind.DWord);
					}
				}
				finally
				{
					key.Close();
				}
			}
		}
	}
}

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
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions