Click here to Skip to main content
15,897,183 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.8K   1.6K   21  
An implementation of a web browser control for the .NET Framework 1.1.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using mshtml;

namespace Pajocomo.Windows.Forms
{
	/// <summary>
	/// Represents a new instance of the <see cref="WebBrowserControlSite"/> class
	/// and implements the <see cref="T:Interop.IOleClientSite"/>,
	/// <see cref="T:Interop.IOleCommandTarget"/>,
	/// <see cref="T:Interop.IDocHostUIHandler"/>,
	/// <see cref="T:Interop.IDocHostShowUI"/>,
	/// <see cref="T:Interop.IServiceProvider"/> interfaces.
	/// </summary>
	public class WebBrowserControlSite :
		Interop.IOleClientSite,
		Interop.IOleCommandTarget,
		Interop.IDocHostUIHandler,
		Interop.IDocHostShowUI,
		Interop.IServiceProvider
	{
		private WebBrowserControl webBrowserControl;
		private InternetSecurityManagerService internetSecurityManagerService;

		/// <summary>
		/// Initializes a new instance of the <see cref="WebBrowserControlSite"/> class.
		/// </summary>
		/// <param name="webBrowserControl">The web browser control.</param>
		public WebBrowserControlSite(WebBrowserControl webBrowserControl)
		{
			this.webBrowserControl = webBrowserControl;
			this.internetSecurityManagerService = new InternetSecurityManagerService(this.webBrowserControl);
		}
		#region Interop.IOleClientSite Members

		Interop.IOleContainer Interop.IOleClientSite.GetContainer()
		{
			throw new COMException(string.Empty, Interop.HRESULT.E_NOINTERFACE);
		}

		int Interop.IOleClientSite.GetMoniker(uint dwAssign, uint dwWhichMoniker, out object ppmk)
		{
			ppmk = null;
			return Interop.HRESULT.E_NOTIMPL;
		}

		int Interop.IOleClientSite.OnShowWindow(int fShow)
		{
			return Interop.HRESULT.FALSE;
		}

		int Interop.IOleClientSite.RequestNewObjectLayout()
		{
			return Interop.HRESULT.E_NOTIMPL;
		}

		int Interop.IOleClientSite.SaveObject()
		{
			return Interop.HRESULT.E_NOTIMPL;
		}

		int Interop.IOleClientSite.ShowObject()
		{
			return Interop.HRESULT.S_OK;
		}

		#endregion

		#region IOleCommandTarget Members

		void Interop.IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, int cCmds, Interop.OLECMD prgCmds, IntPtr pCmdText)
		{
			//return Interop.HRESULT.S_OK;
		}

		int Interop.IOleCommandTarget.Exec(IntPtr pguidCmdGroup, WebBrowserCommand nCmdID, WebBrowserCommandExecutionOptions nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
		{
			if (pguidCmdGroup != IntPtr.Zero)
			{
				Guid guidCmdGroup = (Guid)Marshal.PtrToStructure(pguidCmdGroup, typeof(Guid));
				if (guidCmdGroup == Interop.DocHostCommandHandlerClsid)
				{
					switch (nCmdID)
					{
						case WebBrowserCommand.ShowScriptError:
							ScriptErrorEventArgs scriptErrorEventArgs = new ScriptErrorEventArgs(
								(pvaIn != IntPtr.Zero) ? Marshal.GetObjectForNativeVariant(pvaIn) as mshtml.IHTMLDocument2 : null
								);

							this.webBrowserControl.OnScriptError(scriptErrorEventArgs);

							if (!scriptErrorEventArgs.Handled)
							{
								return Interop.HRESULT.S_FALSE;
							}

							if (pvaOut != IntPtr.Zero)
							{
								Marshal.GetNativeVariantForObject(scriptErrorEventArgs.ContinueScripts, pvaOut);
							}

							return Interop.HRESULT.S_OK;
					}
					return Interop.OLECMDERR.OLECMDERR_E_NOTSUPPORTED;
				}
			}
			return Interop.OLECMDERR.OLECMDERR_E_UNKNOWNGROUP;
		}

		#endregion

		#region Interop.IDocHostUIHandler Members

		int Interop.IDocHostUIHandler.ShowContextMenu(int dwID, ref tagPOINT ppt, object pcmdtReserved, object pdispReserved)
		{
			ShowContextMenuEventArgs showContextMenuEventArgs = new ShowContextMenuEventArgs((ContextMenuType)(1 << dwID), new Point(ppt.x, ppt.y), pcmdtReserved, pdispReserved);

			this.webBrowserControl.OnShowContextMenu(showContextMenuEventArgs);

			return (showContextMenuEventArgs.Handled) ? Interop.HRESULT.S_OK : Interop.HRESULT.S_FALSE;
		}

		void Interop.IDocHostUIHandler.GetHostInfo(Interop.DOCHOSTUIINFO pInfo)
		{
			pInfo.dwFlags = this.webBrowserControl.webBrowserCapabilities;
			pInfo.dwDoubleClick = 0;
		}

		void Interop.IDocHostUIHandler.ShowUI(uint dwID, ref object pActiveObject, ref object pCommandTarget, ref object pFrame, ref object pDoc)
		{
		}

		void Interop.IDocHostUIHandler.HideUI()
		{
		}

		void Interop.IDocHostUIHandler.UpdateUI()
		{
		}

		void Interop.IDocHostUIHandler.EnableModeless(int fEnable)
		{
		}

		void Interop.IDocHostUIHandler.OnDocWindowActivate(int fActivate)
		{
		}

		void Interop.IDocHostUIHandler.OnFrameWindowActivate(int fActivate)
		{
		}

		void Interop.IDocHostUIHandler.ResizeBorder(ref tagRECT prcBorder, int pUIWindow, int fRameWindow)
		{
		}

		void Interop.IDocHostUIHandler.GetOptionKeyPath(ref string pchKey, uint dw)
		{
		}

		object Interop.IDocHostUIHandler.GetDropTarget(ref object pDropTarget)
		{
			return null;
		}

		void Interop.IDocHostUIHandler.GetExternal(out object ppDispatch)
		{
			ppDispatch = this.webBrowserControl.ObjectForScripting;
		}

		int Interop.IDocHostUIHandler.TranslateAccelerator(ref Interop.tagMSG lpMsg, ref Guid pguidCmdGroup, uint nCmdID)
		{
			Message msg = Message.Create(lpMsg.hwnd, lpMsg.message, lpMsg.wParam, lpMsg.lParam);
			bool handled = this.webBrowserControl.ProcessKeyMessage(ref msg);
			lpMsg.hwnd = msg.HWnd;
			lpMsg.message = msg.Msg;
			lpMsg.wParam = msg.WParam;
			lpMsg.lParam = msg.LParam;
			return (handled) ? Interop.HRESULT.S_OK : Interop.HRESULT.S_FALSE;
		}

		int Interop.IDocHostUIHandler.TranslateUrl(uint dwTranslate, string pchURLIn, ref string ppchURLOut)
		{
			TranslateUrlEventArgs translateUrlEventArgs = new TranslateUrlEventArgs(pchURLIn);
			this.webBrowserControl.OnTranslateUrl(translateUrlEventArgs);

			ppchURLOut = translateUrlEventArgs.TranslatedUrl;
			return (ppchURLOut == null) ? Interop.HRESULT.S_FALSE : Interop.HRESULT.S_OK;
		}

		IDataObject Interop.IDocHostUIHandler.FilterDataObject(IDataObject pDO)
		{
			return null;
		}
 
		#endregion

		#region Interop.IDocHostShowUI Members

		uint Interop.IDocHostShowUI.ShowMessage(IntPtr hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, out int lpResult)
		{
			ShowMessageEventArgs showMessageEventArgs = new ShowMessageEventArgs(
				NativeWindow.FromHandle(hwnd),
				lpstrText,
				lpstrCaption,
				(MessageBoxButtons)(dwType & 0xF),
				(MessageBoxIcon)(dwType & 0xF0),
				lpstrHelpFile,
				dwHelpContext);

			this.webBrowserControl.OnShowMessage(showMessageEventArgs);

			if (showMessageEventArgs.Handled)
			{
				lpResult = (int)(showMessageEventArgs.Result);
				return Interop.HRESULT.S_OK;
			}
			else
			{
				lpResult = 0;
				return Interop.HRESULT.S_FALSE;
			}
		}

		uint Interop.IDocHostShowUI.ShowHelp(IntPtr hwnd, string pszHelpFile, uint uCommand, uint dwData, tagPOINT ptMouse, object pDispatchObjectHit)
		{
			ShowHelpEventArgs showHelpEventArgs = new ShowHelpEventArgs(
				NativeWindow.FromHandle(hwnd),
				pszHelpFile,
				(HelpType)uCommand,
				dwData,
				new Point(ptMouse.x, ptMouse.y),
				pDispatchObjectHit);

			this.webBrowserControl.OnShowHelp(showHelpEventArgs);

			if (showHelpEventArgs.Handled)
			{
				return Interop.HRESULT.S_OK;
			}
			else
			{
				return Interop.HRESULT.S_FALSE;
			}
		}

		#endregion

		#region Interop.IServiceProvider Members

		int Interop.IServiceProvider.QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
		{
			ppvObject = IntPtr.Zero;
			if (guidService == typeof(Interop.IInternetSecurityManager).GUID)
			{
				ppvObject = System.Runtime.InteropServices.Marshal.GetComInterfaceForObject(this.internetSecurityManagerService, typeof(Interop.IInternetSecurityManager));
				return Interop.HRESULT.S_OK;
			}
			return Interop.HRESULT.E_NOINTERFACE;
		}

		#endregion

		/// <remarks/>
		[DispId(Interop.DISPID_AMBIENT_USERAGENT)]
		public string UserAgent
		{
			get
			{
				return this.webBrowserControl.UserAgent;
			}
		}

		/// <remarks/>
		[DispId(Interop.DISPID_AMBIENT_DLCONTROL)]
		public DownloadControlOptions DownloadControl
		{
			get
			{
				return this.webBrowserControl.DownloadControl;
			}
		}

	}
}

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