【问题标题】:How to minimize a window by closing it?如何通过关闭窗口来最小化它?
【发布时间】:2018-11-21 21:25:26
【问题描述】:

我想让一个窗口仅在启动任何关闭操作时最小化(单击关闭按钮,Alt+F4)。对于 AutoHotKey,我认为需要 WinMinimize,但我不知道如何检测关闭事件。如果您知道 PowerShell 的解决方案,也请分享。


相关:Is it possible to catch the close button and minimize the window instead?

【问题讨论】:

  • 为什么要标记这个问题PowerShell
  • 我也可以接受 PowerShell 解决方案。这可以接受吗?

标签: powershell autohotkey


【解决方案1】:
#NoEnv
#SingleInstance Force

; Add the ahk_class of the windows you want  minimize by clicking on the close button in this array:
Classes := "Notepad,CabinetWClass,IEFrame" ; ...
Loop, parse, Classes, `,
    GroupAdd, GroupName, ahk_class %A_LoopField%

SetTimer CheckMouse, -300
return

CheckMouse:
    CoordMode, Mouse, Screen
    MouseGetPos, mX, mY, WindowUnderMouse
    WinGetPos, wX, wY, wW, wH, ahk_id %WindowUnderMouse%
        CloseButton := (mY > wY and mY < wY+50 and (mX > wX + (wW-50) and mX < wX+wW))
    SetTimer CheckMouse, -300
return

#If (CloseButton)

    ~LButton::
        MouseGetPos,,, WindowUnderMouse
        WinGetClass, Class, ahk_id %WindowUnderMouse%
        If Class in %Classes%
        {
            WinGet, id, ID, ahk_id %WindowUnderMouse%
            DISABLE_CloseButton(id)
            WinMinimize, ahk_id %WindowUnderMouse%
        }
    return


#If WinActive("ahk_group GroupName")

    !F4:: WinMinimize, A

#If ; turn off context sensitivity


DISABLE_CloseButton(id){ ;By RealityRipple at http://www.xtremevbtalk.com/archive/index.php/t-258725.html   
  menu:=DllCall("user32\GetSystemMenu","UInt",id,"UInt",0)
  DllCall("user32\DeleteMenu","UInt",menu,"UInt",0xF060,"UInt",0x0)
  WinGetPos,x,y,w,h,ahk_id %id%
  WinMove,ahk_id %id%,,%x%,%y%,%w%,% h-1
  WinMove,ahk_id %id%,,%x%,%y%,%w%,% h+1
}

ENABLE_CloseButton(id){ ;By Mosaic1 at http://www.xtremevbtalk.com/archive/index.php/t-258725.html  
  menu:=DllCall("user32\GetSystemMenu","UInt",id,"UInt",1)
  DllCall("user32\DrawMenuBar","UInt",id)
}

https://autohotkey.com/docs/commands/_If.htm

【讨论】:

    【解决方案2】:

    此 AHK 脚本禁用记事本中的关闭按钮并通过单击此按钮最小化记事本:

    #NoEnv
    #SingleInstance Force
    
    SetTimer CheckMouse, -300
    return
    
    CheckMouse:
        CoordMode, Mouse, Screen
        WinGet, id, ID, ahk_class Notepad
            DISABLE_CloseButton(id)
        MouseGetPos, mX, mY, WindowUnderMouse
        WinGetPos, wX, wY, wW, wH, ahk_id %WindowUnderMouse%
            CloseButton := (mY > wY and mY < wY+50 and (mX > wX + (wW-50) and mX < wX+wW))
        SetTimer CheckMouse, -300
    return
    
    #If (CloseButton)
    
        ~LButton Up::
            MouseGetPos,,, WindowUnderMouse
            WinGetClass, Class, ahk_id %WindowUnderMouse%
            If (Class="Notepad")
                WinMinimize, ahk_id %WindowUnderMouse%
        return
    
    #If WinActive("ahk_class Notepad")
    
        !F4:: WinMinimize, A
    
    #If
    
    
    DISABLE_CloseButton(id){ ;By RealityRipple at http://www.xtremevbtalk.com/archive/index.php/t-258725.html   
      menu:=DllCall("user32\GetSystemMenu","UInt",id,"UInt",0)
      DllCall("user32\DeleteMenu","UInt",menu,"UInt",0xF060,"UInt",0x0)
      WinGetPos,x,y,w,h,ahk_id %id%
      WinMove,ahk_id %id%,,%x%,%y%,%w%,% h-1
      WinMove,ahk_id %id%,,%x%,%y%,%w%,% h+1
    }
    
    ENABLE_CloseButton(id){ ;By Mosaic1 at http://www.xtremevbtalk.com/archive/index.php/t-258725.html  
      menu:=DllCall("user32\GetSystemMenu","UInt",id,"UInt",1)
      DllCall("user32\DrawMenuBar","UInt",id)
    }
    

    【讨论】:

    • 完美,谢谢。如果我有一套程序怎么办?我们可以在脚本顶部声明它们吗?
    • 尝试下一个答案。
    • 嗯,您为什么不直接编辑旧答案而不是发布新答案?
    • 可能会使其他只需要一个应用程序的解决方案的用户感到困惑。
    • 但他们只需在Classes 中添加一个应用程序?
    猜你喜欢
    • 2013-10-09
    • 2020-06-26
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多