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

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

问题描述

我只想通过右键单击任务栏图标并选择关闭"来关闭计算机中的所有程序.因此,当我单击任意窗口上的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都是如此,一种解决方法是检查程序,并确定光标相对于窗口的位置是否与关闭按钮一致.

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天全站免登陆