Webbrowser Zoom In and out - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This code is used to zoom a webbrowser page in and out in Visual Basic .NET

For this tutorial, you need a webbrowser, and two buttons. One button will handle zooming in and one button will handle zoom out

Add the following declarations

Dim InitialZoom As Integer = 100
    Public Enum Exec
        OLECMDID_OPTICAL_ZOOM = 63
    End Enum
    Private Enum execOpt
        OLECMDEXECOPT_DODEFAULT = 0
        OLECMDEXECOPT_PROMPTUSER = 1
        OLECMDEXECOPT_DONTPROMPTUSER = 2
        OLECMDEXECOPT_SHOWHELP = 3
    End Enum

The add the form load event. The form load event will navigate the page to any given website address:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://www.google.com")
    End Sub

Then add the following sub that will handle the zooming.

Public Sub PerformZoom(ByVal Value As Integer)
        Try
            Dim Res As Object = Nothing
            Dim MyWeb As Object
            MyWeb = Me.WebBrowser1.ActiveXInstance
            MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, CObj(Value), CObj(IntPtr.Zero))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Zoom In Button Code

Private Sub ZoomIn_Click(sender As System.Object, e As System.EventArgs) Handles ZoomIn.Click
        InitialZoom += 10
        PerformZoom(InitialZoom)
    End Sub

Zoom Out Button Code

Private Sub ZoomOut_Click(sender As System.Object, e As System.EventArgs) Handles ZoomOut.Click
        InitialZoom -= 10
        PerformZoom(InitialZoom)
    End Sub

Try the program and enjoy


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.