Click here to Skip to main content
14,040,817 members
Click here to Skip to main content
Add your own
alternative version


111 bookmarked
Posted 12 Dec 2004

Extended web browser control for .NET 1.0/1.1

, 12 Dec 2004
Rate this:
Please Sign up or sign in to vote.
Provides enhanced functionality for the Windows inbuilt web browser (ActiveX) control.

Sample Image - WebBrowserEx.jpg


Pretty simple: download the project, run it. See 'Adding the referenced ActiveX controls' if it doesn't compile from reference issues.


The next version of the .NET framework features a comprehensive wrapper class for the Microsoft Web Browser control - a COM control that is used by IE and is embedded into Explorer. This new control features everything people are at pains to produce using COM without fishing through newsgroups and tech articles.

Well, I thought it was about time there was an equivalent control for .NET 1.0 / .NET 1.1, so I've put my open-source apron on once more, to produce this control.

This control contains the following features that you do not get (without doing it yourself) with the standard AxWebBrowser control:

  • Context menu
  • Optionally opening all links in a new window
  • Show the find dialog
  • Show the print dialog
  • Show the save-as dialog
  • Show the print preview dialog
  • Disabling accelerator keys such as CTRL+N, CTRL+F and Backspace
  • Restrict images, Java, ActiveX
  • Stop images, sounds, videos from displaying
  • Disable JavaScript
  • Disable downloading/running of ActiveX and Java controls
  • Ability to turn a 3D border on or off
  • Turn the scrollbars on or off
  • Make all form controls XP themed
  • Simplified and more feature-rich navigation methods (Navigate with more options, Refresh with different refresh options.)
  • Documentation for the events.

In some ways, it's actually more feature-rich than the .NET 2.0 control, although the .NET 2.0 control features a security model implementation and a few other properties that will hopefully be put into WebBrowserEx during its lifetime.

How is it implemented?

I'm not going to bother going into detail about how it's been implemented - there is a wealth of information out there on the net that explains this already. See the References section for links to the places I got the information from. The control is a user control with the AxWebBrowser docked to full on it. I tried to derive from AxWebBrowser, but it didn't like me adding properties in, so I gave up on that method. This meant that in the control, lots of properties and events have had to be re-implemented. This was pretty boring to do, but has actually made for a more concise control than having a derived class with all its ActiveX properties drooping out.

IDocHostUIHandler and context menus

The control implements IDocHostUIHandler (see the References for what this is) which handles the context menu and key restrictions. Unlike the .NET 2.0 control, I've left the control with just a ContextMenu property. You can set this to a blank ContextMenu, e.g.:

myBrowser.ContextMenu = new ContextMenu();

if you don't want the default IE context menu. In the version 2 control, there is an additional option to not show the the IE context menu. If there's enough demand for this, I'll add it in.

The legend of the IDispatch_Invoke_Handler

The control also implements IOleClientSite. As before, you can read what this is in the References. The most obscure and important part of the control is the IDispatch_Invoke_Handler(). I spent quite a while trying to work out how to implement IDispatch::Invoke in order to restrict what IE was showing (such as images, ActiveX controls, Java). I found out that if you add a IDispatch_Invoke_Handler() method in your code with the COM dispatch identifier of -5512, this does the job for you. A very obscure answer, but it works well. The options enumeration then works with this to set the options you want available in your browser.

I don't believe the .NET 2.0 control has these options available; hopefully someone at MS who has ties to .NET will read this and request that it's added in, as they're quite core features for the control.


The control features an easier to use ExecWB method which queries the interface before executing it, fixing the "Trying to revoke a drop target that has not been registered." and similar errors. The print, save-as etc. methods, use this wrapped call. You'll find, some of the CMDID constants do nothing. Welcome to the world of COM interop - I found out whilst making this control that some of the constants Microsoft has on the MSDN site haven't worked for years, and they just haven't bothered updating their documentation. Some of these constants may be useable by casting the CurrentDocument to an IOleCommandTarget, and calling its Exec method (see the Find method). This is basically all that the ExecWB method does anyway, as far as I'm aware.

Flags designer

The control implements Thierry Bouquain's designer flag editor for setting the options flags in the designer. It currently has all of its COM imports, interfaces and the flag designer editor in the single file. This makes the source a bit bulkier, but nothing that regions don't overcome. It also makes the control more self-contained in my view.

XP Service Pack 2

In XP Service pack 2, Microsoft has updated the MSHTML part of the browser so that you get more options in your NewWindow event (it's called NewWindowEvent3). I added this originally, but then realized that it would break on most people's machines due to dependency failures. You can add it quite easily to the control, if you want to know how your new window was created.

Adding the referenced ActiveX controls

When you download the project, you might find the following references missing:

  • AxInterop.SHDocVw.dll
  • Interop.SHDocVw.dll
  • Microsoft.mshtml.dll

I haven't included these as they are operating system dependent. I'm running XP SP2, whilst you might be running Windows 2000, XP SP1 etc., so my DLLs will break with your operating system, if I included them.

To add Microsoft.mshtml.dll is straight forward; click on the Add Reference in the project, and add the following:

Add the mshtml reference

Adding SHDocVw is slightly strange. As far as I know, you can't add it as reference, so you have to add it into your toolbox and add it to the control's form, then remove it (something to do with the AxImp tool and VS.NET, I'm not sure what). To do this, open up the WebBrowserEx control so it's in designer mode, and then go to your toolbox, click on Add/Remove items in the context menu, and select the following from the COM tab:

Adding the SHDocVw reference

Once that's added to your toolbox, add a temporary form to the WebBrowserEx project, drop it onto the form, and then remove it. This should add it to your references. Now, do a voodoo dance and compile, and hopefully all should be well.

I haven't tried compiling without VS.NET, adding the references should be straight forward enough though, using the AxImp tool with SHDocVw.dll.

DOM event handling

I've added support into the control for the MSHTML document events. The events allow you to capture keydown, mouseclick etc. events in the document - events which are not available to the browser control itself as it is just a host for the document parser. The implementation I've added is fairly experimental, and can be switched on using EnableHtmlDocumentHandling property. Setting this to true will mean all HtmlDocument* events are fired, however I should stress that these events tend to swallow up your key and mouse events and seem to make the form key presses and link clicking go screwy. I didn't have time to work out why this was, I'll probably have a look at it on a rainy Sunday.

And finally

The method and property names in the control don't match those of the .NET 2.0 control. Some are the same, some aren't. I can't really see a scenario where you'd upgrade from this to the new .NET 2.0 control, but if this is a big problem with people, I can change it so that all the property names and methods are the same.

One small touch I've added to the control is to include the documentation from MSDN for all of the events that come from AxWebBrowser.

That's all. I hope the control comes in useful.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United Kingdom United Kingdom
London based C# programmer.

I maintain my own pet C# site in my spare time.

You may also be interested in...


Comments and Discussions

QuestionDisable an event Pin
Member 112790312-Aug-15 23:07
memberMember 112790312-Aug-15 23:07 
General安装自己写的程序时候弹出未能注册模块 D:\\VAFF\ieframe HRESULT-2147467263 Pin
FFEE9926-May-11 21:10
memberFFEE9926-May-11 21:10 
General为什么设置了我自己写的浏览器(IE核心)为默认浏览器,但是qq空间时弹出的是IE? Pin
FFEE9926-May-11 0:38
memberFFEE9926-May-11 0:38 
QuestionLicensing question Pin
Michellle3-May-10 16:26
memberMichellle3-May-10 16:26 
QuestionHow to disable IE Script error? Pin
Lehoanq13-Sep-09 17:01
memberLehoanq13-Sep-09 17:01 
AnswerRe: How to disable IE Script error? Pin
CptHook28-Jan-11 0:26
memberCptHook28-Jan-11 0:26 
Generalcalling saveas(filename) run over code but not save anything Pin
jacodp2121-Sep-07 4:15
memberjacodp2121-Sep-07 4:15 
GeneralProblem in Using events Pin
Rocky#8-Aug-07 23:10
memberRocky#8-Aug-07 23:10 
NewsRe: Problem in Using events Pin
Rocky#9-Aug-07 0:34
memberRocky#9-Aug-07 0:34 
QuestionError compiling the project Pin
Kjp Inc.19-Jun-07 5:58
memberKjp Inc.19-Jun-07 5:58 
Generalrunning JavaScript in webBrowser Pin
zilibaba6-Mar-07 4:37
memberzilibaba6-Mar-07 4:37 
GeneralJavascript error Pin
Qaiser_Iftikhar5-Mar-07 10:15
memberQaiser_Iftikhar5-Mar-07 10:15 
GeneralRe: Javascript error Pin
MorningZ19-Mar-07 7:34
memberMorningZ19-Mar-07 7:34 
GeneralRe: Javascript error Pin
makinaultral12-Nov-08 4:58
membermakinaultral12-Nov-08 4:58 
GeneralDisabling context menu Pin
nitingautam5-Mar-07 3:12
membernitingautam5-Mar-07 3:12 
GeneralXP Theme not applied on dialog windows using showModalDialog() Pin
Shazam3815-Feb-07 4:18
memberShazam3815-Feb-07 4:18 
Questionhow handling the onclick event on a IHTMLElement object ?: Pin
lulaleila14-Dec-06 6:23
memberlulaleila14-Dec-06 6:23 
AnswerRe: how handling the onclick event on a IHTMLElement object ?: Pin
lulacapixaba7-Nov-07 1:17
memberlulacapixaba7-Nov-07 1:17 
GeneralWebBrowserEx chokes on Javascript in templates Pin
pblecha8-Dec-06 7:14
memberpblecha8-Dec-06 7:14 
QuestionNavigate Problem Pin
taiqi28-Nov-06 11:35
membertaiqi28-Nov-06 11:35 
QuestionHow to Add Onclick method on AXWebBrowser control?? Pin
Mystic_8-Oct-06 22:21
memberMystic_8-Oct-06 22:21 
GeneralManaged to disable ALT key Pin
toATwork28-Sep-06 3:30
membertoATwork28-Sep-06 3:30 
GeneralDo not work RegisterAsBrowser and RegisterAsDropTarget Pin
Dima Filipiuk10-Sep-06 23:41
memberDima Filipiuk10-Sep-06 23:41 
GeneralOS Compatibility Pin
cspinelive17-Aug-06 4:11
membercspinelive17-Aug-06 4:11 
GeneralRe: OS Compatibility Pin
cspinelive17-Aug-06 4:13
membercspinelive17-Aug-06 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190425.1 | Last Updated 12 Dec 2004
Article Copyright 2004 by yetanotherchris
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid