我創建了以下腳本,試圖使用一個熱鍵來啟動計算器,或者,如果它已經打開,則“激活”窗口,或者如果它已經激活,則關閉計算器。一切正常,如果計算器最小化,它無法恢復它。據我所知,我所做的一切都是正確的。我不明白問題是什麼。 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
但這個解決方案要求我經常按熱鍵兩次。一次使其處於活動狀態(如果它不是最頂層,即使它仍然可見),然後再次將其最小化。希望這對某人有幫助。