通过Hotkey最小化/恢复Gui的问题

问题描述 投票:0回答:2

我正在为我的工作区开发一个基于Gui的工具,以提供简单的文本粘贴和WinMinimize / WinRestore功能。我面临的问题是最小化/恢复切换功能。

我正在使用的当前代码详述如下:

;                               Functions
; }-----------------------------------------------------------------------{
GuiToggle()
{
    Sleep, 50
    if WinActive("ServiceDeskAssist")
    {
        Gui, -AlwaysOnTop
        WinMinimize, ServiceDeskAssist
        WinWaitNotActive, ServiceDeskAssist
    }
    else
    {
        WinRestore, ServiceDeskAssist
        WinActivate, ServiceDeskAssist
        WinWaitActive, ServiceDeskAssist
        Gui, +AlwaysOnTop
    }
Sleep, 50
}

;                                Hotkeys
; }-----------------------------------------------------------------------{
^z::GuiToggle()

根据Gui的当前状态,想法是切换最小化/恢复,但是如果Gui存在但仍然可见(即+ AlwaysOnTop)它不是“活动”,因此它将被恢复而不是最小化。有没有办法纠正这个功能?

autohotkey
2个回答
0
投票

除非ServiceDeskAssist是超全局变量,否则if WinActive(ServiceDeskAssist)是错误的。 WinMinimize, ServiceDeskAssist使用ServiceDeskAssist作为文字,而不是字符串(这是我认为你打算为IF语句做的) WinRestore不会自动激活

;                               Functions
; }-----------------------------------------------------------------------{
GuiToggle()
{
    Sleep, 50
    if WinActive("ServiceDeskAssist")
    {
        Gui, -AlwaysOnTop
        WinMinimize, ServiceDeskAssist
        WinWaitNotActive, ServiceDeskAssist
    }
    else
    {
        WinRestore, ServiceDeskAssist
        WinActivate, ServiceDeskAssist
        WinWaitActive, ServiceDeskAssist
        Gui, +AlwaysOnTop
    }
    Sleep, 50
}
;                                Hotkeys
; }-----------------------------------------------------------------------{
^z::GuiToggle()

另外,为什么不将窗口的标题传递给函数?这样它就可以重复使用了

;                               Functions
; }-----------------------------------------------------------------------{
GuiToggle(winTitle)
{
    Sleep, 50
    if WinActive(winTitle)
    {
        Gui, -AlwaysOnTop
        WinMinimize, % winTitle
        WinWaitNotActive, % winTitle
    }
    else
    {
        WinRestore, % winTitle
        WinActivate, % winTitle
        WinWaitActive, % winTitle
        Gui, +AlwaysOnTop
    }
    Sleep, 50
}
;                                Hotkeys
; }-----------------------------------------------------------------------{
^z::GuiToggle("ahk_class Notepad")

0
投票

好吧,所以看起来WinGet - MinMax是前进的方向。以下代码正在顺利运行:

;                               Functions
; }-----------------------------------------------------------------------{
GuiToggle()
{
    Sleep, 50
    WinGet, WinState, MinMax, ServiceDeskAssist
    If(WinState==0||WinState==1)
    {
        Gui, -AlwaysOnTop
        WinMinimize, ServiceDeskAssist
        WinWaitNotActive, ServiceDeskAssist
    }
    else if(WinState==-1)
    {
        WinRestore, ServiceDeskAssist
        WinActivate, ServiceDeskAssist
        WinWaitActive, ServiceDeskAssist
        Gui, +AlwaysOnTop
    }
Sleep, 50
}

Toggle(a)
{
    Sleep, 50
    WinGet, WinState, MinMax, % a
    If(WinState==0||WinState==1)
    {
        WinMinimize, % a
        WinWaitNotActive, % a
    }
    else if(WinState==-1)
    {
        WinRestore, % a
        WinActivate, % a
        WinWaitActive, % a
    }
Sleep, 50
}

;                                Hotkeys
; }-----------------------------------------------------------------------{

^x::ExitApp

^z::GuiToggle()
© www.soinside.com 2019 - 2024. All rights reserved.