下面的代码应该做你想要达到的目标:
;tested on Notepad (Windows 7)
;note: may not work correctly if aero mode is on
;note: if you click a Notepad window that is *not* the active window, it will be closed
;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
#IfWinActive, ahk_class Notepad
LButton::
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass not in Notepad
Return
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, hWnd2
if (hWnd = hWnd2)
{
SendMessage, 0x84, 0, vPosX|(vPosY<<16), , ahk_id %hWnd% ;WM_NCHITTEST
vNCHITTEST := ErrorLevel ;(8 min, 9 max, 20 close)
if (vNCHITTEST = 20)
{
MsgBox can't close me! ;put your code here
Return
}
}
SendInput {LButton Down}
KeyWait, LButton
SendInput {LButton Up}
Return
#IfWinActive
注: 张贴在下面的链接的代码,实现了一些相关的,醒目关闭按钮所有窗口,而不仅仅是一个程序,无论它们是活动的还是非活动的。
Is it possible to catch the close button and minimize the window instead? AutoHotKey