How to fade out a Gui with function + variable?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

How to fade out a Gui with function + variable?

15 Sep 2018, 15:16

Hello,

I created a script which consists of a Gui with a picture inside of it.
Additionally, there is also a fade-in and a fade-out effect which works perfectly.

I use this Gui in scripts which take some seconds until they have finished.
(Just to inform the user that a script is being run.)

By using the include function, I add my Gui script into any other script.
It looks like this:

Code: Select all

#SingleInstance force
;Info-Meldungen
global d = 5 ; Verzögerung in ms
global bild ; Entweder Start, Ende oder Abbruch (Bild wird in der Gui angezeigt)

Start()
{
  bild = Start.png
  Gui_mit_Effekt()
}

Ende()
{
  bild = Ende.png
  SoundPlay, %A_WinDir%\Media\tada.wav
  Sleep, d
  Gui_mit_Effekt()
}

Gui_mit_Effekt()
{
  Gui +E0x20 -Caption +LastFound +ToolWindow +AlwaysOnTop ; Durchklickbar, rahmenlos, zuletzt gefunden, kein Taskleistensymbol, immer im Vordergrund
  Gui, Add, Picture, x0 y0 w500 h250, %A_ScriptDir%\pics\Info-Meldungen\%bild% ; Vorgang erfolgreich-Bild
  Gui, Show, w500 h250 hide, Ende
  fade := 1 ; Variable für Einblend bzw. Ausblend-Effekt
  Loop 25
  {
    fade := fade + 9 ; Einblend-Effekt
    WinSet, Transparent, %fade% ; Pinke Farbe komplett rausfiltern, den Rest einblenden  0xFC62FD 
    if fade = 10 ; Beim ersten Durchgang:
    {
      WinShow, Ende ; Gui anzeigen
    }
    Sleep, 10
  }
  Sleep, 800
  Loop 25
  {
    fade := fade - 9 ; Ausblend-Effekt
    WinSet, Transparent, %fade% ; Pinke Farbe komplett rausfiltern, den Rest einblenden  0xFC62FD 
    if fade = 217 ; Beim ersten Durchgang:
    {
      WinShow, Ende ; Gui anzeigen
    }
    Sleep, 10
  }
}
I call it from another script by this way:

Code: Select all

#SingleInstance force
;Info-Meldungen-Test
#include Info-Meldungen.ahk
global d = 5 ; Verzögerung in ms

F1::
Start()
return

F2::
Ende()
return
When I press F1, the Start image appears but starts disappearing after 800 ms, which is logical.
I would like that it stays until it gets the information/command to disappear.

I think I will need to include a variable which tells the Start() function when to fade out.
Something like this:

Code: Select all

#SingleInstance force
;Info-Meldungen
global d = 5 ; Verzögerung in ms
global bild ; Entweder Start, Ende oder Abbruch (Bild wird in der Gui angezeigt)

Start(mode)
{
  bild = Start.png
  Gui_mit_Effekt(mode)
}

Ende(mode)
{
  bild = Ende.png
  SoundPlay, %A_WinDir%\Media\tada.wav
  Sleep, d
  Gui_mit_Effekt(mode)
}

Gui_mit_Effekt(mode)
{
  Gui +E0x20 -Caption +LastFound +ToolWindow +AlwaysOnTop ; Durchklickbar, rahmenlos, zuletzt gefunden, kein Taskleistensymbol, immer im Vordergrund
  Gui, Add, Picture, x0 y0 w500 h250, %A_ScriptDir%\pics\Info-Meldungen\%bild% ; Vorgang erfolgreich-Bild
  Gui, Show, w500 h250 hide, Ende
  fade := 1 ; Variable für Einblend bzw. Ausblend-Effekt
  if mode = 1
  {
    Loop 25
    {
      fade := fade + 9 ; Einblend-Effekt
      WinSet, Transparent, %fade% ; Pinke Farbe komplett rausfiltern, den Rest einblenden  0xFC62FD 
      if fade = 10 ; Beim ersten Durchgang:
      {
        WinShow, Ende ; Gui anzeigen
      }
      Sleep, 10
    }
  }
  if mode = 0
  {
    fade := 226
    Sleep, 800
    Loop 25
    {
      fade := fade - 9 ; Ausblend-Effekt
      WinSet, Transparent, %fade% ; Pinke Farbe komplett rausfiltern, den Rest einblenden  0xFC62FD 
      if fade = 217 ; Beim ersten Durchgang:
      {
        WinShow, Ende ; Gui anzeigen
      }
      Sleep, 10
    }
  }
}

Code: Select all

#SingleInstance force
;Info-Meldungen-Test
#include Info-Meldungen.ahk
global d = 5 ; Verzögerung in ms

F1::
Start(1)
return

F2::
Start(0)
return
In this case, it stays forever when pressing F1 (or better when Start() gets called with 1.
As soon as it gets called with a 0, it starts disappearing, but it looks weird.
- it disappears immediately without any effect
- then it appears again without any effect
- after this is starts disappearing, but it doesn't look as smooth as the first code.

Maybe it's just a small issue.
I realized that the fade value has to be set to 226 in the 2nd code, otherwise the script "forgets" it.
This is how it looks like:
Image


Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to fade out a Gui with function + variable?

17 Nov 2018, 18:01

the offending line is Gui, Show, w500 h250 hide, Ende, but AnimateWindow already does fading(and more), so there's no need to come up with ur own implementation

Code: Select all

;Info-Meldungen
#SingleInstance force

hwndStart := makeGUI("Start")
hwndEnde := makeGUI("Ende")

makeGUI(bild) {
	Gui +E0x20 -Caption +LastFound +ToolWindow +AlwaysOnTop +Hwndhwnd ; Durchklickbar, rahmenlos, zuletzt gefunden, kein Taskleistensymbol, immer im Vordergrund
	Gui, Add, Picture, x0 y0 w500 h250, %A_ScriptDir%\pics\Info-Meldungen\%bild%.png ; Vorgang erfolgreich-Bild
	Gui, Show, w500 h250 hide

	return hwnd
}

fade(hwnd, fadeIn := 0, duration := 200) {
	DllCall("AnimateWindow", "Ptr", hwnd, "UInt", duration, "UInt", fadeIn ? 0x80000 : 0x80000 | 0x10000) ; AW_BLEND := 0x80000, AW_HIDE := 0x10000
}

Start(mode, duration := 200) {
	global hwndStart
	fade(hwndStart, mode, duration)
}

Ende(mode, d := 5, duration := 200) {
	global hwndEnde

	SoundPlay, %A_WinDir%\Media\tada.wav
	Sleep d
	fade(hwndEnde, mode, duration)
}

;Info-Meldungen-Test
#SingleInstance force
#include Info-Meldungen.ahk

F1::Start(1)
F2::Start(0)
F3::Ende(1)
F4::Ende(0)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 314 guests