I want all programs in my computer to be closed only by right clicking the taskbar icon and choosing close. So when I click the X button on any window, it should translate t
This AutoHotkey script should achieve what you're looking for. Note: the code may not work correctly if aero mode is on.
MouseGetPos
gets the hWnd
of the window under the cursor.WM_NCHITTEST
gets information about the type of item under the cursor, needing a bit of fiddly maths to combine the coordinates into a 4-byte structure.The difficult bit is that every time you use the mouse, this script captures every left mouse button press, but must not interfere with the mouse's normal functioning. When the mouse button is pressed, no presses are sent, the press is suppressed. A check is done on the window below the cursor. If a close button is found, then that window is minimised, and we never actually send a mouse press. If no close button is found we emulate the normal function of the mouse: a down press is sent, until it is detected that the mouse button has been released, and a mouse release is sent. I use the word 'press' because it is a clearer word than 'click' or 'hold' which imply quick/long durations.
This solution is in fact quite simple, but the issue gave me a lot of trouble, some years ago when I first investigated it and tested out different approaches. I have never had the mouse functionality compromised, but I can't guarantee, there isn't some software where it won't work, or would work incorrectly.
If some programs don't return the standard NCHITTEST
value for a close button, I believe this was true of Winamp 2.5e and previous versions of Mozilla Firefox, then a workaround is to check for the class/exe of the program, and work out if the location under the cursor relative to the window is consistent with the close button.
Btw I use the same principles for my 'minimiser' script that I plan to release in possibly the next few months, where if you right-click the minimise button, it minimises the window to the system tray.
;tested on Windows 7
;note: may not work correctly if aero mode is on
;note: some programs don't return the standard NCHITTEST value for a close button,
;a workaround is to compare the cursor position against the window coordinates
LButton::
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, hWnd
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass not in BaseBar,#32768,Shell_TrayWnd,WorkerW,Progman,DV2ControlHost
{
SendMessage, 0x84, 0, vPosX|(vPosY<<16), , ahk_id %hWnd% ;WM_NCHITTEST
vNCHITTEST := ErrorLevel ;(8 min, 9 max, 20 close)
;ToolTip %vNCHITTEST%
if (vNCHITTEST = 20)
{
WinMinimize, ahk_id %hWnd%
Return
}
}
SendInput {LButton Down}
KeyWait, LButton
SendInput {LButton Up}
Return