Autohotkey - How to link a GUI to a window to act the same as its parent

痴心易碎 提交于 2019-12-12 03:45:22

问题


I would like to link a GUI to a certain window, so it could act like it's a part of it.

This is my GUI and I would like it to follow the Calculator (for testing). If the calculator is minimized, the gui would be minimized as well.

Thanks in advance!

#SingleInstance Force
#Persistent

BC = 0

Gui, Color, EEAA99
Gui, Margin , 0, 0
GUI, +AlwaysOnTop -Border -SysMenu -Caption +ToolWindow +Owner
Gui, Font, S48 CDefault Bold CBlue, Verdana
Gui, Add, Text, BackgroundTrans , Units completed:
Gui, Font, S72 CDefault Bold CGreen, Verdana
Gui, Add, Text, BackgroundTrans vBuildCounter, %BC%
WinSet, TransColor, EEAA99
Gui +LastFound +AlwaysOnTop +ToolWindow
WinSet, TransColor, EEAA99
Gui -Caption
Gui, Show, % "x" A_ScreenWidth - 400 " y" A_ScreenHeight / 4

:?*:asd:: ;count up
    SoundBeep, 500,500
    BC := BC += 1
    GuiControl,, BuildCounter, %BC%
Return

:?*:qwe:: ;reset the counter
    SoundBeep, 500,500
    BC := 0
    GuiControl,, BuildCounter, %BC%
Return

Esc::
ExitApp

回答1:


I ended up with two scripts. Maybe this can be combined later.

One script is for the ToolMenu, the second for the activation. Since I could not control the GUI, Show/Hide from the Activation script, I "solved" it by using Ctrl+Alt+Win+F1 and Ctrl+Alt+Win+F2.Not the most elegant way, but it works...

ToolMenu.ahk

#SingleInstance Force
#installKeybdHook
#Persistent

Gui, Destroy
Gui,+AlwaysOnTop
Gui,+ToolWindow
Gui,+Border
Gui, Add, Button, y5 w60, &LowBeep
Gui, Add, Button, y5 w60, &HighBeep
Gui, Add, Button, y8 h18, X
Gui, Show, y0, MyToolWindow
Return

ButtonLowBeep:
    SoundBeep, 300, 300
Return

ButtonHighBeep:
    SoundBeep, 500, 300
Return

ButtonX:
ButtonCancel:
    Gui, Destroy
ExitApp

^!#F1::
    Gui, Hide
Return

^!#F2::
    Gui, Show, y0, MyToolWindow
Return

DetectWindowChange.ahk

#SingleInstance
#installKeybdHook
#Persistent
Global SwitchCounter

Gui +LastFound 
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam )
{
    If (wParam = 4)
    { 
        WinGetTitle, CurrName, A
        If (CurrName = "Calculator" OR CurrName = "MyToolWindow")
        {
            If ( SwitchCounter = 0)
            {
                ;WinRestore, MyToolWindow
                Send, ^!#{F2} ; Send Ctrl+Alt+Win+F2 to trigger GUI Show in GUI script
            }
            SwitchCounter += 1
        }
        Else
        {
            If ( SwitchCounter > 0)
            {
                ;WinMinimize, MyToolWindow
                Send, ^!#{F1} ; Send Ctrl+Alt+Win+F1 to trigger GUI Hide in GUI script
            }
            SwitchCounter := 0
        }
    }
}
Return

Let me know how this works...




回答2:


You can (as far as I know) only do this with a settimer.

Pseudo code, not tested!

SetTitleMatchMode := 2
SetTimer, CheckWindow, 200

CheckWindow:
    If WinActive("Calculator")
    {
       Gui, Show, % "x" A_ScreenWidth - 400 " y" A_ScreenHeight / 4, Popup
    }
    Else If !WinActive("Popup")
    {
        Gui, Hide
    }
Return

Edit: Added a condition to avoid hiding the popup if it is activated.



来源:https://stackoverflow.com/questions/15179134/autohotkey-how-to-link-a-gui-to-a-window-to-act-the-same-as-its-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!