Convert helgef's WinHole

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Convert helgef's WinHole

Post by 20170201225639 » 26 Aug 2023, 02:31

Link: viewtopic.php?t=30622

This is the one of the most unusual, at times hilarious, but also unexpectedly useful ahk scripts I've ever come across.

Please, @Helgef or someone else, convert it to v2!

Rohwedder
Posts: 7733
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Convert helgef's WinHole

Post by Rohwedder » 26 Aug 2023, 06:15

Sorry,
I have not converted it, only shortened, F2:: now toggles circle/square:

Code: Select all

#Requires AutoHotkey v1.1.33
; Note: Exit script with Esc::
OnExit("exit")
; Settings
radius := 105	; Starting radius of the hole.
increment := 25	; Amount to decrease/increase radius of circle when turning scroll wheel
rate := 40	; The period (ms) of the timer. 40 ms is 25 "fps"
shape := 0 ; 0 = circle, 1 = square 
; Script settings
SetWinDelay, -1
listlines, off ; Remove when debugging.
F1::timer(toggle:=!toggle, rate), pause := 0 ; Toggle on/off
#if toggle
F2::shape:=!shape, timer(pause?-1:1)	; When on, toggle shape: circle/square  
F3::timer((pause:=!pause)?-1:1)			; When on, toggle pause.
WheelUp::	; Increase the radius of the circle
WheelDown::	; Decrease 	-- "" --
InStr(A_ThisHotkey, "Up") ? radius+=increment : radius-=increment
radius<1 ? radius:=1 : "", timer(1)	; Ensure greater than 0 radius
return
#if
esc::exit()	; Exit script with Esc::
exit(){
	timer(0) ; For restoring the window if region applied when script closes.
	ExitApp
	return
}
timer(state, rate:=50){
	; Call with state=0 to restore window and stop timer, state=-1 stop timer but do not restore
	; rate, the period of the timer.
	static timerFn, paused, hWin, aot
	if (state=0)
	{ ; Restore window and turn off timer
		if timerFn
			SetTimer,% timerFn, Off
		if !hWin
			return
		WinSet, Region,,% "ahk_id " hWin
		if !aot	; Restore not being aot if appropriate.
			WinSet, AlwaysOnTop, off,% "ahk_id " hWin
		hWin:="", timerFn:="", aot:="", paused:=0
		return
	} else if (timerFn)
	{ ; Pause/unpause or...
		if (state=-1){
			SetTimer,% timerFn, Off
			return paused:=1
		} else if paused {
			SetTimer,% timerFn, On
			return paused:=0
		} else ; ... stop timer before starting a new one.
			SetTimer,% timerFn, Off
	}
	if !hWin{ ; Get the window under the mouse.
		MouseGetPos,,,hWin
		WinGet, aot, ExStyle,% "ahk_id " hWin 	; Get always-on-top state, to preserve it.
		if !aot&=0x8
			WinSet, AlwaysOnTop, On,% "ahk_id " hWin
	}
	timerFn:=Func("timerFunction").Bind(hWin)	; Initialise the timer.
	timerFn.Call(1)	; For better responsiveness, 1 is for reset static
	SetTimer,% timerFn,% rate
}
timerFunction(hWin,resetStatic:=0){
	; Get mouse position and convert coords to win coordinates, for displacing the circle
	static px, py
	WinGetPos, wx, wy,,,% "ahk_id " hWin
	CoordMode, Mouse, Screen
	MouseGetPos, x, y
	x-=wx, y-=wy
	if (x=px && y=py && !resetStatic)
		return
	else px:=x, py:=y
	WinSetRegion(hWin, x, y)
	return
}
WinSetRegion(hWin, dx:=0, dy:=0){
	Global radius, shape
	WinGetPos,,, w, h,% "ahk_id " hWin
	outer := DllCall("CreateRectRgn", "Int", 0, "Int", 0, "Int", w, "Int", h)
	inner := DllCall(shape?"CreateRectRgn":"CreateEllipticRgn", "Int"
	, dx-radius, "Int", dy-radius, "Int", dx+radius, "Int", dy+radius)
	DllCall("CombineRgn", "UInt", outer, "UInt", outer, "UInt", inner, "Int", 3) ; RGN_XOR = 3
	DllCall("SetWindowRgn", "UInt", hWin, "UInt", outer, "UInt", true)
}
; by Helgef, Date: 2017-04-15
; Rohwedder: replaced makeCircle(), F2:: now toggles circle/square

20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: Convert helgef's WinHole

Post by 20170201225639 » 26 Aug 2023, 10:43

Rohwedder wrote:
26 Aug 2023, 06:15
Sorry,
I have not converted it, only shortened, F2:: now toggles circle/square:
Thanks Rohwedder for sharing this! I still have an v1 executable on my machine, so I can bind a shortcut to run this script from v1 when pressed.

This inspired me to actually take a look at helgef's source code. Somehow I thought it'd be full of DllCalls and tricky to convert to v2, but it actually just uses winset region.

Post Reply

Return to “Ask for Help (v2)”