是否可以捕捉关闭按钮并最小化窗口?自动热键 [英] Is it possible to catch the close button and minimize the window instead? AutoHotKey

查看:39
本文介绍了是否可以捕捉关闭按钮并最小化窗口?自动热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想通过右键单击任务栏图标并选择关闭来关闭计算机中的所有程序.因此,当我单击任何窗口上的 X 按钮时,它应该转换为最小化.如何使用 AutoHotKey 实现此目的?

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 to minimize instead. How can I achieve this using AutoHotKey?

推荐答案

这个 AutoHotkey 脚本应该可以实现您的需求.注意:如果航空模式开启,代码可能无法正常工作.

This AutoHotkey script should achieve what you're looking for. Note: the code may not work correctly if aero mode is on.

  • MouseGetPos 获取光标所在窗口的hWnd.
  • WM_NCHITTEST 获取有关光标下项目类型的信息,需要一些繁琐的数学运算才能将坐标组合成 4 字节结构.
  • 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.

如果某些程序没有返回关闭按钮的标准 NCHITTEST 值,我相信 Winamp 2.5e 和以前版本的 Mozilla Firefox 都是如此,那么解决方法是检查程序的class/exe,并计算光标下相对于窗口的位置是否与关闭按钮一致.

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

这篇关于是否可以捕捉关闭按钮并最小化窗口?自动热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆