As per solution provided on the AutoIt forum, here is a version of the script that hides and shows the dialog on each usage. There is a big caveat (read the forum thread for details) in that on creation of the dialog, it must be explicitly hidden!
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
; Do not declare Global variables in a function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $settings_window_handle, $ok_button, $cancel_button
;; Set up settings dialog
InitSettingsDialog();
;; Start program in system tray
SetupSystemTrayEntry()
Func SetupSystemTrayEntry()
Opt("TrayMenuMode", 1)
$settingsitem = TrayCreateItem("Settings")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TraySetState()
While 1
Switch TrayGetMsg()
Case $settingsitem
ShowSettingsDialog() ;; Bring up settings dialog
Case $exititem
Exit ;; Exit program
EndSwitch
WEnd
EndFunc ;==>SetupSystemTrayEntry
Func InitSettingsDialog()
; Create
$settings_window_handle = GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
$ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
$cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc ;==>SettingsDialog
Func ShowSettingsDialog()
GUISetState(@SW_SHOW, $settings_window_handle)
While 1
Switch GUIGetMsg()
Case $ok_button
MsgBox(0, "test", "test")
ExitLoop
Case $cancel_button
ExitLoop
EndSwitch
WEnd
GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc