我创建了以下脚本,试图让一个热键启动计算器,或者,如果计算器已经打开,则“激活”窗口,或者,如果计算器已经激活,则关闭计算器。一切正常,但如果计算器最小化,则无法恢复。据我所知,我做的一切都是正确的。我不明白问题出在哪里。AHK 文档声称,如果您在最小化的窗口上调用 WinActivate,它将首先恢复该窗口,但这是谎言。如果我取消注释 MsgBox 行,窗口最小化时我仍然会收到一条消息,但它无能为力。
If WinExist("Calculator") {
;MsgBox Calculator Exists.
IfWinActive
WinKill
Else
WinGet, winState, MinMax
If (winState = -1)
WinRestore, Calculator
WinActivate, Calculator
}
Else {
run calc
WinActivate, Calculator
}
答案1
你使用的是哪种操作系统?如果我将 ahk_class 添加到标题中,你的代码在 Win10 上对我有效:
If WinExist("Calculator ahk_class ApplicationFrameWindow")
{
;MsgBox Calculator Exists.
IfWinActive
WinClose
Else
{
WinGet, winState, MinMax
If (winState = -1)
{
WinRestore
WinActivate
}
}
}
Else
{
run calc
WinWait, Calculator
WinActivate
}
答案2
这就是我必须为我做的事情。
;------hotkey to open/close explorer------
^LWin:: ;control and leftwindows
if WinExist("ahk_class ActualTools_TabbedExplorerContainerWindow"){ ; if the window exists
if WinActive("ahk_class ActualTools_TabbedExplorerContainerWindow") or WinActive("ahk_exe Explorer.EXE")
WinMinimize, ahk_class ActualTools_TabbedExplorerContainerWindow
else{
WinActivate ; otherwise make it the active window
}
}else
run, explorer.exe ;otherwise not open, open explorer
return
我习惯使用:
;------hotkey to open/close explorer------
^LWin:: ;control and leftwindows
;WinGetClass, Clipboard, A ;Use this to get the name(class?) of the window you want the script to open. https://stackoverflow.com/questions/45642727/what-is-ahk-class-how-can-i-use-it-for-window-matching
if WinExist("ahk_class ActualTools_TabbedExplorerContainerWindow"){ ; if the window exists
WinGet, state, MinMax ;get the state of the window. is it maximized or minimized. this could be part of the issue
If state >= 0 ; if its not minimized, minimize it
WinMinimize
else
WinActivate ; otherwise make it the active window
}else
Run, Explorer.exe ;otherwise not open, open explorer
return
但这个解决方案要求我经常按两次热键。一次激活它(如果它不在最顶部,它仍然可见),然后再次最小化它。希望这对某些人有帮助。