当目标 window 未激活或最小化时隐藏 GUI
Hide GUI when target window not active or minimized
我的代码执行以下操作:
当 google.com window 激活时,它会显示一个简单的 GUI。
然后,如果您切换到另一个选项卡,GUI 将被隐藏。[1]
[1]。请注意,它也适用于在另一个浏览器中打开的选项卡 window。例如,您可能打开了 2 个浏览器 windows - 第一个 window 使用 google.com
和 apple.com
,第二个 window 使用 amazon.com
。您可以单击 apple.com
选项卡以及 amazon.com
选项卡,在这两种情况下,GUI 都将被隐藏。然后,如果您单击 google.com
选项卡,将再次显示 GUI。
问题是,当 window(包含 google.com
选项卡)最小化时,我不知道如何强制隐藏 GUI。 (而且,我无法摆脱还有另一个错误的感觉,这些错误在测试时不可见,但实际上存在)。
综上所述,我想要的是:
As soon as a non-google window or tab is clicked/activated (excluding
the AHK Gui) the Gui should be hidden and as soon as any
google-window/tab is clicked/activated the Gui should be shown.
这是我的代码:
#Persistent
SetTimer, Show_Gui, 300
Return
Show_Gui:
IfWinNotActive, foo
IfWinNotActive, Google
{
Gui, Destroy
Return
}
; Otherwise:
SetTimer, Show_Gui, Off
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, Show,, foo
WinWaitNotActive, foo
WinWaitNotActive, Google
SetTimer, Show_Gui, On
Return
Test1:
; do something
Return
Test2:
; do something
Return
编辑。这是 Forivin post 评论中讨论的代码。这不是实际问题的一部分。
#Persistent
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
GroupAdd, myGroup, Google
GroupAdd, myGroup, foo
WinWaitActive, Google
Gui, Show,, foo
SetTimer, Gui_Visibility_Handler, 300
Return
Gui_Visibility_Handler:
WinGet, googleWinState, MinMax, Google
If (googleWinState = -1 || !WinExist("Google")) ;if window minimized or not existent
{
Gui, Hide
If (googleWinState = -1) ;workaround for a bug where windows thinks
Send, !{Esc} ;that the minimized window is active
WinWaitActive, Google
Gui, Show
Return
}
IfWinActive, Google
{
Gui, Show
Return
}
IfWinNotActive, ahk_group MyGroup
{
Gui, Hide
Return
}
Return
Test1:
;do something
Return
Test2:
;do something
Return
编辑:
这是评论中讨论的新代码:
#Persistent
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, +hwndAhkGui
lastActive := WinExist("A")
SetTimer, Active_Window_Memory
SetTimer, Gui_Visibility_Handler, 300
Return
Gui_Visibility_Handler:
WinGet, googleWinState, MinMax, Google
If (googleWinState != -1 && IsGoogleWindow(currentActive)) ;if google is not minimized and: (google is active or: (ahk gui is active and google was active before that))
{
Gui, Show
}
Else If (currentActive = AhkGui && IsGoogleWindow(lastActive))
{
;No need to do anything
}
Else
{
Gui, Hide
}
Return
IsGoogleWindow(hWnd) {
WinGetTitle, title, ahk_id %hWnd%
If (InStr(title, " - Google Chrome"))
{
title := RegExReplace(title, " - Google Chrome$", "") ;remove the " - Google Chrome" from the title
}
If (InStr(title, "Google"))
{
Return True
}
Return False
}
Active_Window_Memory:
currentActive := WinExist("A")
WinWaitNotActive, ahk_id %currentActive%
lastActive := currentActive
Return
Test1:
; do something
Return
Test2:
; do something
Return
#Persistent
SetTitleMatchMode, 2
GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe chrome.exe
GroupAdd, Amazon_Apple_Group, Apple - Mozilla Firefox ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Apple - Google Chrome ahk_exe chrome.exe
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
SetTimer, Show_Gui, 300
Return
Show_Gui:
IfWinActive, ahk_group Amazon_Apple_Group
{
SetTimer, Show_Gui, Off
Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On
}
Return
Test1:
Gui, Submit
; do sth
Return
Test2:
Gui, Submit
; do sth
return
https://autohotkey.com/docs/commands/GroupAdd.htm
"If a timer is disabled while its subroutine is currently running, that subroutine will continue until it completes."
https://autohotkey.com/docs/commands/SetTimer.htm#Remarks
"SetTimer, Show_Gui, Off" 关闭定时器直到下面的子程序
Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On
完成。
子程序完成后自动开启定时器。
我的代码执行以下操作:
当 google.com window 激活时,它会显示一个简单的 GUI。
然后,如果您切换到另一个选项卡,GUI 将被隐藏。[1]
[1]。请注意,它也适用于在另一个浏览器中打开的选项卡 window。例如,您可能打开了 2 个浏览器 windows - 第一个 window 使用 google.com
和 apple.com
,第二个 window 使用 amazon.com
。您可以单击 apple.com
选项卡以及 amazon.com
选项卡,在这两种情况下,GUI 都将被隐藏。然后,如果您单击 google.com
选项卡,将再次显示 GUI。
问题是,当 window(包含 google.com
选项卡)最小化时,我不知道如何强制隐藏 GUI。 (而且,我无法摆脱还有另一个错误的感觉,这些错误在测试时不可见,但实际上存在)。
综上所述,我想要的是:
As soon as a non-google window or tab is clicked/activated (excluding the AHK Gui) the Gui should be hidden and as soon as any google-window/tab is clicked/activated the Gui should be shown.
这是我的代码:
#Persistent
SetTimer, Show_Gui, 300
Return
Show_Gui:
IfWinNotActive, foo
IfWinNotActive, Google
{
Gui, Destroy
Return
}
; Otherwise:
SetTimer, Show_Gui, Off
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, Show,, foo
WinWaitNotActive, foo
WinWaitNotActive, Google
SetTimer, Show_Gui, On
Return
Test1:
; do something
Return
Test2:
; do something
Return
编辑。这是 Forivin post 评论中讨论的代码。这不是实际问题的一部分。
#Persistent
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
GroupAdd, myGroup, Google
GroupAdd, myGroup, foo
WinWaitActive, Google
Gui, Show,, foo
SetTimer, Gui_Visibility_Handler, 300
Return
Gui_Visibility_Handler:
WinGet, googleWinState, MinMax, Google
If (googleWinState = -1 || !WinExist("Google")) ;if window minimized or not existent
{
Gui, Hide
If (googleWinState = -1) ;workaround for a bug where windows thinks
Send, !{Esc} ;that the minimized window is active
WinWaitActive, Google
Gui, Show
Return
}
IfWinActive, Google
{
Gui, Show
Return
}
IfWinNotActive, ahk_group MyGroup
{
Gui, Hide
Return
}
Return
Test1:
;do something
Return
Test2:
;do something
Return
编辑: 这是评论中讨论的新代码:
#Persistent
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
Gui, +hwndAhkGui
lastActive := WinExist("A")
SetTimer, Active_Window_Memory
SetTimer, Gui_Visibility_Handler, 300
Return
Gui_Visibility_Handler:
WinGet, googleWinState, MinMax, Google
If (googleWinState != -1 && IsGoogleWindow(currentActive)) ;if google is not minimized and: (google is active or: (ahk gui is active and google was active before that))
{
Gui, Show
}
Else If (currentActive = AhkGui && IsGoogleWindow(lastActive))
{
;No need to do anything
}
Else
{
Gui, Hide
}
Return
IsGoogleWindow(hWnd) {
WinGetTitle, title, ahk_id %hWnd%
If (InStr(title, " - Google Chrome"))
{
title := RegExReplace(title, " - Google Chrome$", "") ;remove the " - Google Chrome" from the title
}
If (InStr(title, "Google"))
{
Return True
}
Return False
}
Active_Window_Memory:
currentActive := WinExist("A")
WinWaitNotActive, ahk_id %currentActive%
lastActive := currentActive
Return
Test1:
; do something
Return
Test2:
; do something
Return
#Persistent
SetTitleMatchMode, 2
GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Amazon.com ahk_exe chrome.exe
GroupAdd, Amazon_Apple_Group, Apple - Mozilla Firefox ahk_exe firefox.exe
GroupAdd, Amazon_Apple_Group, Apple - Google Chrome ahk_exe chrome.exe
Gui, Add, Button, w200 h25 gTest1, button 1
Gui, Add, Button, w200 h25 gTest2, button 2
SetTimer, Show_Gui, 300
Return
Show_Gui:
IfWinActive, ahk_group Amazon_Apple_Group
{
SetTimer, Show_Gui, Off
Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On
}
Return
Test1:
Gui, Submit
; do sth
Return
Test2:
Gui, Submit
; do sth
return
https://autohotkey.com/docs/commands/GroupAdd.htm
"If a timer is disabled while its subroutine is currently running, that subroutine will continue until it completes." https://autohotkey.com/docs/commands/SetTimer.htm#Remarks
"SetTimer, Show_Gui, Off" 关闭定时器直到下面的子程序
Gui, Show, NoActivate, foo
WinSet, AlwaysOnTop, On, foo ahk_class AutoHotkeyGUI
WinWaitNotActive, ahk_group Amazon_Apple_Group
Sleep, 50
Gui, Cancel
SetTimer, Show_Gui, On
完成。
子程序完成后自动开启定时器。