Check Window Status with WinGet, ExStyle (AutoHotkey Tip)

ExStyle Settings Help to Polish AutoHotkey Window Manipulation Scripts, Plus a Couple of Tricks

Each window in Microsoft Windows includes style settings (Style and ExStyle) which control its appearance and action. You can view these settings with the CheckStyles.ahk script discussed in the blog “The WinSet, ExStyle Command for Mouse-Click Transparent Windows (Intermediate AutoHotkey Tip).” If you build AutoHotkey window manipulation tools, then you’ll find CheckStyles.ahk indispensable both as a quick reference and a tester. The CheckStyles.ahk script displays the settings for any window under the mouse cursor.

As the mouse cursor hovers over an open Untitled Notepad window, the ExStyle settings for that window appear in the second tab of the Window Styles/ExStyles pop-up in the CheckStyles.ahk script. Notice the WS_EX_LAYERED setting turns on after the using the transparency wheel in the SeeThruWinWheel.ahk script.

I edited the original unfinished CheckStyles.ahk script eliminating the two extra tabs on the right. Those tabs acted as placeholders for future features which never materialized. In any case, the most important capabilities appear in these first two tabs.

As previously mentioned and discussed in the referenced blog, the window Styles/ExStyle app allows you to see real-time changes in window style settings as you hover the mouse cursor over any window. This makes it easier to determine what settings control what features in a Windows’ window.

AutoHotkey Library Deal
AutoHotkey Library Deal

Note: In the referenced blog, I did not mention another useful (yet potentially dangerous) feature of the CheckStyles.ahk script. Press the PrtScrn (Print Screen) key while hovering over a window and the app freezes the display and toggles into a style editing mode for that window. Using this editing mode might help you determine which Style/ExStyle you need for adding certain features. However, unless you understand how each window characteristic works (some features use a combination of settings), you may encounter unpredictable behavior which forces you to reset the program or use Task Manager to close an app. On the safe side, I tend to use CheckStyles.ahk for observation of changes, then reset parameters through other AutoHotkey commands in a script.

The original transparency script assumed that any new window was totally opaque (visibility level of 255), assigning that value to the variable. While this is usually the case, any window could conceivably start at a different level. Using the CheckStyles.ahk app allowed me to note that WS_EX_LAVERED activates when I first make a window partially transparent with the Hotkey combinations found in the SeeThruWinWheel.ahk script. I used this setting to determine the transparency level (if any) for the window under the mouse with the following subroutine:

WinGet, ExStyle, ExStyle, ahk_id %currentWindow%
if (ExStyle & 0x80000) ; 0x8 is WS_EX_LAYERED.
  WinGet, TransLevel, Transparent, ahk_id %currentWindow%
  %currentWindow% := TransLevel
  %currentWindow% := 255

The WinGet command can capture either the Style or ExStyle of a window. That Style/ExStyle variable includes all of the window’s settings buried in a hexadecimal number. AutoHotkey pulls out individual settings with an expression similar to ExStyle & 0x80000 (for the WS_EX_LAYERED transparency setting). The same can be done for always-on-top (ExStyle & 0x8, WS_EX_TOPMOST) or any other Style/ExStyle setting. The saved ExStyle value represents all the current window settings by combining the various hexadecimal numbers into one. The expression Exstyle & [ExStyle in Hex] tells us if a true setting for that window style exists (the expression returns 0 for no and a positive number for yes).

In the above subroutine, after capturing the ExStyle settings, we check for WS_EX_LAYERED activation. If found true, the routine saves the transparency level. If not, it sets transparency to 255. This way any previously transparency level gets recalled as the starting point.

You can use a similar technique to unveil always-on-top status of any window.

Always-on-Top Status

While I regularly use a Hotkey combination to toggle the always-on-top status of a window on and off, I often get confused about which windows might be stuck in that mode. I find myself randomly using the key combination on a series of windows looking for the culprit locked into the wrong mode. Eventually, I end up totally lost. I needed some sign offering a positive affirmation that I turned off (or on) that priority status. The checking ExStyle technique gave me the idea to flash a message whenever I changed the state of a window.

Always-on-Top Status with a Couple of AutoHotkey Tricks

At the same time, I decided to greatly reduce the required code by including a couple of tricks from Chapter Seventeen and Chapter Eighteen of the book AutoHotkey Hotkey Techniques. By adding both a forced expression with the ternary operator to the Tooltip command, I built a conditional Tooltip comment pop-up displaying the state of always-on-top with only one line code:

  WinSet, AlwaysOnTop, toggle, A
  WinGet, ExStyle, ExStyle, A
  Tooltip, % (ExStyle & 0x8 = 0) ? "Always-On-Top OFF" : "Always-On-Top ON"
  Sleep, 1500

(Rather than word wrapped, the Tooltip command line of code in red should appear as one line of code in the AutoHotkey script.)

The Tooltip command acts as a powerful one-line conditional by inserting a forced expression (%) and the ternary operator (? :). In this case, when AutoHotkey detects WS_EX_TOPMOST off, it displays the “Always-On-Top OFF” message in the pop-up. Otherwise “Always-On-Top ON” activates with the Tooltip command.

When off, The window WS_EX_TOPMOST expression (ExStyle & 0x8) returns 0.

Tip: Be sure to enclose any text results inside the ternary operator in quotes. Even though the AutoHotkey command itself doesn’t require them, the forced expression (%) does.

WS_LAYEREDExStyleValueNote: You can determine the value of a Style/ExStyle by placing it in a MsgBox. For example, checking for transparency activation, I temporarily added the following line:

MsgBox, % ExStyle & 0x80000

In this example, WS_EX_LAYERED transparency was on and returned the value of 524288. The forced expression could just as easily use a positive value (WS_EX_TOPMOST returns 8), although the true/false results need to switch positions:

Tooltip,  % (ExStyle & 0x8 = 8) ? "Always-On-Top ON" : "Always-On-Top OFF"

I added this code to the Always_on_Top.ahk script found on the ComputorEdge Free AutoHotkey Scripts page.

Of course, when using a positive affirmation, you can always evaluate the expression as true (non-zero) or false (zero):

Tooltip,  % ExStyle & 0x8 ? "Always-On-Top ON" : "Always-On-Top OFF"

This eliminates the need for the equation within the forced expression.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s