Sign in to follow this  
Followers 0
justt

Help getting my first GUI to close

6 posts in this topic

#1 ·  Posted (edited)

Wanted to try my hand at making a GUI and decided to make a simple one to turn off the laptop after an amount of time specified by me. For testing purposes it uses Msgbox where I would use Shutdown.

The problem I am having is that say I set the GUI to sleep 1 hour before shutting down, I press ok but then the Cancel button as well as the X button at the top right both stop working and I cannot exit the GUI. I assume this is because the script is paused because of Sleep. How can I make it so that I can close the program even when the shutdown process is happening and it is sleeping.

I'm still pretty new so it probably isn't pretty, but ahh well, I got it to work (mostly).

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1_1 = GUICreate("Shutdown Computer", 193, 133, 192, 114)
Global $Hours = GUICtrlCreateInput("Hours", 24, 48, 41, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Global $Minutes = GUICtrlCreateInput("Minutes", 112, 48, 49, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Global $Start = GUICtrlCreateButton("Start", 8, 96, 75, 25, $WS_GROUP)
Global $Cancel = GUICtrlCreateButton("Cancel", 104, 96, 75, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("Shutdown Time", 57, 16, 79, 17)
Global $h = GUICtrlCreateLabel("h", 66, 51, 10, 17)
Global $m = GUICtrlCreateLabel("m", 162, 51, 12, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Cancel
Exit
Case $Start
$sleeptime = (GUICtrlRead($Hours)*60+GUICtrlRead($Minutes))*60*1000
$Label1 =GUICtrlCreateLabel("", 57, 16, 79, 17)
$Label2 = GUICtrlCreateLabel("Shutdown timer began at:", 34, 8, 125, 17)
$Label3 = GUICtrlCreateLabel(_NowTime(), 67, 24, 58, 17)
sleep ($sleeptime)
MsgBox (0,"Test","Shutdown")
EndSwitch
WEnd
Edited by justt

Share this post


Link to post
Share on other sites



I haven't looked closely at your script, but you shouldn't use sleep. Instead use TimerInit and TimerDiff. Check them out in the help file. You should then use something like an 'If' statement to check to see if one hour has elapsed before the shutdown script runs.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'm not really sure exactly what you want, but I have made some modifications to the GUI. There are different ways to do this. The best would be to call on a function. This arrangement closes the GUI once the time is set.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>
#Region ### START Koda GUI section ### Form=
Local $Form1_1 = GUICreate("Shutdown Computer", 193, 133, 192, 114)
Local $Hours = GUICtrlCreateInput("Hours", 24, 48, 41, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Local $Minutes = GUICtrlCreateInput("Minutes", 112, 48, 49, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Local $Start = GUICtrlCreateButton("Start", 8, 96, 75, 25, $WS_GROUP)
Local $Cancel = GUICtrlCreateButton("Cancel", 104, 96, 75, 25, $WS_GROUP)
Local $Label1 = GUICtrlCreateLabel("Shutdown Time", 30, 10, 125, 34, $SS_CENTER)
Local $h = GUICtrlCreateLabel("h", 66, 51, 10, 17)
Local $m = GUICtrlCreateLabel("m", 162, 51, 12, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$iFlag = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Cancel
            ExitLoop
        Case $Start
            $sleeptime = (GUICtrlRead($Hours)*60+GUICtrlRead($Minutes))*60*1000
            GUICtrlSetData($Label1, "Shutdown timer began at:" & @CRLF & _NowTime())
            $iFlag = 1
            ExitLoop
        ;sleep ($sleeptime)
    EndSwitch
WEnd
GUIDelete($Form1_1)

If $iFlag = 1 Then
    sleep ($sleeptime)
    MsgBox (0,"Test","Shutdown") ; Shutdown here
EndIf
Edited by czardas

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Okay here's a better try, I think.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>
#Region ### START Koda GUI section ### Form=
Local $Form1_1 = GUICreate("Shutdown Computer", 193, 133, 192, 114)
Local $Hours = GUICtrlCreateInput("Hours", 24, 48, 41, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Local $Minutes = GUICtrlCreateInput("Minutes", 112, 48, 49, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
Local $Start = GUICtrlCreateButton("Start", 8, 96, 75, 25, $WS_GROUP)
Local $Cancel = GUICtrlCreateButton("Cancel", 104, 96, 75, 25, $WS_GROUP)
Local $Label1 = GUICtrlCreateLabel("Shutdown Time", 30, 10, 125, 34, $SS_CENTER)
Local $h = GUICtrlCreateLabel("h", 66, 51, 10, 17)
Local $m = GUICtrlCreateLabel("m", 162, 51, 12, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$iFlag = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            $iFlag = 0
            ExitLoop
        Case $Cancel
            GUICtrlSetData($Label1, "Shutdown Time")
            $iFlag = 0
        Case $Start
            $sleeptime = (GUICtrlRead($Hours)*60+GUICtrlRead($Minutes))*60*1000
            GUICtrlSetData($Label1, "Shutdown timer began at:" & @CRLF & _NowTime())
            $iFlag = 1
            $iTimer = TimerInit()
    EndSwitch
    If $iFlag = 1 Then
        If TimerDiff($iTimer) > $sleeptime Then ExitLoop
    EndIf
WEnd
GUIDelete($Form1_1)

If $iFlag = 1 Then
    MsgBox (0,"Test","Shutdown") ; Shutdown here
EndIf

I added one more line to return the GUI back to a ready state after you press cancel. I'm not sure if this is what you want.

Edited by czardas

Share this post


Link to post
Share on other sites

Thanks czardas, that's exactly what I wanted! I figured out the "If TimerDiff($iTimer) > $sleeptime Then ExitLoop" part out when you mentioned TimerDiff but wasn't quite sure how to do the rest. And I didn't even think of returning to a ready state, but that works even better than it just exiting. Thanks again.

Share this post


Link to post
Share on other sites

No problem. You should try to avoid creating controls on top of each other. Instead delete a control before you create another in it's place. Nice GUI for a first attempt. :)

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