Make Odd Shaped Windows with the AutoHotkey WinSet, Region Command, Plus a Script for Creating a Mouse Spotlight
It rarely occurs to anyone that a window can be anything other than rectangular. We almost exclusively see Windows’ windows as boxes. When something different pops up,
who knows what to do with it? That’s the way I feel about the WinSet, Region command. While I find the possibilities for reshaping a window cool, I have trouble visualizing applications. I did find a spotlight script which puts an invisible octagon around the mouse cursor (discussed below), but even that appears limited in its use. Maybe in games. I leave it up to you to decide how to use this fun AutoHotkey technique.
Similar to the WinSet, TransColor option discussed in the last blog, the AutoHotkey command WinSet, Region makes part of a window invisible to both the eye and mouse clicks. But rather than working on a single color, any part of the window outside the designated Region gets removed as if it doesn’t exist. This creates a lot of possibilities, but be careful. When you crop a window, you will most likely remove the title bar, tools, and border. If you don’t set up a hotkey (or other method) for exiting, plan to close the window from the Windows Taskbar or System Tray icon.
By adding the proper coordinates, dimensions, and options, you can apply a special shape to any window. The area within the shape stays active, while anything outside the boundaries disappears. This technique might be used to create floating buttons—no window frame or title bar.
I threw together a test script so I could experience the results from the command. (I find nothing works better for providing AutoHotkey command insights and stimulating ideas than running short scripts.)
The script uses the Gui, Add, Picture command to display an image of two kids playing soccer inside a 300 pixel wide pop-up. (Substitute any image file to achieve a similar result.) After displaying the window (Gui, Show), the WinSet command cuts out an elliptical shape using the E option—discarding everything outside the ellipse. Since the command deletes the window borders and close box x, the window can’t be dragged or closed (except by hovering over the Taskbar program icon and clicking the x in the pop-up image or right-clicking on the AutoHotkey icon in the notification area of the Taskbar and selecting Exit).
Gui, Add, Picture, w300, SoccerPlay.png Gui, Show WinSet, Region, 40-50 W250 H350 E, WinEllipse.ahk
The E option in the WinSet, Region command creates the ellipse. The 40-50 (x-y coordinates)—as a single set—designates the location of the upper left-hand corner of the Region. The WinSet command designates coordinates for all Region options using this x-y pixel format (x pixels across and y pixels down separated by a hyphen – ). The width (W250) and height (H350) constrain the shape of the crop. Delete the E option (ellipse) to create a rectangle with those same dimensions. Add rounded corners to the rectangle with the R(w-h) option. The w-h (width-height) determines the radius of the rounded corners.
* * *
Get your free download of the e-book AutoHotkey Tricks You Ought to Do with Windows. The new Third Edition includes a number of useful AutoHotkey techniques, plus the Table of Contents and the Indexes from the other five e-books. Pick it up as an easy download in any of the three formats: EPUB, MOBI, and PDF.
* * *
Remove the W250 and H350 options and AutoHotkey looks for multiple sets of coordinates to outline a polygon. Each set of coordinates (x-y) separated by a space lays out the new window shape in order. This polygon technique creates the spotlight discussed below—although this script uses a clever trick making the invisible spot appear inside rather than outside the window polygon outline.
A Practical Application? Create a Spotlight by Punching a Hole in a GUI Window Background
I searched the Web and found this Spotlight using WinSet, Region script. It provides the interesting effect of an invisible spotlight, both surrounding and following the mouse cursor, which highlights and clicks-through to the window underneath it. I’ve inserted the script using word-wrapped italics due to the exceeding long WinSet, Region command in the Alert2 subroutine:
Gui, Margin , 0, 0
Gui, -Caption +AlwaysOnTop
Gui, Color, 000000
Gui, Show, Maximize, Mouse Spotlight
WinSet, Transparent, 200, Mouse Spotlight
WinSet, Region, 0-85 0-%A_ScreenHeight% %A_ScreenWidth%-%A_ScreenHeight% %A_ScreenWidth%-0 0-0 0-85, Mouse Spotlight
SetTimer, Alert2, 50
MouseGetPos, OutputVarX3, OutputVarY3, OutputVarWin, OutputVarControl
OutputVarX1:= OutputVarX3 -80
OutputVarX2:= OutputVarX3 -56
OutputVarX4:= OutputVarX3 +56
OutputVarX5:= OutputVarX3 +80
OutputVarY1:= OutputVarY3 -80
OutputVarY2:= OutputVarY3 -56
OutputVarY4:= OutputVarY3 +56
OutputVarY5:= OutputVarY3 +80
WinSet, Region, 0-85 0-%A_ScreenHeight% %A_ScreenWidth%-%A_ScreenHeight% %A_ScreenWidth%-0 0-0 0-85 %OutputVarX1%-%OutputVarY3% %OutputVarX2%-%OutputVarY4% %OutputVarX3%-%OutputVarY5% %OutputVarX4%-%OutputVarY4% %OutputVarX5%-%OutputVarY3% %OutputVarX4%-%OutputVarY2% %OutputVarX3%-%OutputVarY1% %OutputVarX2%-%OutputVarY2% %OutputVarX1%-%OutputVarY3%, Mouse Spotlight
In this script, the always-on-top GUI window (Gui, -Caption +AlwaysOnTop) covers the entire screen (Gui, Show, Maximize, Mouse Spotlight) with a semi-transparent (WinSet, Transparent, 200, Mouse Spotlight) black background (Gui, Color, 000000). But how does the script draw the invisible spotlight inside the window?
Here’s the trick! The coordinates actually draw the spotlight external to the boundaries created by the first putting the entire maximized window covering the screen inside the crop, then continues to cut out the spotlight with the calculated coordinates for the octagon. This creates an internal polygon technically external to the main window shape—even though it appears inside the image. This interior cropping works as long as the entire set of points (background image and spotlight) terminates at the same starting point. In fact, any number of polygons can poke holes in the background as long as the boundary is continuous from beginning to end. (Still confused? Try plotting all the points on graph paper.)
The script uses the SetTimer command to reset the spotlight to follow the mouse cursor every 200 milliseconds by recalculating the spotlight coordinates (the eight corners of the octagon) used by WinSet, Region. (Another examples of the SetTimer command appears in the Reminder script.)
While the possibilities for different WinSet, Region shapes abound, I’m not sure what to do with it. However, in my experience, someone out there is saying, “That’s just what I was looking for!”