Gui in function swap image proble

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Gui in function swap image proble

20 Oct 2013, 02:22

I'm building a GUI inside a function. The images are together in a different directory. RadioNO.png loads. Can anyone suggest why I'm getting a soundbeep but the image is not being swapped and I'm getting no error messages. I've used something similar before but this is not working. Thanks

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Recommended for catching common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.
SetWorkingDir ..\			; Makes the root folder \files

Settings()

return

Settings()
{
	static SoundHint
	Static makeImage = 
		
	Gui, 10:font, s30
	Gui, 10:add, picture, x20 w50 h-1 gCheckSound vTestSound, elements\RadioNO.png
	Gui, 10:add, text, x+12 vSoundHint, Sound
	
	Gui, 10:show, center AutoSize, Current Settings
	Return
	
	CheckSound:

		makeImage := !makeImage
		if makeImage
		GuiControl,10:, TestSound, elements\RadioYES.png
		else
		GuiControl,10:, TestSound, elements\RadioNO.png
		soundbeep
	return

}

10GuiClose:
10Guiescape:
exitapp
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui in function swap image proble

20 Oct 2013, 06:44

Add global TestSound to the function will work. (static TestSound won't work.)
just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Gui in function swap image proble

20 Oct 2013, 08:54

I'd suggest to use the "Gui in a function" thing only for Guis that are kinda modal; i.e. the script may stay within the function because no other processing is needed until this step is completed. This allows included subroutines to access local variabels. So you don't need global Gui vVariables if you use the hwnd option instead:

Code: Select all

#NoEnv         ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn          ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetWorkingDir ..\           ; Makes the root folder \files

Settings()
MsgBox, 0, Settings, Done!
;
; other stuff
;
ExitApp

Settings()
{
   Static MakeImage =
   Gui, SettingsGui:+LastFound +LabelSettingsGui ; set the 'last found window'
   ; Changed Pic to Text for testing
   ; Gui, SettingsGui:Add, Picture, x20 w50 h-1 gCheckSound vTestSound, elements\RadioNO.png
   Gui, SettingsGui:Add, Text, x20 w200 gCheckSound hwndHTestSound, elements\RadioNO.png
   Gui, SettingsGui:Font, s30
   Gui, SettingsGui:Add, Text, x+12 hwndHSoundHint, Sound
   Gui, SettingsGui:Show, Center AutoSize, Current Settings
   WinWaitActive  ; wait for the 'last found window' (i.e. the Gui) to be active
   WinWaitClose   ; wait for the 'last found window' (i.e. the Gui) to be destroyed
   Return
   ; =======================================================
   CheckSound:
      MakeImage := !MakeImage
      If MakeImage
         GuiControl, , %HTestSound%, elements\RadioYES.png
      Else
         GuiControl, , %HTestSound%, elements\RadioNO.png
      SoundBeep
   Return
   ; =======================================================
   SettingsGuiClose:
   SettingsGuiEscape:
      Gui, Destroy
   Return
}
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Gui in function swap image proble

20 Oct 2013, 16:31

Thanks for the replies. Using a global variable works though I don't get why it needs to be global. I couldn't get the hwnd idea to work with a picture. I got warnings that a variable was missing and a global variable was required to make it work. What I eventually want to have happen is for my main script to be paused while the settings function runs. I tried adding the two Winwait lines to my main script but the results were sporadic.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: scriptor2016 and 444 guests