Sign in to follow this  
Followers 0
Steph

Using events - what am I doing wrong?

5 posts in this topic

I thought I'd develop what I've contributed to in this thread about 'always-on-top clocks', mainly for me to learn more about AutoIt. I'd thought I'd try using using events instead of polling for messages.

When I run the script a little window appears in the top left hand corner, right click on this and select 'prefs', the preferences window opens, but doesn't seem to close when you click on the 'X', 'OK' or 'Cancel'. If you uncomment the indicated line in the script so the preferences window appears straight away then you can close it, but again it doesn't work if you make it appear using the menu. (Hope that made sense).

So what am I doing wrong - thanks for your help.

Steph

;*********************************************************************
; Options
;*********************************************************************
Opt("MustDeclareVars", 1)   ; Require that variables are declared (Dim) before use
Opt("GUIOnEventMode", 1 )   ; Event driven rather than polling

;*********************************************************************
; Constants
;*********************************************************************
#include <GuiConstants.au3>

Global Const $HTCAPTION = 2
Global Const $WM_NCLBUTTONDOWN = 0xA1
Global Const $TRUE = 1
Global Const $FALSE = 0

;*********************************************************************
; Variables
;*********************************************************************

; Clock dimensions
Global $Height = 36
Global $Width = 100

; Clock GUI
Global $gui_Clock, $lb_Time

; Preferences GUI
Global $gui_Prefs, $sl_prefs_Trans, $bt_prefs_OK, $bt_prefs_Cancel

; Menu
Global $mn_Main, $mn_Quit, $mn_Prefs

Global $EndSetPrefs

;*********************************************************************
; Main body
;*********************************************************************
CreateClockGUI()
CreatePrefsGUI()
;SetPrefs()   ; >>> Try uncommenting this line - it works first time, but not next! <<<
While 1
    Sleep(50)
WEnd

;*********************************************************************
Func CreateClockGUI()
    $gui_Clock = GUICreate ( "Clock", $Width, $Height, 0, 0, $WS_POPUP + $WS_BORDER + $WS_SIZEBOX, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)
    $lb_Time = GUICtrlCreateLabel ( "", 0, 0, $Width, $Height, $SS_CENTER )
    
;Create a right-click context menu
    $mn_Main = GUICtrlCreateContextMenu($lb_Time)
    $mn_Prefs = GUICtrlCreateMenuitem("Prefs",$mn_Main)
    $mn_Quit = GUICtrlCreateMenuitem("Quit", $mn_Main )
    
;Associate events
    GuiCtrlSetOnEvent($mn_Quit, "MyExit")
    GuiCtrlSetOnEvent($mn_Prefs, "SetPrefs")
    
;Show the GUI
    GUISetState(@SW_SHOW, $gui_Clock)
EndFunc

Func CreatePrefsGUI()
    Const $PREF_WIDTH = 400
    Const $PREF_HEIGHT = 200
    $gui_Prefs = GUICreate("Preferences", $PREF_WIDTH, $PREF_HEIGHT, -1, -1, -1, -1, $gui_Clock)
    $sl_prefs_Trans = GUICtrlCreateSlider(10, 10, $PREF_WIDTH-20, 40)
    $bt_prefs_Cancel = GUICtrlCreateButton("Cancel", $PREF_WIDTH - 110, $PREF_HEIGHT - 40, 50, 30)
    $bt_prefs_OK = GUICtrlCreateButton("OK", $PREF_WIDTH - 60, $PREF_HEIGHT - 40, 50, 30)
    
    GUISetOnEvent($GUI_EVENT_CLOSE, "PrefsEnd")
    GUICtrlSetonEvent($bt_prefs_OK, "PrefsEnd")
    GuiCtrlSetOnEvent($bt_prefs_Cancel, "PrefsEnd")
EndFunc

Func SetPrefs()
    $EndSetPrefs = $FALSE
    GUISetState(@SW_SHOW, $gui_Prefs)
    Do
        Sleep(50)
    Until $EndSetPrefs = $TRUE
    GUISetState(@SW_HIDE, $gui_Prefs)
EndFunc

Func PrefsEnd()
    $EndSetPrefs = $TRUE
EndFunc

Func MyExit()
    GUIDelete($gui_Clock)
    Exit
EndFunc

Share this post


Link to post
Share on other sites



didn't have any luck figuring out why both gui's are not using the on event properly

so i played around with the second one and used polling on it, this seems to work properly

;*********************************************************************
; Options
;*********************************************************************
Opt("MustDeclareVars", 1)   ; Require that variables are declared (Dim) before use
Opt("GUIOnEventMode", 1 )   ; Event driven rather than polling

;*********************************************************************
; Constants
;*********************************************************************
#include <GuiConstants.au3>

Global Const $HTCAPTION = 2
Global Const $WM_NCLBUTTONDOWN = 0xA1
Global Const $TRUE = 1
Global Const $FALSE = 0

;*********************************************************************
; Variables
;*********************************************************************

; Clock dimensions
Global $Height = 36
Global $Width = 100

; Clock GUI
Global $gui_Clock, $lb_Time

; Preferences GUI
Global $gui_Prefs, $sl_prefs_Trans, $bt_prefs_OK, $bt_prefs_Cancel

; Menu
Global $mn_Main, $mn_Quit, $mn_Prefs

Global $EndSetPrefs

;*********************************************************************
; Main body
;*********************************************************************
CreateClockGUI()
SetPrefs()   ; >>> Try uncommenting this line - it works first time, but not next! <<<
While 1
    Sleep(50)
WEnd

;*********************************************************************
Func CreateClockGUI()
    $gui_Clock = GUICreate ( "Clock", $Width, $Height, 0, 0, $WS_POPUP + $WS_BORDER + $WS_SIZEBOX, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)
    $lb_Time = GUICtrlCreateLabel ( "", 0, 0, $Width, $Height, $SS_CENTER )
    
;Create a right-click context menu
    $mn_Main = GUICtrlCreateContextMenu($lb_Time)
    $mn_Prefs = GUICtrlCreateMenuitem("Prefs",$mn_Main)
    $mn_Quit = GUICtrlCreateMenuitem("Quit", $mn_Main )
    
;Associate events
    GuiCtrlSetOnEvent($mn_Quit, "MyExit")
    GuiCtrlSetOnEvent($mn_Prefs, "SetPrefs")
    
;Show the GUI
    GUISetState(@SW_SHOW, $gui_Clock)
EndFunc

Func SetPrefs()
    Opt("GUIOnEventMode", 0 )
    Const $PREF_WIDTH = 400
    Const $PREF_HEIGHT = 200
    local $msg
    $gui_Prefs = GUICreate("Preferences", $PREF_WIDTH, $PREF_HEIGHT, -1, -1, -1, -1, $gui_Clock)
    $sl_prefs_Trans = GUICtrlCreateSlider(10, 10, $PREF_WIDTH-20, 40)
    $bt_prefs_Cancel = GUICtrlCreateButton("Cancel", $PREF_WIDTH - 110, $PREF_HEIGHT - 40, 50, 30)
    $bt_prefs_OK = GUICtrlCreateButton("OK", $PREF_WIDTH - 60, $PREF_HEIGHT - 40, 50, 30)
    GUISetState()
    Do
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $bt_prefs_OK Or $msg = $bt_prefs_Cancel
            ExitLoop
        EndSelect
    Until $MSG = $GUI_EVENT_CLOSE
    GUIDelete($gui_prefs)
    Opt("GUIOnEventMode", 1 )   ; Event driven rather than polling
EndFunc

Func MyExit()
    GUIDelete($gui_Clock)
    Exit
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

The GuiOnEvent stuff appears to dislike the Sleep(50) inside the SetPrefs function. Move the logic to the main While-Wend loop:

CODE
Opt("TrayIconDebug", 1)

;*********************************************************************

; Options

;*********************************************************************

Opt("MustDeclareVars", 1) ; Require that variables are declared (Dim) before use

Opt("GUIOnEventMode", 1 ) ; Event driven rather than polling

;*********************************************************************

; Constants

;*********************************************************************

#include <GuiConstants.au3>

Global Const $HTCAPTION = 2

Global Const $WM_NCLBUTTONDOWN = 0xA1

Global Const $TRUE = 1

Global Const $FALSE = 0

;*********************************************************************

; Variables

;*********************************************************************

; Clock dimensions

Global $Height = 36

Global $Width = 100

; Clock GUI

Global $gui_Clock, $lb_Time

; Preferences GUI

Global $gui_Prefs, $sl_prefs_Trans, $bt_prefs_OK, $bt_prefs_Cancel

; Menu

Global $mn_Main, $mn_Quit, $mn_Prefs

Global $EndSetPrefs = $TRUE

;*********************************************************************

; Main body

;*********************************************************************

CreateClockGUI()

CreatePrefsGUI()

;SetPrefs() ; >>> Try uncommenting this line - it works first time, but not next! <<<

While 1

Sleep(50)

If $EndSetPrefs = $FALSE Then

GUISetState(@SW_SHOW, $gui_Prefs)

Else

GUISetState(@SW_HIDE, $gui_Prefs)

EndIf

WEnd

;*********************************************************************

Func CreateClockGUI()

$gui_Clock = GUICreate ( "Clock", $Width, $Height, 0, 0, $WS_POPUP + $WS_BORDER + $WS_SIZEBOX, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

$lb_Time = GUICtrlCreateLabel ( "", 0, 0, $Width, $Height, $SS_CENTER )

;Create a right-click context menu

$mn_Main = GUICtrlCreateContextMenu($lb_Time)

$mn_Prefs = GUICtrlCreateMenuitem("Prefs",$mn_Main)

$mn_Quit = GUICtrlCreateMenuitem("Quit", $mn_Main )

;Associate events

GuiCtrlSetOnEvent($mn_Quit, "MyExit")

GuiCtrlSetOnEvent($mn_Prefs, "SetPrefs")

;Show the GUI

GUISetState(@SW_SHOW, $gui_Clock)

EndFunc

Func CreatePrefsGUI()

Const $PREF_WIDTH = 400

Const $PREF_HEIGHT = 200

$gui_Prefs = GUICreate("Preferences", $PREF_WIDTH, $PREF_HEIGHT, -1, -1, -1, -1, $gui_Clock)

$sl_prefs_Trans = GUICtrlCreateSlider(10, 10, $PREF_WIDTH-20, 40)

$bt_prefs_Cancel = GUICtrlCreateButton("Cancel", $PREF_WIDTH - 110, $PREF_HEIGHT - 40, 50, 30)

$bt_prefs_OK = GUICtrlCreateButton("OK", $PREF_WIDTH - 60, $PREF_HEIGHT - 40, 50, 30)

GuiSetState(@SW_HIDE)

GUISetOnEvent($GUI_EVENT_CLOSE, "PrefsEnd")

GUICtrlSetonEvent($bt_prefs_OK, "PrefsEnd")

MsgBox(4096,"setOnEvent", GuiCtrlSetOnEvent($bt_prefs_Cancel, "PrefsEnd"))

EndFunc

Func SetPrefs()

$EndSetPrefs = $FALSE

EndFunc

Func PrefsEnd()

$EndSetPrefs = $TRUE

EndFunc

Func MyExit()

GUIDelete($gui_Clock)

Exit

EndFunc


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Here was your problem:

You had GuiCtrlSetOnEvent call a function that did not return immediately.

It appears that Events are queued up until the event function returns.

Example: Click the button twice in a row, the click close. You have to wait about 6 seconds before the GUI closes.

#include <GuiConstants.au3>
Opt("GuiOnEventMode", 1)

GuiCreate("Example")
GuiSetOnEvent($GUI_EVENT_CLOSE, "quit")
GuiCtrlCreateButton("Click Me", 50, 50)
GuiCtrlSetOnEvent(-1, "foo")
GuiSetState()

While 1
    sleep(10)
WEnd

Func Quit()
    Exit
EndFunc

Func foo()
    sleep(3000)
EndFunc

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

You had GuiCtrlSetOnEvent call a function that did not return immediately.

It appears that Events are queued up until the event function returns.

Thank you CyberSlug (and gafrost) for your suggestions and solutions. Is this behaviour documented anywhere and I missed it or is it something we should suggest is added?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0