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
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.