【问题标题】:AutoHotKey: Run code on Window Event (Close)AutoHotKey:在窗口事件上运行代码(关闭)
【发布时间】:2016-12-31 09:42:58
【问题描述】:

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

#IfWinActive ahk_class name_of_app_class_here

Escape::return

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

#IfWinActive ahk_class name_of_app_class_here

OnWindowClose

  `Do_something`

希望得到很好的解释。 谢谢!

【问题讨论】:

    标签: events


    【解决方案1】:

    下面的代码应该做你想要实现的:

    ;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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-27
      • 2012-07-29
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多