Click here to Skip to main content
16,016,501 members
Articles / Programming Languages / Visual Basic
Article

Working with the Web Browser Control in Visual Studio 2005 - IE7Clone.

Rate me:
Please Sign up or sign in to vote.
4.89/5 (65 votes)
14 Jan 20076 min read 602.7K   12.1K   150   138
Example application working with the VS2005 Web browser control

Ie7 Clone

Introduction

As many people know Microsoft has included a browser control with Visual Studio 2005.

The browser control is a managed wrapper around the internet explorer control found in earlier versions of visual studio (SHDocVW.DLL) and it exposes some functionality that was not always easy to get at, while at the same time leaving out some functionality that would have made the control truly great to work with.

The goal of this article and accompanying sample application is to show working with the new WebBrowser Control in Visual Studio 2005 by cloneing many of the features in Internet Explorer 7. It is not meant to replace your existing browser or be a 100% complete implementation of a browser, The features implemented are to show examples of working with various properties and events of the control and some idea's on what to do to write your own complete browser application. Every feature is not fully error checked as this is meant to be a "starting point" for working with the control and every user interaction possibility has not been addressed. That said with minimal effort you could make this example a complete browser based application. You will find some tips in the comments for certain events and routines on how to improve or use that routine in your own applications as well as some "stubs" such as download manager, phishing filter, rss reader, Document Explorer, Image Grabber etc.

This example was put together with out referencing the Active X (SHDocVW.DLL) directly. That said, It again needs to be pointed out that the browser control is a managed wrapper around the internet explorer control found in earlier versions of visual studio (SHDocVW.DLL) and you may find a mix of the 2 will best suit your purposes.

Getting Started...

To make our example application one of the 1st things we are going to need is closable tabs, not wanting to re-invent the wheel a suitable tab control is found. The tabs used in this example were written by Eduardo Oliveira and can be found here. (Updated Tab Control Code provided by the author of the control. Thanks Eduardo).

Extending the browser control...

The browser control falls short on some basic functionality we will want to use so we will need to extend the control, an example of this is the NewWindow event..

New Window event standard with the control.

Image 2

Now take a look at what we have with the extended browser.

Image 3

Please look at the extended browser control for how this was done, several MSDN articles were used in putting it together and you could certainly add a lot more.

Context Menus

One of the nicest features of the new control is the ability to override the built in context menu and create your own,not easily done in the past. In this example we will use our own context menu, which will show you a bit about working with the loaded document directly.

To disable the built in menu set the web browser property IsWebBrowserContextMenuEnabled to false, then create your own context menu and set the browsers context menu to your new custom menu.

Image 4

This is a pretty powerful tool especially when used with the controls document property as demonstrated in the example application.

* In the example application I do not check parent and children of the selected element, this would not be hard to implement if you choose to and would allow more accurate or robust options in the context menu.

JavaScript Document.External...

One of the most powerful features you can use, especially when creating intranet browser based applications is the javascript window.external call in conjuction with your browser based application. You can build extremely powerful browser based applications using this functionality.

Lets take a look at the search provider code:

Our document might contain a link or onclick such as: window.external.AddSearchProvider

We can react to this call to window.external in whatever way we see fit, in this case we launch a form.

VB.NET
Public Sub AddSearchProvider(ByVal value As String)
   ' See: http://www.opensearch.org/ for more information.
   Dim ofrm As New frmAddSearchProvider
   ofrm.strXML = value
   ofrm.Show()
End Sub

Image 5

As you can see we are reacting to a call from the document.

window.external.AddSearchProvider / Public Sub AddSearchProvider(ByVal value As String)

*Note - Make sure your routine signatures match as shown above in red.

Using this feature you can extend and add whatever type of feature you would want to provide...

Example:

HTML
<a href="#" onClick="window.external.ShowCalc">ShowCalculator</a>

Public Sub ShowCalc()
    Process.Start("Calc.exe")
End sub

This is also demostrated on the blocked site page to launch the settings form.

As you can see using this feature you can do just about anything you want to with the full power of a windows application.

Phishing Filters, Blocked Sites, Iterating the document etc...

The browser control exposes the loaded document making it easy to implement features such as a site blockers, phishing filters and simular types of features with a simple iteration through the documents elements allow you to check the document and perform actions based on it's contents.

Image 6

VB.NET
Dim oEl As HtmlElement

For Each oEl In wb.Document.All
   'Do something based on the element.
Next

It should also be noted that the HtmlElement object and the document itself has a rich set of functions and features to work with, you will see some of these in the example application such as: GetAttribute, GetElementFromPoint etc...

Features in this Example

Most of the learning value is in the example application itself, there are many articles on MSDN and on the web dealing with working with the ShDocVW.DLL and MSHTML. I tried to provide as many starting points and examples of how to implement many features to show working with different the control in many ways, some of the features implemented either fully or partially are:

  • Basic Browser functionality.
  • Document explorer.
  • Most of Internet Explorers Dialogs (properties, find, print dialogs, favorites, select all etc).
  • Grabing images from a page.
  • Blocking sites.
  • Phishing Filter example.
  • Working with VS2005 My.Settings.
  • RSS Viewer + Detecting RSS.
  • History / Cache Viewer.
  • Cookie Viewer.
  • Search Providers.
  • Working with popups, implementing an info bar.
  • Creating a ruler user control.
  • Example of how you might deal with errors
  • Alot more, just poke around in the code.

Background / Notes

The basic idea behind this example is to show a good overview of working with the new Web Browser control by cloning a lot of the features in Internet Explorer 7 as I have found one of the best ways to learn is to "clone" an existing application as well as to point out some idea's on where to go with your own browser based applications and some of the possibilities.

Many articles (including some on MSDN) recommend using on error resume next type error handling in your code due to the nature of connected applications that use browser controls. I have implemented some error handling in the example, but leave the rest up to you to determine how best to handle the many possibilities. All of the features implemented in the example application work for the sites I browse most often.

VS2005 provides application events such as when network connection state has changed, this could be handy for you depending on how your own browser based applications are intended to work.

Using the code

The code is pretty straight forward and is commented where I thought would be needed.

History

This is the 1st release of this example.

License

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


Written By
Web Developer
United States United States
I have played various roles in IT since around 1993 when I began writing commercial web pages. I then moved to mainly coding in Visual Basic around 1995, making point of sale and kiosk systems, more recently moving to e-commerce and crm applications. Since the release of .net I have been mainly working in .net focusing in vb.net although I have done several projects in c#. I have worked for various companies and have held the position of lead developer at several, recently (2005) leading the e-commerce team at a national retailer to a successful e-commerce implementation. I currently hold comptia a+, network plus, mcp, mcsa, mcse (server system 2003) and mcsd certifications. I enjoy sharing what I have learned as well as embracing new technologies as they are released.

Comments and Discussions

 
GeneralRe: Unable to use arrow keys or ESC key in Flash Pin
Vokainer17-Oct-08 7:47
Vokainer17-Oct-08 7:47 
GeneralRe: Unable to use arrow keys or ESC key in Flash Pin
pokeybit6-Feb-09 0:56
pokeybit6-Feb-09 0:56 
QuestionAm I ok to use this as a base for a university project? Pin
murderstick13-Jul-08 8:45
murderstick13-Jul-08 8:45 
QuestionAnyway to get the generated source from this? i.e Ajax sent source, not loaded source? Pin
bhermer5-Jul-08 9:33
bhermer5-Jul-08 9:33 
Generalit doesnt open multiple home pages Pin
bader130-May-08 12:06
bader130-May-08 12:06 
GeneralAdobe Flash Player Freezes in IE7Clone Pin
snoop123412-May-08 22:14
snoop123412-May-08 22:14 
GeneralRe: Adobe Flash Player Freezes in IE7Clone Pin
Vokainer17-May-08 9:15
Vokainer17-May-08 9:15 
GeneralFlash fix! Pin
Vokainer31-May-08 21:43
Vokainer31-May-08 21:43 
There is a solution.

Private Enum WindowsMessages
WM_ACTIVATE = 6
WM_ACTIVATEAPP = 28
WM_AFXFIRST = 864
WM_AFXLAST = 895
WM_APP = 32768
WM_ASKCBFORMATNAME = 780
WM_CANCELJOURNAL = 75
WM_CANCELMODE = 31
WM_CAPTURECHANGED = 533
WM_CHANGECBCHAIN = 781
WM_CHAR = 258
WM_CHARTOITEM = 47
WM_CHILDACTIVATE = 34
WM_CLEAR = 771
WM_CLOSE = 16
WM_COMMAND = 273
WM_COMPACTING = 65
WM_COMPAREITEM = 57
WM_CONTEXTMENU = 123
WM_COPY = 769
WM_COPYDATA = 74
WM_CREATE = 1
WM_CTLCOLORBTN = 309
WM_CTLCOLORDLG = 310
WM_CTLCOLOREDIT = 307
WM_CTLCOLORLISTBOX = 308
WM_CTLCOLORMSGBOX = 306
WM_CTLCOLORSCROLLBAR = 311
WM_CTLCOLORSTATIC = 312
WM_CUT = 768
WM_DEADCHAR = 259
WM_DELETEITEM = 45
WM_DESTROY = 2
WM_DESTROYCLIPBOARD = 775
WM_DEVICECHANGE = 537
WM_DEVMODECHANGE = 27
WM_DISPLAYCHANGE = 126
WM_DRAWCLIPBOARD = 776
WM_DRAWITEM = 43
WM_DROPFILES = 563
WM_ENABLE = 10
WM_ENDSESSION = 22
WM_ENTERIDLE = 289
WM_ENTERMENULOOP = 529
WM_ENTERSIZEMOVE = 561
WM_ERASEBKGND = 20
WM_EXITMENULOOP = 530
WM_EXITSIZEMOVE = 562
WM_FONTCHANGE = 29
WM_GETDLGCODE = 135
WM_GETFONT = 49
WM_GETHOTKEY = 51
WM_GETICON = 127
WM_GETMINMAXINFO = 36
WM_GETOBJECT = 61
WM_GETTEXT = 13
WM_GETTEXTLENGTH = 14
WM_HANDHELDFIRST = 856
WM_HANDHELDLAST = 863
WM_HELP = 83
WM_HOTKEY = 786
WM_HSCROLL = 276
WM_HSCROLLCLIPBOARD = 782
WM_ICONERASEBKGND = 39
WM_IME_CHAR = 646
WM_IME_COMPOSITION = 271
WM_IME_COMPOSITIONFULL = 644
WM_IME_CONTROL = 643
WM_IME_ENDCOMPOSITION = 270
WM_IME_KEYDOWN = 656
WM_IME_KEYLAST = 271
WM_IME_KEYUP = 657
WM_IME_NOTIFY = 642
WM_IME_REQUEST = 648
WM_IME_SELECT = 645
WM_IME_SETCONTEXT = 641
WM_IME_STARTCOMPOSITION = 269
WM_INITDIALOG = 272
WM_INITMENU = 278
WM_INITMENUPOPUP = 279
WM_INPUTLANGCHANGE = 81
WM_INPUTLANGCHANGEREQUEST = 80
WM_KEYDOWN = 256
WM_KEYFIRST = 256
WM_KEYLAST = 264
WM_KEYUP = 257
WM_KILLFOCUS = 8
WM_LBUTTONDBLCLK = 515
WM_LBUTTONDOWN = 513
WM_LBUTTONUP = 514
WM_MBUTTONDBLCLK = 521
WM_MBUTTONDOWN = 519
WM_MBUTTONUP = 520
WM_MDIACTIVATE = 546
WM_MDICASCADE = 551
WM_MDICREATE = 544
WM_MDIDESTROY = 545
WM_MDIGETACTIVE = 553
WM_MDIICONARRANGE = 552
WM_MDIMAXIMIZE = 549
WM_MDINEXT = 548
WM_MDIREFRESHMENU = 564
WM_MDIRESTORE = 547
WM_MDISETMENU = 560
WM_MDITILE = 550
WM_MEASUREITEM = 44
WM_MENUCHAR = 288
WM_MENUCOMMAND = 294
WM_MENUDRAG = 291
WM_MENUGETOBJECT = 292
WM_MENURBUTTONUP = 290
WM_MENUSELECT = 287
WM_MOUSEACTIVATE = 33
WM_MOUSEFIRST = 512
WM_MOUSEHOVER = 673
WM_MOUSELAST = 522
WM_MOUSELEAVE = 675
WM_MOUSEMOVE = 512
WM_MOUSEWHEEL = 522
WM_MOVE = 3
WM_MOVING = 534
WM_NCACTIVATE = 134
WM_NCCALCSIZE = 131
WM_NCCREATE = 129
WM_NCDESTROY = 130
WM_NCHITTEST = 132
WM_NCLBUTTONDBLCLK = 163
WM_NCLBUTTONDOWN = 161
WM_NCLBUTTONUP = 162
WM_NCMBUTTONDBLCLK = 169
WM_NCMBUTTONDOWN = 167
WM_NCMBUTTONUP = 168
WM_NCMOUSEHOVER = 672
WM_NCMOUSELEAVE = 674
WM_NCMOUSEMOVE = 160
WM_NCPAINT = 133
WM_NCRBUTTONDBLCLK = 166
WM_NCRBUTTONDOWN = 164
WM_NCRBUTTONUP = 165
WM_NEXTDLGCTL = 40
WM_NEXTMENU = 531
WM_NOTIFY = 78
WM_NOTIFYFORMAT = 85
WM_NULL = 0
WM_PAINT = 15
WM_PAINTCLIPBOARD = 777
WM_PAINTICON = 38
WM_PALETTECHANGED = 785
WM_PALETTEISCHANGING = 784
WM_PARENTNOTIFY = 528
WM_PASTE = 770
WM_PENWINFIRST = 896
WM_PENWINLAST = 911
WM_POWER = 72
WM_PRINT = 791
WM_PRINTCLIENT = 792
WM_QUERYDRAGICON = 55
WM_QUERYENDSESSION = 17
WM_QUERYNEWPALETTE = 783
WM_QUERYOPEN = 19
WM_QUEUESYNC = 35
WM_QUIT = 18
WM_RBUTTONDBLCLK = 518
WM_RBUTTONDOWN = 516
WM_RBUTTONUP = 517
WM_RENDERALLFORMATS = 774
WM_RENDERFORMAT = 773
WM_SETCURSOR = 32
WM_SETFOCUS = 7
WM_SETFONT = 48
WM_SETHOTKEY = 50
WM_SETICON = 128
WM_SETREDRAW = 11
WM_SETTEXT = 12
WM_SETTINGCHANGE = 26
WM_SHOWWINDOW = 24
WM_SIZE = 5
WM_SIZECLIPBOARD = 779
WM_SIZING = 532
WM_SPOOLERSTATUS = 42
WM_STYLECHANGED = 125
WM_STYLECHANGING = 124
WM_SYNCPAINT = 136
WM_SYSCHAR = 262
WM_SYSCOLORCHANGE = 21
WM_SYSCOMMAND = 274
WM_SYSDEADCHAR = 263
WM_SYSKEYDOWN = 260
WM_SYSKEYUP = 261
WM_TCARD = 82
WM_TIMECHANGE = 30
WM_TIMER = 275
WM_UNDO = 772
WM_UNINITMENUPOPUP = 293
WM_USER = 1024
WM_USERCHANGED = 84
WM_VKEYTOITEM = 46
WM_VSCROLL = 277
WM_VSCROLLCLIPBOARD = 778
WM_WINDOWPOSCHANGED = 71
WM_WINDOWPOSCHANGING = 70
WM_WININICHANGE = 26
End Enum
Public Event Quit As EventHandler
Protected Overrides Sub WndProc(ByRef m As Message)

Dim flag1 As Boolean = m.Msg <> 528
If Not flag1 Then
Dim intPtr1 As IntPtr = m.WParam
Dim wp As Integer = intPtr1.ToInt32()
Dim X As Integer = wp And 65535
flag1 = X <> 2
If Not flag1 Then
OnQuit()
End If
End If

'Flashproblem
Select Case m.Msg
Case &H201, &H204, &H207, &H21 ' WM_LMOUSEBUTTON
MyBase.DefWndProc(m)
Return
End Select


MyBase.WndProc(m)
End Sub

This Code is not very nice but it fixes the flash Problem. The rest of the code is for something different, but I do not know for what exactly.
GeneralSSL Secure Sites are not opening Pin
Member 37309806-May-08 13:39
Member 37309806-May-08 13:39 
Questionadd Internet Explorer 7 tab in my.... Pin
marwik2-May-08 22:33
marwik2-May-08 22:33 
GeneralIE warning for popup with active-x control [modified] Pin
Member 6185232-Apr-08 10:05
Member 6185232-Apr-08 10:05 
GeneralPuzzled and looking for help Pin
lardpvo21-Mar-08 8:16
lardpvo21-Mar-08 8:16 
GeneralRe: Puzzled and looking for help Pin
Overon25-Mar-08 9:13
Overon25-Mar-08 9:13 
GeneralRe: Puzzled and looking for help Pin
KevB1-Apr-08 5:34
KevB1-Apr-08 5:34 
GeneralRe: Puzzled and looking for help Pin
lardpvo18-Apr-08 4:29
lardpvo18-Apr-08 4:29 
GeneralRe: Puzzled and looking for help Pin
KevB5-May-08 16:41
KevB5-May-08 16:41 
GeneralRe: Puzzled and looking for help Pin
rrupsing4-Jun-08 18:37
rrupsing4-Jun-08 18:37 
Generalfind and search text [modified] Pin
shm4-Mar-08 8:14
shm4-Mar-08 8:14 
GeneralRe: find and search text Pin
shm7-Mar-08 5:27
shm7-Mar-08 5:27 
GeneralRe: find and search text Pin
Vokainer13-Mar-08 21:17
Vokainer13-Mar-08 21:17 
GeneralBlocking access to other windows applications (like outlook express) Pin
ric9421-Feb-08 23:14
ric9421-Feb-08 23:14 
GeneralOnNewWindowExtended - pDisp Question Pin
TerriTop12-Jan-08 15:58
TerriTop12-Jan-08 15:58 
GeneralRe: OnNewWindowExtended - pDisp Question Pin
Vokainer20-Jan-08 1:51
Vokainer20-Jan-08 1:51 
GeneralRe: OnNewWindowExtended - pDisp Question Pin
Vokainer5-May-08 7:53
Vokainer5-May-08 7:53 
GeneralLicense Pin
Vokainer9-Jan-08 5:13
Vokainer9-Jan-08 5: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.