0

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
Ismo
  • 47
  • 1
  • 3
  • 7

2 Answers2

1

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.

DBolton
  • 506
  • 4
  • 9
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • I'm using AutoHotKey_L. I've also seen something like Set_parent_by_title but I couldn't make it work. :/ – Ismo Mar 02 '13 at 21:13
  • Just changed the minimize to GUI, Hide or GUI, Show – Robert Ilbrink Mar 02 '13 at 21:42
  • The problem is when it runs the GUI show command, it takes away the focus of the calculator and activates the GUI. This hides the GUI, ask Calculator isn't active. Then Calculator becomes active which shows the GUI again... And it's switching between the two windows all the time. – Ismo Mar 02 '13 at 22:04
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...

Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • Interesting :) It does work, I'm gonna play with it tomorrow. Thanks again! :) – Ismo Mar 03 '13 at 19:47
  • Please provide some feedback on the proposed solution and if the answer was helpful, then please "Accept" that answer by clicking on the white check-mark to turn it green. Thank you! – Robert Ilbrink Mar 06 '13 at 11:43
  • Please "Accept" the given answer by clicking on the WHITE "Check-mark", next to that answer, to turn it GREEN. Thank you! See: [Accepting answers](http://meta.stackexchange.com/a/5235/210367) – Robert Ilbrink Mar 09 '13 at 19:10