But titlebar detection is not perfect. For example, sometimes it fails to detect title bar (Windows 11 file explorer window), and sometimes false-detects titlebar in any point of window's background (PowerToys settings window). How could it be improved?
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#If MouseIsOverTitlebar()
RButton::WinMinimize
MButton::WinClose
WheelUp::WinMaximize
WheelDown::WinRestore
MouseIsOverTitlebar() {
static WM_NCHITTEST := 0x84, HTCAPTION := 2
CoordMode Mouse, Screen
MouseGetPos x, y, w
if WinExist("ahk_class Shell_TrayWnd ahk_id " w) ; Exclude taskbar.
return false
SendMessage WM_NCHITTEST,, x | (y << 16),, ahk_id %w%
WinExist("ahk_id " w) ; Set Last Found Window for convenience.
return ErrorLevel = HTCAPTION
}