目标窗口未激活或最小化时隐藏GUI

时间:2016-01-06 21:32:05

标签: autohotkey

我的代码执行以下操作:

  • 当google.com窗口变为活动状态时,它会显示一个简单的GUI。

  • 然后,如果切换到另一个选项卡,GUI将被隐藏。[1]

[1]。请注意,它对于在另一个浏览器窗口中打开的选项卡也有效。例如,您可能打开了2个浏览器窗口 - 第一个窗口显示google.comapple.com,第二个窗口显示amazon.com。您可以单击apple.com选项卡以及amazon.com选项卡,在这两种情况下都会隐藏GUI。然后,如果单击google.com选项卡,将再次显示GUI。

问题是,当窗口(包含google.com选项卡)最小化时,我无法弄清楚如何强制隐藏GUI。 (而且,我无法摆脱感觉还有其他错误,这些错误在测试时不可见,但确实存在)。

总结一下,我想要的是:

  

点击/激活非谷歌窗口或标签后(不包括在内)   AHK Gui)Gui应该尽快隐藏   单击/激活google-window / tab,应显示Gui。

这是我的代码:

#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帖子的评论中讨论的代码。这不是实际问题的一部分。

#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

2 个答案:

答案 0 :(得分:2)

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  

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

"如果定时器在其子程序当前正在运行时被禁用,则该子程序将继续运行直至完成。" https://autohotkey.com/docs/commands/SetTimer.htm#Remarks

" SetTimer,Show_Gui,Off"关闭定时器直到以下子程序

new ObjectMapper.readValue("\"name\": ...", Map.class);

完成。

子程序在完成后自动打开定时器。

答案 1 :(得分:1)

修改: 以下是评论中讨论的新代码:

#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