Page 1 of 1

Webbrowser OLECMD Optical Zoom How to zoom in on a webbrowser document Rate Topic: -----

#1 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 23 April 2010 - 04:44 AM

Hi, and welcome to another webbrowser tutorial. In this one I'm going to show you how to zoom in on a webbrowser document. Since there is no direct function to zoom in on the webbrowser control, we're going to call on ActiveX for a little bit of help.


So start Visual Basic (I use 2008 Express) and create a new Windows Forms applications. Add the following objects


-A Webbrowser, named "wbMain"
-A Textbox, named "txtURL"
-Four buttons, named "btnGO", "btnZoom50", "btnZoom100", "btnZoom200"

Your form should now look something like this:
Posted Image
Probably more visually gratifying than mine, but ah well...


So now switch to the code view, and add these declarations. Make sure they are part of any events or subs, but make sure they are included in the class for your form.
 Private Enum ExecOpt
        'A few options for what we want to do, using ActiveX

        'This option will just carry out the default action
        OLECMDEXECOPT_DODEFAULT = 0
        'This option will prompt the user before carrying out an action   
        OLECMDEXECOPT_PROMPTUSER = 1
        'This option will not prompt the user before carring out an action
        OLECMDEXECOPT_DONTPROMPTUSER = 2
        'This option will show help
        OLECMDEXECOPT_SHOWHELP = 3
 End Enum
 Private Enum Exec
        'This is the main ActiveX declaration for the webbrowser Zoom
        OLECMDID_OPTICAL_ZOOM = 63
 End Enum



Now go back to the windows forms designer, and double click 'btnZoom50'. Put this code in the Button.Click event:
        'If for any reason we encounter any errors, we just want to ignore them.
        On Error Resume Next
        'Declare an ActiveX instance for the webbrowser
        Dim wbInstance As Object = wbMain.ActiveXInstance
        'Execute the zoom. The third parameter, the nubmer is the one which tells the ActiveX instance how far to zoom in on the browser, in this case by 50%.
        wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 50, DBNull.Value)



So what did we do?

First, we told the program to ignore any errors, then made an ActiveX instance for the webbrowser (this only works in IE, or as the case may be the IE webbrowser control) and then we told the ActiveX instance to execute a certain event on the webbrowser.


So now go to the 'btnZoom100' and double click it. Add this code to the Button.Click event:
       On Error Resume Next
       Dim wbInstance As Object = wbMain.ActiveXInstance
       wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 100, DBNull.Value)


So we just did the same thing, but changed the zoom factor to 100%.

So now go to the 'btnZoom200' and double click it. Add this code to the Button.Click event:
       On Error Resume Next
       Dim wbInstance As Object = wbMain.ActiveXInstance
       wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 200, DBNull.Value)


Same old, same old but with 200%. I hope you get the idea...


Hope you enjoyed my short tutorial on webbrowser zooming. More on webbrowsers coming soon, as I'm stuck on holiday in france (cos of disruptions from the ash cloud), and I'm very bored.

:balloon:

Is This A Good Question/Topic? 1
  • +

Replies To: Webbrowser OLECMD Optical Zoom

#2 phatphamhoang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 10

Posted 18 May 2010 - 06:20 AM

Could you tell me how to Zoom in (+10%) and Zoom Out (-10%) ?

Thank you very much !

This post has been edited by phatphamhoang: 18 May 2010 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 22 May 2010 - 01:48 AM

Is your browser tabbed or not?
Was This Post Helpful? 0
  • +
  • -

#4 Steveheather  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 12

Posted 12 February 2012 - 01:36 PM

View PostJack Eagles1, on 22 May 2010 - 01:48 AM, said:

Is your browser tabbed or not?


Hi Jack,

I have tried your code on my TabControl box for my tabbed web browser, but it doesn't except the code because of the ActiveX. Is there a way of changing your code so that it works in the TabControl?

I am using Visual Studio 2010.

Regards,

Steve
Was This Post Helpful? 0
  • +
  • -

#5 gamer6336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Posted 12 March 2012 - 02:27 PM

View PostSteveheather, on 12 February 2012 - 01:36 PM, said:

View PostJack Eagles1, on 22 May 2010 - 01:48 AM, said:

Is your browser tabbed or not?


Hi Jack,

I have tried your code on my TabControl box for my tabbed web browser, but it doesn't except the code because of the ActiveX. Is there a way of changing your code so that it works in the TabControl?

I am using Visual Studio 2010.

Regards,

Steve


Tag the browser to the TabPage and then refer to it (I've done this in my code)
You'll need an empty TabControl, and a textbox and Button for navigating
First: make a browser object and tag it to a TabPage instance (perhaps in Form_load event, or in a Button)
 Dim NewBrowser As New WebBrowser
        Dim NewTab As New TabPage
        NewBrowser.Tag = NewTab
        NewTab.Tag = NewBrowser
        TabControl1.TabPages.Add(NewTab)
        NewTab.Controls.Add(NewBrowser)
        NewBrowser.Dock = DockStyle.Fill



Then use that tag to refer to the currently selected tabs browser and then use the ActiveX stuff. Make sure you've set up the enums earlier :)
 Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
        Dim wbInstance As Object = WB.ActiveXInstance
        wbInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DONTPROMPTUSER, 150, DBNull.Value)


If you need to navigate, use the tag again:
Dim WB As WebBrowser = TabControl1.SelectedTab.Tag
WB.Navigate


Hope that helps, if you need explanation, please ask :)
Was This Post Helpful? 0
  • +
  • -

#6 gamer6336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Posted 12 March 2012 - 02:33 PM

Sorry, that last snippet should be
Dim WB As WebBrowser = TabControl1.SelectedTab.Tag
WB.Navigate(TextBox1.Text)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1