Events 自动热键:在窗口事件上运行代码(关闭)

Events 自动热键:在窗口事件上运行代码(关闭),events,Events,我使用一个本地应用程序,当按下ESCAPE时,当前窗口关闭。我想避免这种情况,所以我制作了一个带有自动热键的脚本,可以检测应用程序中窗口的名称,如果我按ESCAPE,窗口不会关闭(除非我按X) #如果此处有应用程序类的活动ahk类名称( Escape::return 现在我想应用一个关闭窗口的代码(按X键),但在论坛或帮助中找不到任何解决方案。 例如: #如果此处有应用程序类的活动ahk类名称( OnWindowClose `Do_something` 希望能解释清楚。 谢谢 下面的代码应

我使用一个本地应用程序,当按下ESCAPE时,当前窗口关闭。我想避免这种情况,所以我制作了一个带有自动热键的脚本,可以检测应用程序中窗口的名称,如果我按ESCAPE,窗口不会关闭(除非我按X)

#如果此处有应用程序类的活动ahk类名称(

Escape::return

现在我想应用一个关闭窗口的代码(按X键),但在论坛或帮助中找不到任何解决方案。 例如:

#如果此处有应用程序类的活动ahk类名称(

OnWindowClose

  `Do_something`
希望能解释清楚。
谢谢

下面的代码应该满足您的要求:

;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
;在记事本(Windows 7)上测试
;注意:如果启用aero模式,可能无法正常工作
;注意:如果您单击的记事本窗口*不是*活动窗口,它将被关闭
;注意:某些程序不返回关闭按钮的标准NCHITTEST值,
;解决方法是将光标位置与窗口坐标进行比较
#IfWinActive,ahk_类记事本
按钮::
WinGet,hWnd,ID,A
WingtClass,vWinClass,ahk_id%hWnd%
如果vWinClass不在记事本中
返回
协作模式、鼠标、屏幕
鼠标操作系统,vPosX,vPosY,hWnd2
如果(hWnd=hWnd2)
{
SendMessage,0x84,0,vPosX |(vPosY