Jump to content
spudw2k

Always On Top Tool

Recommended Posts

Posted (edited)

Had a customer who's Outlook somehow got Always on Top (AOT) enabled and we couldn't figure out how that happened or how to undo it.  I put together a quick hack to turn it off, but then got inspired to make a (very simple) tool to allow toggling AOT on-demand via a hotkey.  It runs in the notification area.  Ctrl + Alt + T to toggle AOT for the active window.

#NoTrayIcon
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
#include <AutoItConstants.au3>
#include <WinAPI.au3>

Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode",1)

HotKeySet("^!t","_ToggleActiveWindow")
_UI()

Func _UI()
    TrayCreateItem("&About")
    TrayItemSetOnEvent(-1, "__About")
    TrayCreateItem("") ; Create a separator line.
    TrayCreateItem("E&xit")
    TrayItemSetOnEvent(-1, "__Exit")

    TraySetState($TRAY_ICONSTATE_SHOW)
    TraySetToolTip("Always on Top Tool")

    While 1
        sleep(10)
    WEnd
EndFunc

Func _ToggleActiveWindow()
    Local $hWnd = WinGetHandle("[ACTIVE]")
    Local $sTitle = WinGetTitle("[ACTIVE]")
    If $sTitle == "" Then Return -1
    $iExStyle = __GetWindowState($hWnd, $GWL_EXSTYLE)
    If BitAND($iExStyle, $WS_EX_TOPMOST) Then
        $iExStyle = BitXOR($iExStyle,$WS_EX_TOPMOST)
        $hAfter = $HWND_NOTOPMOST
        $sMsg = "off"
    Else
        $iExStyle = BitOR($iExStyle,$WS_EX_TOPMOST)
        $hAfter = $HWND_TOPMOST
        $sMsg = "on"
    EndIf
    $aWinPos = WinGetPos($hWnd)
    _WinAPI_SetWindowLong($hWnd,$GWL_EXSTYLE,$iExStyle)
    _WinAPI_SetWindowPos($hWnd, $hAfter, $aWinPos[0], $aWinPos[1], $aWinPos[2], $aWinPos[3], $SWP_FRAMECHANGED)
    __ToolTip("AOT turned " & $sMsg, $aWinPos[0] + ($aWinPos[2]/2), $aWinPos[1] + ($aWinPos[3]/2), $sTitle, 0, $TIP_CENTER + $TIP_BALLOON)
EndFunc

Func __About()
    MsgBox($MB_SYSTEMMODAL, "", "Always on Top Tool" & @CRLF & @CRLF & "Press Ctrl + Alt + T to toggle" & @CRLF & "Always on Top for the active window.", 5)
EndFunc

Func __Exit()
    HotKeySet("^!t")
    Exit
EndFunc

Func __GetWindowState($hWnd, $iIndex)
    Return _WinAPI_GetWindowLong($hWnd, $iIndex)
EndFunc

Func __ToolTip($sMsg, $iX, $iY, $sTitle, $iICon, $iOptions)
    AdlibRegister("__ToolTipOff","1500")
    ToolTip($sMsg, $iX, $iY, $sTitle, $iICon, $iOptions)
EndFunc

Func __ToolTipOff()
    ToolTip("")
    AdlibUnRegister()
EndFunc

 

Edited by spudw2k

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...