Page 1 of 1

How to reuse Gui window?

Posted: 04 Apr 2024, 13:35
by slishnevsky
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.

Re: How to reuse Gui window?

Posted: 04 Apr 2024, 13:52
by RussF
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

Re: How to reuse Gui window?

Posted: 04 Apr 2024, 17:13
by slishnevsky
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 264 times

Re: How to reuse Gui window?

Posted: 04 Apr 2024, 22:00
by niCode
Are you just looking for this?

Code: Select all

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

Re: How to reuse Gui window?

Posted: 05 Apr 2024, 04:09
by Rohwedder
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
}

Re: How to reuse Gui window?

Posted: 05 Apr 2024, 06:06
by RussF
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

Re: How to reuse Gui window?

Posted: 05 Apr 2024, 07:40
by Rohwedder
@slishnevsky
Do you mean with
When I call this function from other places
other scripts that contain a copy of this function?

Re: How to reuse Gui window?

Posted: 05 Apr 2024, 08:12
by slishnevsky
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