How to reuse Gui window?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

How to reuse Gui window?

Post by slishnevsky » 04 Apr 2024, 13:35

Hello.

I have this simple function that creates a Gui window (borderless blue window) that stays visible for 1 second:

Code: Select all

ShowMessageBox(message, stay := 0) {
  MyGui := Gui('-ToolWindow +Border -Caption +AlwaysOnTop')
  MyGui.BackColor := '007ACC'
  MyGui.SetFont('s30 cWhite', 'Roboto')
  MyGui.AddText('Center', message)
  MyGui.Show()
  if (stay = 0)
    SetTimer(() => MyGui.Hide(), 1000)
}
When I call this function from other places, AHK creates a separate AHK window for each call.
I would like to reuse this window, just replace the text that appears in the window.
What is the correct way to do this?

Thank you.

RussF
Posts: 1296
Joined: 05 Aug 2021, 06:36

Re: How to reuse Gui window?

Post by RussF » 04 Apr 2024, 13:52

How about this:

Code: Select all

#Requires AutoHotkey v2.0+

MyGui := Gui('-ToolWindow +Border -Caption +AlwaysOnTop')
MyGui.BackColor := '007ACC'
MyGui.SetFont('s30 cWhite', 'Roboto')
MyText := MyGui.Add('text','Center', '                              ')  ;spaces to accommodate your largest message
Return

F8::
{
    ShowMessageBox('This is a test',3)
}

f10::Exitapp

ShowMessageBox(message, stay := 0) {
    MyText.Value := message
    MyGui.Show()
    SetTimer(() => MyGui.Hide(), stay = 0 ? -1000 : stay * -1000)
  }

Russ

Edit: Changed duration to accommodate lack of argument and to cause timer to fire only once

slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: How to reuse Gui window?

Post by slishnevsky » 04 Apr 2024, 17:13

You didn't understand.
Each time I call ShowMessageBox() it will create a new instance.
Take a look, this is a countdown timer:
image.png
image.png (1.58 MiB) Viewed 239 times

niCode
Posts: 309
Joined: 17 Oct 2022, 22:09

Re: How to reuse Gui window?

Post by niCode » 04 Apr 2024, 22:00

Are you just looking for this?

Code: Select all

static MyGui := Gui('-ToolWindow +Border -Caption +AlwaysOnTop')

Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to reuse Gui window?

Post by Rohwedder » 05 Apr 2024, 04:09

Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0
q::ShowMessageBox(A_TickCount)
w::ShowMessageBox(A_TickCount, 1)
ShowMessageBox(message, stay := 0) {
	Static MyGui := 0
	IF !MyGui {
		MyGui := Gui('-ToolWindow +Border -Caption +AlwaysOnTop')
		MyGui.BackColor := '007ACC'
		MyGui.SetFont('s30 cWhite', 'Roboto')
		MyGui.AddText('Center vMText', message)
		MyGui.Show()
	} Else MyGui['MText'].Value := message, MyGui.Show()
	SetTimer () => MyGui.Hide(), -1000 * !stay
}

RussF
Posts: 1296
Joined: 05 Aug 2021, 06:36

Re: How to reuse Gui window?

Post by RussF » 05 Apr 2024, 06:06

slishnevsky wrote:You didn't understand.
No, I gave you exactly what you asked for.
slishnevsky wrote: I would like to reuse this window, just replace the text that appears in the window.
Which is what my code does. It only creates one instance of the GUI before you ever call your function. Calling the function only shows the GUI with the text you pass to the message argument, for the amount of time you pass (in seconds) to the stay argument. If you leave stay blank, the GUI will show for a default of 1 second. The timer is passed a negative value so that it is only called once per call of the function.

You can see by the following modification, I call the function in a loop 10 times, giving stay a value of 5 seconds. However, it sleeps for only 1 second before calling it again. The new text is displayed in the SAME GUI and the timer is reset. It only disappears after the last call and the timer actually runs out at 5 seconds.

Code: Select all

#Requires AutoHotkey v2.0+

MyGui := Gui('-ToolWindow +Border -Caption +AlwaysOnTop')
MyGui.BackColor := '007ACC'
MyGui.SetFont('s30 cWhite', 'Roboto')
MyText := MyGui.Add('text','Center', '                              ')  ;spaces to accommodate your largest message
Return

F8::
{
    Loop 10 {
      ShowMessageBox('This is test ' A_Index,5)
      Sleep(1000)
    }
}

f10::Exitapp

ShowMessageBox(message, stay := 0) {
    MyText.Value := message
    MyGui.Show()
    SetTimer(() => MyGui.Hide(), stay = 0 ? -1000 : stay * -1000)
  }
Russ

Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to reuse Gui window?

Post by Rohwedder » 05 Apr 2024, 07:40

@slishnevsky
Do you mean with
When I call this function from other places
other scripts that contain a copy of this function?

slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: How to reuse Gui window?

Post by slishnevsky » 05 Apr 2024, 08:12

Rohwedder wrote:
05 Apr 2024, 07:40
@slishnevsky
Do you mean with
When I call this function from other places
other scripts that contain a copy of this function?
What? No. One script file, other places in code

Post Reply

Return to “Ask for Help (v2)”