Optical Zoom For The Web Browser Control

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Optical Zoom For The Web Browser Control

03 Sep 2016, 19:13

I've seen a few examples for how to perform an optical zoom on the web browser control but I've never seen a method to programmatically perform a zoom adjustment, i.e. zoom in/out from the current zoom factor. I found an obscure post on the interwebs that gave me a few clues and I put together the following example.

Code: Select all

#NoEnv
#SingleInstance Force
ListLines Off

;-- Constants
OLECMDID_OPTICAL_ZOOM        :=63
OLECMDEXECOPT_DODEFAULT      :=0
OLECMDEXECOPT_PROMPTUSER     :=1
OLECMDEXECOPT_DONTPROMPTUSER :=2
OLECMDEXECOPT_SHOWHELP       :=3

;-- GUI options
gui -DPIScale +hWndhGUI +Resize
gui Margin,0,0

;-- Add the window handle to a group
GroupAdd ExampleGUI_Group,ahk_id %hGUI%

;-- GUI objects
gui Add,ActiveX,hWndhWB vWB,Shell.Explorer
gui Add,StatusBar,+hWndhStatusBar vSB
SB_SetText("Use Ctrl+Numpad+ to zoom in, Ctrl+Numpad- to zoom out, and Ctrl+0 to reset zoom")

;-- Get status bar statistics
GUIControlGet $SB,Pos,SB

;-- Navigate to a web page
WB.Navigate("https://autohotkey.com/")

;-- Show it
gui Show,w600 h400,Optical Zoom Demo
return


GUISize:

;-- Adjust the web browser control
GUIControl
    ,Move
    ,WB
    ,% ""
        . "w" . A_GUIWidth . A_Space
        . "h" . A_GUIHeight-$SBH . A_Space

return


GUIClose:
GUIEscape:
ExitApp


/*
    Poor man's optical zoom.  Tries (i.e. uses "Try") to perform optical zoom.
    Success if the control responds accordingly or does nothing because it was
    already at the requested size.  Script ignores COM error if the control is
    not ready to accept command or if the command or any of the properties are
    not supported.  Try is also used because the commands can fail if the web
    browser control is busy.

    Note: A_ScreenDPI probably could be used instead of
    WB.document.frames.screen.systemXDPI but the latter is used in case there is
    variation in values somewhere.
*/

OpticalZoomDecrease:
Try
    {
    ;-- Calculate the current zoom factor where 100 = No zoom
    ZoomFactor:=Round((WB.document.frames.screen.deviceXDPI/WB.document.frames.screen.systemXDPI)*100)

    ;-- Decrease by 5
    ZoomFactor-=5

;;;;;    ;-- ##### Experimental
;;;;;    if A_ThisHotkey and (A_ThisHotkey=A_PriorHotkey)
;;;;;        if (A_TimeSincePriorHotkey<50)
;;;;;            ZoomFactor-=5

    ;-- Adjust to factor in the current screen DPI
    ZoomFactor:=Round(ZoomFactor*(WB.document.frames.screen.systemXDPI/96))

    ;-- Zoom
    WB.ExecWB(OLECMDID_OPTICAL_ZOOM,OLECMDEXECOPT_DONTPROMPTUSER,ZoomFactor)

    ;-- Recalculate the current zoom factor
    ZoomFactor:=Round((WB.document.frames.screen.deviceXDPI/WB.document.frames.screen.systemXDPI)*100)

    ;-- Update the status bar
    SB_SetText("Zoom decreased to " . ZoomFactor . "%")
    return
    }

;-- Try failure
SoundPlay *-1  ;-- System default beep
return


OpticalZoomIncrease:
Try
    {
    ;-- Calculate the current zoom factor where 100 = No zoom
    ZoomFactor:=Round((WB.document.frames.screen.deviceXDPI/WB.document.frames.screen.systemXDPI)*100)

    ;-- Increase by 5
    ZoomFactor+=5

;;;;;    ;-- ##### Experimental
;;;;;    if A_ThisHotkey and (A_ThisHotkey=A_PriorHotkey)
;;;;;        if (A_TimeSincePriorHotkey<50)
;;;;;            ZoomFactor+=10

    ;-- Adjust to factor in the current screen DPI
    ZoomFactor:=Round(ZoomFactor*(WB.document.frames.screen.systemXDPI/96))

    ;-- Zoom
    WB.ExecWB(OLECMDID_OPTICAL_ZOOM,OLECMDEXECOPT_DONTPROMPTUSER,ZoomFactor)

    ;-- Recalculate the current zoom factor
    ZoomFactor:=Round((WB.document.frames.screen.deviceXDPI/WB.document.frames.screen.systemXDPI)*100)

    ;-- Update the status bar
    SB_SetText("Zoom increased to " . ZoomFactor . "%")
    return
    }

;-- Try failure
SoundPlay *-1  ;-- System default beep
return


OpticalZoomReset:
Try
    {
    ;-- Set to no zoom (i.e. 100) and adjust to factor in the current screen DPI
    ZoomFactor:=Round(100*(WB.document.frames.screen.systemXDPI/96))
    WB.ExecWB(OLECMDID_OPTICAL_ZOOM,OLECMDEXECOPT_DONTPROMPTUSER,ZoomFactor)

    ;-- Recalculate the current zoom factor
    ZoomFactor:=Round((WB.document.frames.screen.deviceXDPI/WB.document.frames.screen.systemXDPI)*100)

    ;-- Update the status bar
    SB_SetText("Zoom reset to " . ZoomFactor . "%")
    return
    }

;-- Try failure
SoundPlay *-1  ;-- System default beep
return


;*****************
;*               *
;*    Hotkeys    *
;*               *
;*****************
;-- Begin #IfWinActive directive
#IfWinActive ahk_group ExampleGUI_Group

^]::
^NumpadAdd::
^WheelUp::
    ;-- This keyboard shortcut is built-in to the web browser control.  However,
    ;   it is defined here so that it will override the default functionality.
gosub OpticalZoomIncrease
return


^[::
^NumpadSub::
^WheelDown::
    ;-- This keyboard shortcut is built-in to the web browser control.  However,
    ;   it is defined here so that it will override the default functionality.
gosub OpticalZoomDecrease
return


^0::
^Numpad0::
^NumpadIns::
gosub OpticalZoomReset
return

;-- End #IfWinActive directive
#IfWinActive
This works on my Window 7 machine with my screen DPI (currently set at 125%). I read stuff that indicates that other Windows versions may operate differently so I'm very curious to find out if this works on other-than-Windows7. Please let me if works for you and if not, let me know what happens.

Also note that this solution won't work on a PC that is using a web browser control that doesn't support any of the methods or properties used by this script. This may occur if using an older version of the web browser control.

I hope that someone finds this useful.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Optical Zoom For The Web Browser Control

04 Sep 2016, 05:21

Thanks for sharing.
Works fine it seems, same OS: Win7, but: DPI (currently set at 100%).
just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Optical Zoom For The Web Browser Control

04 Sep 2016, 07:01

It's working on Win 10 x64 (100 % DPI).
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Optical Zoom For The Web Browser Control

04 Sep 2016, 13:34

works on Win XP SP3
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: Optical Zoom For The Web Browser Control

05 Sep 2016, 00:15

Thanks for the feedback. If the Collaboration forum existed I would have posted it there first to work out the kinks but this will do for now.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Optical Zoom For The Web Browser Control

23 Sep 2018, 14:44

Can this be modified for Chrome?
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: Optical Zoom For The Web Browser Control

23 Sep 2018, 18:26

carno wrote:Can this be modified for Chrome?
No (probably). This example was written specifically for the web browser control that is built-in to most versions of Windows. I am not a Chrome expert but from what I understand, Chrome uses a custom control and so it is very likely that most of the code in this example would provide no value for the Chrome web browser control. Sorry 'bout that.
User avatar
rommmcek
Posts: 1474
Joined: 15 Aug 2014, 15:18

Re: Optical Zoom For The Web Browser Control

24 Sep 2018, 00:36

Tested on Win10 x64 120 Dpi (125% - scale factor), excelent work!
For me OpticalZoomReset has The special value, since zooming alone works by default via Send, ^{WheelUp} & Send, ^{WheelDown}.

Off topic: To prevent Script Error warning I have to use:

Code: Select all

...
;-- Navigate to a web page
wb.Silent := true
WB.Navigate("https://autohotkey.com/")
...
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: Optical Zoom For The Web Browser Control

24 Sep 2018, 10:02

@jballi, thanks for the script.
@rommmcek, thank you, that error was driving me nuts, and not only on this script. I'll be using "wb.Silent := true" in other places now.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 177 guests