After Always-On-Top and Translucent Windows, Use the WinSet Command to Make a Mouse-Click Transparent Help Window, Plus a Discussion of the Mysterious (and Confusing) Microsoft Window Styles/ExStyles (WS_XXX and WS_EX_XXX)
The WinSet command offers many more options than merely setting windows to AlwaysOnTop and Transparent. As I reviewed WinSet, I realized that variations on the command might be worth exploring for other applications. In addition to sending a window to the bottom or bringing it to the top, as a window manipulation tool WinSet offers a variety of options for adjusting Window Styles (WinSet, Style and WinSet, ExStyle), click-through-ness (or mouse-click transparent—making clicks drops to the window below— WinSet, TransColor), and changing the window shape (WinSet, Region).
New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”
Get this overview and introductory e-book free, AutoHotkey Tricks You Ought to Do with Windows!
* * *
The WinSet, Style/ExStyle Command
The WinSet, Style/ExStyle command accesses special Window Style (WS_XXXX) codes built into Microsoft Windows. Appearing as hexadecimal codes (a 0x preceding the number indicates hexadecimal), understanding what they are and how to use them takes a little more effort. From the AutoHotkey online documentation:
WinSet, Style, -0xC00000, A ; Remove title bar (WS_CAPTION). WinSet, ExStyle, ^0x80, WinTitle ; Removes/adds window ; (or toggles) from alt-tab list (WS_EX_TOOLWINDOW).
The WinSet command implements the new style with the assigned Hex code preceded by either a minus sign – , a plus sign + (to turn off or on the style), or a caret ^ for toggling a setting on and off. Although simple to use, the only list of Window Style codes readily available for AutoHotkey fell short of my expectations. The list includes only a few of the Window Styles (WS_XXXX) and none of the Extended Window Styles (WS_EX_XXXX)—which happens to include the code for making a window mouse click-through-able.
I didn’t start to grasp how WinSet, Style/ExStyle actually works until I found this AutoHotkey script by djcpal1 in the old AutoHotkey forum. The routine opens a GUI called windowstyles which displays many of the more useful Styles/ExStyles implemented for the window underneath the hovering mouse cursor.
The information inside windowstyles, while cryptic, offers insight into how to use the Style/ExStyle codes. These codes come from the Window Styles (WS_XXXX) and Extended Window Styles (WS_EX_XXXX) settings available in Microsoft Windows. The image above displays both the hexadecimal code and the associated WS_XXXX or WS_EX_XXXX setting name. Designating the particular style with the code in the WinSet, Style/ExStyle command tells AutoHotkey which setting to change. But directly using these codes can get complicated.
Note: To get a partial understanding of the effect of each Window Style, check the linked Window Style pages above—although the descriptions don’t always help. Simply inserting the code into the WinSet command may yield a desired result, but it often gets more complex than that.
For example, Microsoft assigned WS_EX_TOPMOST (0x8) as the Window Style for always-on-top. You might think that this would provide an alternative method to toggle a window always-on-top with WinSet, however additional requirements appear in the Microsoft documentation (“use the SetWindowPos function”)—which makes it even more confusing. This may be why AutoHotkey handles it directly by including the AlwaysOnTop option in WinSet, as well as, other commands.
The easiest way to determine the usability of a Window Style may be by trying it. I found that toggling the window resizing setting (WS_SIZEBOX) works:
WinSet, Style, ^0x40000, A
But in many cases, changing WS_XXXX values for an active window wouldn’t take effect unless there were adjustments made to other settings in conjunction with it. Many times values must be set before the window gets displayed.
Often Microsoft explanations confuse due to their use of ambiguous terms. I ran into this when looking at how to make a window mouse-click transparent (or click-through-able).
Making a Window Transparent Vs. Mouse-Click Transparent
While, as discussed last time, the WinSet command includes an option for setting the transparency of a window (WinSet, Transparent,…), it does not include a direct option for allowing mouse clicks to drop through an entire window. The WinSet, TransColor command (a topic for next time) offers mouse-click invisibility for specified colors, but not the entire window. For that we turn to WinSet, ExStyle. But first, the terms transparent and invisible need clarifying.
Microsoft has done a horrible job of naming various Window Styles. In the Microsoft documentation you see terms such as transparent and invisible used in a variety of different ways with little or no clarification. But for our purposes, I use two specific types of transparency or invisibility. The first is opaqueness which denotes the level of visual transparency. Visual transparency defines how well you can see through one window to a window underneath.
The second form of transparency describes mouse-click transparency. That means any click of the mouse on the window drops through to the window below. The window is invisible to the mouse, but not to the eye. The distinction is important.
You might expect that WS_EX_TRANSPARENT sets how well you can see through a window. It doesn’t. Whether a window is translucent or opaque is wrapped up in the WS_EX_LAYER feature. To allow any window to become translucent, AutoHotkey initiates WS_EX_LAYER settings with the WinSet, Transparent command—handling the other functions needed to set the transparency level.
When turned on, WS_EX_TRANSPARENT allows mouse clicks to drop through an entire window—although WS_EX_LAYER must first be set on for it to work. AutoHotkey does not include a mouse-click transparent option, therefore the WinSet, ExStyle command must be used to make an entire window mouse-click transparent.
Note: The WinSet, TransColor command does offer mouse-click transparency for a single color, but not for the other colors in the window. The use of the term TransColor tends to add to the confusion. Next time, I discuss a practical use for the TransColor mouse-click transparent option.
Toggling Mouse-Click Transparency On and Off
Last time I referenced a script I found in the old AutoHotkey forum for making a window always-on-top or transparent. In that same script you’ll find Hotkeys for changing any window to mouse-click transparent and back again. The following modified version of those Hotkeys include one for turning mouse-click transparency on and one for turning it off:
^!x:: ; Make mouse-click transparent WinGet, currentWindow, ID, A WinSet, ExStyle, +0x80020, ahk_id %currentWindow% Return ^!z:: ; Undo mouse-click transparent WinSet, ExStyle, -0x20, ahk_id %currentWindow% Return
The Hotkey combination CTRL+ALT+X (^!x) turns on mouse-click transparency by first grabbing the active window ID with the WinGet command, placing it in the variable currentWindow, then setting mouse-click transparency on with the WinSet, ExStyle command.
Initially, I found the ExStyle code used in this example perplexing. I couldn’t find a reference to 0x80020 anywhere in the Microsoft documentation. I eventually realized that the code was a combination of two codes. The WS_EX_LAYER window style (0x80000) and the WS_EX_TRANSPARENT windows style (0x20). The window layer style must be turned on for the mouse-click transparent mode to work.
If a window already has WS_EX_LAYER set on, then the AutoHotkey line only requires the mouse-click transparent code (0x20). Therefore, the second Hotkey (CTRL+ALT+Z—^!z), which turns the mouse-click transparency off, only needs that shortened ExStyle code (-0x20).
Note: The original script also includes a MouseGetPos command in the restore Hotkey routine. However, this command serves no purpose since MouseGetPos won’t detect a mouse-click transparent window. Plus, these on and off Hotkeys only work with one window at a time since using the mouse-click transparent Hotkey with another window resets the value of the variable currentWindow.
The following line of code offers a more universal toggle for mouse-click transparency:
^#F12::WinSet, ExStyle, ^0x80020, A
While this alternative approach works with any active window (A), it creates a problem when toggling off the click-through feature—the window must be active, yet it can’t be activated with a mouse click. One solution involves first activating the mouse-click transparent window from the program icon in the Windows Taskbar.
Always-On-Top, Transparent, Mouse-Click-Through Help Windows
All this discussion about a unique window feature gets pretty involved, but does it have any practical application? How can I use these features in a practical way?
As an initial thought, you can create Help windows which won’t interfere with your work.
First, the Help window must pop-up always-on-top so it doesn’t get lost. Second, you should be able to see the working window underneath. Third, the pop-up should not interfere with mouse-click operations.
The following example script creates a Help GUI window using the WinSet command to set all three of the above conditions (AlwaysOnTop, visual transparency, and mouse-click transparency) while, since we can’t remove the window with a mouse-click, including an important escape Hotkey to close the Help pop-up:
^F12:: Gui, Add, Text,, 1. Sit up straight Gui, Add, Text,, 2. Look at the keyboard Gui, Add, Text,, 3. Drink a cup of coffee Gui, Add, Text,, CTRL+ALT+F12 to close help window. Gui, Show, x800 y20, Transparent Help Menu WinGet, active_id, ID, Transparent Help Menu WinSet, AlwaysOnTop, On, ahk_id %active_id% WinSet, Transparent, 190, ahk_id %active_id% WinSet, ExStyle, +0x80020,ahk_id %active_id% Return ^!F12::Gui, Destroy
Although this example creates a specific GUI window, you can use the individual Hotkeys discussed in this and previous blogs to set these conditions for any active Help window.
The following line of code, when included at the top of the GUI setup, demonstrates an alternative method for setting a couple of these conditions in a GUI window :
Gui, +AlwaysOnTop +E0x20
This line forces the GUI always-on-top and makes it mouse-click transparent (+E0x20—the Hex code), however an addition option for setting GUI visual transparency does not exist. Using the WinSet command as shown in the initial example is necessary.
Next time, a look at the WinSet, TransColor command with a practical example which offers mouse-click transparency and visual transparency for a single color.