Visual Basic 2008, WebBrowser, Zoom, and ExecWB

Last Modified: Thu, 12 Jun 2008 17:13:00 +0000 ; Created: Thu, 12 Jun 2008 17:13:00 +0000

So I discovered today how to use a WebBrowser object on a Form in Visual Basic 2008 that would zoom the page just like you can in IE7.

First Method

The first method I tried used the .NET Framework Components, System.Windows.Forms, WebBrowser control object. This is a simplified interface for showing an IE web browser on your form.

After the document has loaded (see WebBrowser DocumentComplete method) you can call Me.WebBrowser1.Document.Body.Style = "zoom 10%" to change the zoom. However you will see a brief flicker as the document will already be displayed and visible at the normal zoom before going to your new zoom.

One option would be to hide the web browser control until after the document is loaded, and you've changed the zoom. However that might result in unwanted screen flicker as well.

The disadvantage of this method is that you must reset the zoom each time you navigate to a different URL with the control.


Second Method (Best)

I searched on the Internet and also saw references to a ExecWB method. Unfortantely this method doesn't exist for the .Net Framework Component "WebBrowser." I did find another component which I was able to use that had this method.

COM Components, Microsoft Web Browser, ieframe.dll

Adding this to my toolbox and then to my form gave me a AxWebBrowser1 object. Maybe not as friendly or simple as the WebBrowser .Net one, but it has the ExecWB method I needed to set the zoom!

I start by having my Form1_Load method call:
Me.AxWebBrowser1.Navigate("about:blank")

This allows the control to load a quick document as required before making the zoom call.

Below is the method I used to set the zoom one time before the control was rendered:


Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
If (Me.AxWebBrowser1.LocationURL().Equals("about:blank")) Then
'Loading page for very first time
' The first time we set the browser zoom level

' The AxWebBrowser requires that at least 1 document has already been loaded before calling ExecWB
' otherwise a COM exception occurs
' So this function is triggered by navigating to about:blank the first time

Dim factor As Object

factor = 125

Try
Me.AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, factor, IntPtr.Zero)
Catch ex As Exception
Me.AxWebBrowser1.Hide()
Me.errorLabel.Text = "This screensaver requires that you have Internet Explorer version 7 or later"
Me.errorLabel.Visible = True
Return
End Try
End If

End Sub