Introduction

csEXWB is a C# .NET 2.0 control that creates, hosts and sinks the events of the original Webbrowser control (Not .NET or any other wrapper). Advanced customization and total control over the Webbrowser control are achieved via implementation of a number of interfaces, along with the addition of many methods, properties, events and a COM library. The control has no dependencies on MSHTML interop. All of the interfaces, enums and structs are defined within the project. Here are some of the features of this control:

Can easily be extended by adding other interfaces and methods, unlike .NET or any other Webbrowser wrapper

Monitor all HTTP and HTTPS request and response headers with the opportunity to add your own headers. This includes images, sounds, scripts, etc.

Built in HTMLDocument and HTMLWindow events

Frameset aware. All the relevant routines check for frames

Full drag and drop support

A comprehensive demo that demonstrates how to use most of the functionality offered by this control beyond the basics. An HTTP and HTTPS request and response header viewer which can be used for web application development. A complete DOM viewer. Multi tab simulation. Cookies and Cache viewer and remover, per site or all, A simple file download viewer. A demo of how to retrieve information from document object. An HTML editor with edit, source, and preview panes. Many editing functionalities, internal and external drag drop, font, color, inserting items such as table, reading and setting properties such as table cell, etc.

Refresh begin and end events

Create and resolve Internet shortcuts using IShellLink interface

Load HTML content specifying BaseURL

Added functionality to use the control as an MSHTML editor

MSHTML as a UI-less HTML parser helper class

Control

This is a basic class derived from Control class. It overrides a number of Control methods in order to create, host and dispose the Webbrowser control. In addition, it handles resize events and forwards keyboard, focus, refresh and visibility events to the hosted Webbrowser control.

csExWBDLMan COM library

csExWBDLMan is written in VC++ 6.0 using ATL 3.0. It is compiled with minimum dependencies, i.e. no MFC, std::, CString. The library is used to allow the client to employ the IDownloadManager implementation of csExWBDLMan library. In addition, the library implements the PassthroughAPP package by Igor Tandetnik, which enables the client to intercept all HTTP and HTTPS request and responses. The last functionality offered by the library is the ability to set any of the available windows hooks with the option to cancel a call. This functionality is used to stop dialogs launched using the showModelessDialog() and showModalDialog() methods. The library can be used in any other project that needs to set a global windows hook or view HTTP and HTTPS headers, as long as a valid window handle is supplied.

Before opening the solution or running the demo, you need to register this library

Copy csExWBDLMan.dll located in the csExWB\COM_Component_Source_Binaries\ReleaseMinDependency sub folder to your system directory

Register csExWBDLMan.dll using regsvr32.exe

Open the solution

Example: Assuming the system dir path is 'C:\windows\system32\'

regsvr32.exe C:\windows\system32\csExWBDLMan.dll

Interfaces

Each interface mentioned below plays a role in either the hosting or customization process. I decided not to go through a detailed explanation of how to implement and use the interfaces. One can easily find all of the information regarding implementation and functionalities offered by these interfaces from MSDN, or right here on this site.

IOleClientSite - required as part of Webbrowser control hosting interfaces

IOleInPlaceSite - required as part of Webbrowser control hosting interfaces

IWindowForBindingUI - returns a handle via ::GetWindow that is used by MSHTML to display information in client UI

INewWindowManager - WinX sp2 and up, to handle popups

IAuthenticate - to handle basic and NTLM authentication

IOleCommandTarget - to intercept script errors via the ::Exec method

IinternetSecurityManager - to handle ::ProcessUrlAction

IHTMLEventCallBack - A callback mechanism to pass HTMLDocument and HTMLWindow events to the control and, in turn, to the client

IProtectFocus - IE7 + Vista - Queries for permission to grab the focus when loading the page or when a script attempts to focus an element

IHTMLOMWindowServices - XP sp2 - Enables applications that are hosting the WebBrowser Control to intercept the window object calls for manipulating the host window coordinates from Dynamic HTML (DHTML) script

Properties

WBDOCDOWNLOADCTLFLAG - get set - DOC_DOWNLOAD_CONTROL_FLAGS

DownloadImages - get set - shortcut for DOCDOWNLOADCTLFLAG.DLIMAGES

DownloadSounds - get set - shortcut for DOCDOWNLOADCTLFLAG.BGSOUNDS

DownloadActiveX - get set - shortcut for DOCDOWNLOADCTLFLAG.NO_DLACTIVEXCTLS

DownloadJava - get set - shortcut for DOCDOWNLOADCTLFLAG.NO_JAVA

DownloadFrames - get set - shortcut for DOCDOWNLOADCTLFLAG.NO_FRAMEDOWNLOAD

DownloadScripts - get set - shortcut for DOCDOWNLOADCTLFLAG.NO_SCRIPTS

WBDOCHOSTUIFLAG - get set - DOC_HOST_UI_FLAGS

Border3DEnabled - get set - Webbrowser 3D border

ScrollBarsEnabled - get set - Webbrowser Scrollbars

WBDOCHOSTUIDBLCLK - get set - DOC_HOST_UI_DBLCLK

TextSize - get set - Webbrowser zoom property

CanGoBack - get - can navigate backward

CanGoForward - get - can navigate forward

WebbrowserObject - get - Document object of Webbrowser control

SendSourceOnDocumentCompleteWBEx - get set - fires DocumentCompleteEX event rather than DocumentComplete. DocumentCompleteEX event has one additional parameter that contains the source of the pDisp document object at the time DocmentComplete was called

IEServerHwnd - get - Internet Explorer_Server HWND

ShellEmbedingHwnd - get - ShellEmbedding HWND

ShellDocObjectHwnd - get - ShellDocObject HWND

RegisterAsDropTarget - get set - Webbrowser default drag drop

RegisterForInternalDragDrop - get set - Instructs the control to take over drag drop. Uses WBDragxxx and WBDropxxx events to notify the client

RegisterAsBrowser - get set - Registers Webbrowser as a top-level browser for target name resolution

Silent - get set - whether the Webbrowser control can show dialog boxes

LocationName - get - name of the resource that Webbrowser control is currently displaying

LocationUrl - get set - URL of the resource that Webbrowser control is currently displaying

Busy - get - indicating whether the Webbrowser control is engaged in a navigation or downloading operation

OffLine - get set - currently operating in offline mode

ReadyState - get - retrieves Readystate of the Webbrowser control

ThumbImage - get - Contains a thumb image of the Webbrowser or null

ObjectForScripting property. Allows JavaScript functions in an HTML page to call methods and properties of a an instance class passed to this property. Same as ObjectForScripting of C# Webbrowser wrapper control. An example of how to use this property has been provided in WinExternal class of frmMain

DocumentTitle - Sets or retrieves the title of the document

DocumentSource - Sets or retrieves the HTML source of the document

UseInternalDownloadManager - Set to true, default, to allow the control to take over file downloads. FileDownloadExxxx events are fired instead of FileDownload event. This functionality is achieved via COM library which implements IDownloadManager interface. The download routines account for redirect and Content_Disposition header

FileDownloadDirectory Default file download directory. Set to users MyDocuments folder by default. Used only if UseInternalDownloadManager property is set to true

public bool LoadHtmlIntoBrowser(string html) - Loads HTML content into browser using IPersistStreamInit interface, baseurl, is set to about:blank by MSHTML

public void ShowCertificateDialog() - If available, displays certificate for current website

public Image DrawThumb(int W, int H, System.Drawing.Imaging.PixelFormat pixFormat) - Uses IE_Server HWND to draw a thumb image of the Webbrowser control. Faster than other methods with one draw back. It only works if the Webbrowser control is in front of Zorder

public int FindAndHightAllInPage(string sFind, bool MatchWholeWord, bool MatchCase, int cbackColor, int cForeColor) - Finds and highlights all matches. Returns number of matches found. In a frameset, it attempts to find and use the active document.

public int DownloadFile(string Url) Attempts to download a file asynch. FileDownloadExXXX events are used for notifications. Return value is a unique ID for this download that can be used to stop the download

public void StopFileDownload(int dlUID) Stops a file download that was started by calling the DownloadFile method

FileDownloadExStart - If UseInternalDownloadManagertrue, notifies client of a request to start a file download. Stop download at any point using a unique ID, save file in the background. Overrides Webbrowser default file download mechanism

FileDownloadExEnd - If UseInternalDownloadManagertrue, notifies client of end of a file download

FileDownloadExProgress - If UseInternalDownloadManagertrue, notifies client of status of a file download

FileDownloadExAuthenticate - If UseInternalDownloadManagertrue, notifies client of a request from server for authentication

FileDownloadExError - If UseInternalDownloadManagertrue, notifies client of an error during a file download

ProtocolHandlerOnBeginTransaction Enables the client to view all the HTTP and HTTPS request headers of Webbrowser control

ProtocolHandlerOnResponse Enables the client to view all HTTP and HTTPS the response headers of Webbrowser control

Usage of a static class to share forms and controls within the application

Find and highlight in page

Authentication

Clear cache

Loading images from an embedded image strip

Search engine query

Final note

Although reasonable care has been taken to ensure the correctness of this implementation, expect bugs. This code should never be used in any application without proper verification and testing. When reporting bugs, sending suggestions or requests, or asking a question, please include information regarding IE and OS versions and service packs. Include as much detail as possible. This will be very helpful, not only for me to find and fix bugs or implement requested features, but for others who may be in a similar position.

License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.

05 June 2007 - version (1.0.0.3)

Added an overload for InvokeScript which accepts an IWebBrowser2 interface

Fixed return value of IDocHostShowUI.ShowMessage method

25 May 2007 - version (1.0.0.2)

I would like to thank everyone for sending their suggestions, bug reports, and feature requests.

Properties

ObjectForScripting property. Allows JavaScript functions in an HTML page to call methods and properties of a an instance class passed to this property. Same as ObjectForScripting of C# Webbrowser wrapper control. An example of how to use this property has been provided in WinExternal class of frmMain

UseInternalDownloadManager - Set to true, default, to allow the control to take over file downloads. FileDownloadExxxx events are fired instead of FileDownload event

FileDownloadDirectory Default file download directory. Set to users MyDocuments folder by default. Used only if UseInternalDownloadManager property is set to true

DocumentTitle

DocumentSource

Border3DEnabled - Webbrowser 3D border

ScrollBarsEnabled - Webbrowser Scrollbars

DownloadImages - shortcut for DOCDOWNLOADCTLFLAG.DLIMAGES

DownloadSounds - shortcut for DOCDOWNLOADCTLFLAG.BGSOUNDS

DownloadActiveX - shortcut for DOCDOWNLOADCTLFLAG.NO_DLACTIVEXCTLS

DownloadJava - shortcut for DOCDOWNLOADCTLFLAG.NO_JAVA

DownloadFrames - shortcut for DOCDOWNLOADCTLFLAG.NO_FRAMEDOWNLOAD

DownloadScripts - shortcut for DOCDOWNLOADCTLFLAG.NO_SCRIPTS

Events

FileDownloadExStart - If UseInternalDownloadManagertrue, notifies client of a request to start a file download. Stop download at any point using a unique id, save file in the background, ... Overrides webbrowser default file download mechanism

FileDownloadExEnd - If UseInternalDownloadManagertrue, notifies client of end of a file download

FileDownloadExProgress - If UseInternalDownloadManagertrue, notifies client of status of a file download

FileDownloadExAuthenticate - If UseInternalDownloadManagertrue, notifies client of a request from server for authentication

FileDownloadExError - If UseInternalDownloadManagertrue, notifies client of an error during a file download

ProtocolHandlerOnBeginTransaction - Enables the client to view all the HTTP and HTTPS request headers of webbrowser control

ProtocolHandlerOnResponse Enables the client to view all HTTP and HTTPS the response headers of webbrowser control

AllowFocusChange To notify client when focus is being changed

HTMLOMWindowServices_moveTo - IHTMLOMWindowServices implementation - Moves the screen position of the upper-left corner of the application window to the specified coordinates

HTMLOMWindowServices_moveBy - IHTMLOMWindowServices implementation - Moves the screen position of the application window by the specified offset values

HTMLOMWindowServices_resizeTo - IHTMLOMWindowServices implementation - Changes the current size of the application window by the specified offset values.

HTMLOMWindowServices_resizeBy - IHTMLOMWindowServices implementation - Sets the size of the application window to the specified values

Interfaces

IProtectFocus interface. IE7 and Vista only. Queries for permission to grab the focus when loading the page or when a script attempts to focus an element

Share

About the Author

Comments and Discussions

Odd that nobody else is reporting this. This always worries me when I see that I'm the only one with a problem. It means my problem will be either VERY SIMPLE to solve. Or IMPOSSIBLE. Let's hope for the former...

Anybody else get NotSupportedExceptions thrown sporadically? I had an app working fine. Now, in OnDocumentComplete() I try to get DocumentTitle ( or GetTitle(true) ) and it throws a NotSupportedException. Here is where the exception is thrown:

We're now seeing a new issue that occurs on newer extremely high resolution screen laptops - this web browser control is rendering the pages in a way where they appear small and disproportional. Internet Explorer itself renders the pages just fine but similarly to the last question we posted this custom web control does not seem to be working as Internet Explorer does and manifests these rendering issues. Is there an update or a newer version of this wrapper code available that can help alleviate these issues?

We've been using this code for some time, and we've been experiencing general issues when IE11 is installed on a user's system, where certain websites either don't render correctly or don't function correctly. These same issues are typically not present in IE11 itself, and are only present in this custom web browser on a system that has IE11 installed. Is there a way to allow this custom control to behave like IE11 itself and have the redundancy that IE11 has for properly rendering web pages?