Jump to content

Recommended Posts

Posted

I make shortcut for immediately sleep my computer, its working well but when i click 'X' button on Gui windows, its not close.

#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)


; Set the hotkey for ESC to cancel sleep
HotKeySet("{ESC}", "CancelSleep")

; Countdown timer duration in seconds
Local $countdownTime = 10

; Get screen dimensions to maximize the window
Local $screenWidth = @DesktopWidth
Local $screenHeight = @DesktopHeight

; Get the current hour to determine if it's day or night (use system time)
Local $currentHour = @HOUR

; Create GUI window with full screen size
$hGUI = GUICreate("Sleep Countdown", $screenWidth, $screenHeight, 0, 0, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE, $WS_EX_TOPMOST)

; Set background color based on the time of day (night or day mode)
If $currentHour >= 18 Or $currentHour < 6 Then
    GUISetBkColor(0x000000) ; Black background for night
Else
    GUISetBkColor(0xFFFFFF) ; White background for day
EndIf

; Calculate the width of the label based on the font and text
Local $textWidth = StringLen($countdownTime) * 50 ; Estimate width based on text length and font size

; Create label with countdown text, positioned to center horizontally and vertically
$label = GUICtrlCreateLabel($countdownTime, ($screenWidth - $textWidth) / 2, $screenHeight / 2 - 100, $textWidth, 200)
GUICtrlSetFont($label, 50, 800) ; Set large text size
GUICtrlSetColor($label, 0xFF0000) ; Set red color for text

; Set up event-driven mode for window events
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")

; Show the GUI and maximize the window
GUISetState(@SW_MAXIMIZE)

; Countdown loop
For $i = $countdownTime To 1 Step -1
    GUICtrlSetData($label, $i)  ; Update the label to show only the countdown number
    Sleep(1000) ; Wait for 1 second
Next

; Put the system to sleep when the countdown reaches 0
Shutdown($SD_STANDBY)
Exit

; Function to handle window close event
Func CloseWindow()
     GUIDelete()
    MsgBox($MB_ICONINFORMATION, "Canceled", "Sleep canceled by closing the window.")
    Exit
EndFunc

; Function to cancel the sleep if ESC key is pressed
Func CancelSleep()
 GUIDelete()
    MsgBox($MB_ICONINFORMATION, "Canceled", "Sleep canceled due to ESC key press.")
    Exit
EndFunc

 

Posted

@Jos maybe this could be useful for you, you'll see :)

1) OP's script  :

GUISetOnEvent($GUI_EVENT_CLOSE, "_Close") ; function _Close() doesn't exist

* If you check the syntax with Ctrl+F5 (from Scite) then you got a fatal error in the Console :

error: _Close(): undefined function
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

* If you run the script, then there is no fatal error, only the GUI won't close as there is no _Close function (just key Esc to end the script)

2) Let's change this line in the script, which becomes :

Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "_Close") ; function _Close() doesn't exist

* If you check the syntax with Ctrl+F5 => same fatal error in Console as 1)

error: _Close(): undefined function
Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

* But if you run the script, then there is a fatal error at runtime, the console displaying this message :

Error in expression.:
Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
Local $iRet = ^ ERROR

So just the fact of adding "Local $iRet = " changes AutoIt behavior when the script is run and the function doesn't exist (AutoIt 3.3.16.1)

3) Of course there is no error at runtime when the function exists :

Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "CloseWindow") ; function CloseWindow() exists.
ConsoleWrite("$iRet = " & $iRet & "   @error = " & @error & @crlf) ; $iRet = 1 , @error = 0

Here is the basic reproducer, for what it's worth, allowing to test the 3 possibilites by commenting out what needed :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

; Set the hotkey for ESC to cancel sleep
HotKeySet("{ESC}", "CancelSleep")

; Get screen dimensions to maximize the window
Local $screenWidth = @DesktopWidth
Local $screenHeight = @DesktopHeight

; Create GUI window with full screen size
$hGUI = GUICreate("Sleep Countdown", $screenWidth, $screenHeight, 0, 0, $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)

;===========================================
; Set up event-driven mode for window events

; 1) OP's script (function _Close doesn't exist)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
ConsoleWrite("@error = " & @error & @crlf) ; @error = 1 when script is run (+++)

; 2) Adding Local $iRet (function _Close doesn't exist)
; Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
; ConsoleWrite("$iRet = " & $iRet & "   @error = " & @error & @crlf) ; this line will never be executed

; 3) All is good when the function exists :
; Local $iRet = GUISetOnEvent($GUI_EVENT_CLOSE, "CloseWindow")
; ConsoleWrite("$iRet = " & $iRet & "   @error = " & @error & @crlf) ; $iRet = 1 , @error = 0
;===========================================

; Show the GUI and maximize the window
GUISetState(@SW_MAXIMIZE)

While 1
    Sleep(10)
WEnd

; Function to handle window close event
Func CloseWindow()
     GUIDelete()
    MsgBox($MB_ICONINFORMATION, "Canceled", "Sleep canceled by closing the window.")
    Exit
EndFunc

; Function to cancel the sleep if ESC key is pressed
Func CancelSleep()
 GUIDelete()
    MsgBox($MB_ICONINFORMATION, "Canceled", "Sleep canceled due to ESC key press.")
    Exit
EndFunc

 

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted

@pixelsearch Your code is working fine, but the timer text showing up at the bottom-right before moving to the center not directly show at the center.

 

My code is:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; Enable event-driven mode

; Set the hotkey for ESC to cancel sleep
HotKeySet("{ESC}", "CancelSleep")

; Countdown timer duration in seconds
Local $countdownTime = 10

; Get screen dimensions to maximize the window
Local $screenWidth = @DesktopWidth
Local $screenHeight = @DesktopHeight

; Get the current hour to determine if it's day or night (use system time)
Local $currentHour = @HOUR

; Create GUI window with full screen size
$hGUI = GUICreate("Sleep Countdown", $screenWidth, $screenHeight, 0, 0, $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)

; Set up event-driven mode for window close (X button)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseWindow")

; Check if it's day or night and set background color
If $currentHour >= 18 Or $currentHour < 6 Then
    GUISetBkColor(0x000000) ; Black background for night
Else
    GUISetBkColor(0xFFFFFF) ; White background for day
EndIf

; Calculate the width of the label based on the font and text
Local $textWidth = StringLen($countdownTime) * 50

; Create label initially centered
Local $label = GUICtrlCreateLabel($countdownTime, ($screenWidth - $textWidth) / 2, ($screenHeight / 2) - 100, $textWidth, 200)
GUICtrlSetFont($label, 50, 800)
GUICtrlSetColor($label, 0xFF0000)

; Center the label explicitly before showing the GUI
GUICtrlSetPos($label, ($screenWidth - $textWidth) / 2, ($screenHeight / 2) - 100)

; Show the GUI maximized
GUISetState(@SW_MAXIMIZE)

; Countdown loop
For $i = $countdownTime To 1 Step -1
    GUICtrlSetData($label, $i)
    Sleep(1000)
Next

; Put the system to sleep when the countdown reaches 0
Shutdown($SD_STANDBY)
Exit

; Function to handle window close event (X button)
Func CloseWindow()
    Exit
EndFunc

; Function to cancel the sleep if ESC key is pressed
Func CancelSleep()
    Exit
EndFunc

 

Posted (edited)

Create your label this way instead :

Local $label = GUICtrlCreateLabel($countdownTime, ($screenWidth - 100) / 2, ($screenHeight - 100) / 2, 100, 100, BitOR($SS_CENTER, $SS_CENTERIMAGE))

See help file for more details.

Edited by Nine
typo
Posted (edited)
  On 11/14/2024 at 3:36 PM, Nine said:

I tried and it is working perfectly.  Remember to remove the :

GUICtrlSetPos($label, ....)

otherwise it will change the position of the label incorrectly.

Expand  

@Nine Its not working for me,

I tried this code:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>


Opt("GUIOnEventMode", 1) ; Enable event-driven mode

; Set the hotkey for ESC to cancel sleep
HotKeySet("{ESC}", "CancelSleep")

; Countdown timer duration in seconds
Local $countdownTime = 10

; Get screen dimensions to maximize the window
Local $screenWidth = @DesktopWidth
Local $screenHeight = @DesktopHeight

; Get the current hour to determine if it's day or night (use system time)
Local $currentHour = @HOUR

; Create GUI window with full screen size
$hGUI = GUICreate("Sleep Countdown", $screenWidth, $screenHeight, 0, 0, $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)

; Set up event-driven mode for window close (X button)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseWindow")

; Check if it's day or night and set background color
If $currentHour >= 18 Or $currentHour < 6 Then
    GUISetBkColor(0x000000) ; Black background for night
Else
    GUISetBkColor(0xFFFFFF) ; White background for day
EndIf

; Calculate the width of the label based on the font and text
Local $textWidth = StringLen($countdownTime) * 50

; Create label initially centered
Local $label = GUICtrlCreateLabel($countdownTime, ($screenWidth - 100) / 2, ($screenHeight - 100) / 2, 100, 100, BitOR($SS_CENTER, $SS_CENTERIMAGE))
;~  $label = GUICtrlCreateLabel($countdownTime, ($screenWidth - $textWidth) / 2, ($screenHeight / 2) - 100, $textWidth, 200)
GUICtrlSetFont($label, 50, 800)
GUICtrlSetColor($label, 0xFF0000)

; Center the label explicitly before showing the GUI
;~ GUICtrlSetPos($label, ($screenWidth - $textWidth) / 2, ($screenHeight / 2) - 100)

; Show the GUI maximized
GUISetState(@SW_MAXIMIZE)

; Countdown loop
For $i = $countdownTime To 1 Step -1
    GUICtrlSetData($label, $i)
    Sleep(1000)
Next

; Put the system to sleep when the countdown reaches 0
Shutdown($SD_STANDBY)
Exit

; Function to handle window close event (X button)
Func CloseWindow()
    Exit
EndFunc

; Function to cancel the sleep if ESC key is pressed
Func CancelSleep()
    Exit
EndFunc

 

Edited by jmp
grammer mistake
Posted

The script seems to be written by an AI filled with so much information that it's hard to read.
I removed what I considered unnecessary, such as e.g.  Local $screenWidth = @DesktopWidth
(what's the point of the $screenWidth variable holding the value of the @DesktopWidth macro since it doesn't change?)
as well as the additional functions that do the same thing like CloseWindow(), CancelSleep()
in my opinion a regular window is not needed either,  so no need event to close the window,   since we have the hotkey with esc

(These are my thoughts that I wanted to share :))

here is my lite variation

; https://www.autoitscript.com/forum/topic/212464-gui-window-not-close-when-click-on-x-button/?do=findComment&comment=1538540
#include <AutoItConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

HotKeySet("{ESC}", "CancelSleep") ; Set the hotkey for ESC to cancel sleep

Local $countdownTime = 10 ; Countdown timer duration in seconds

; Create GUI window with full screen size
$hGUI = GUICreate("Sleep Countdown", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
WinSetTrans($hGUI, "", 150) ;255 = Solid, 0 = Invisible.

; Create label initially centered
Local $label = GUICtrlCreateLabel($countdownTime, (@DesktopWidth * 0.4), (@DesktopHeight * 0.4), (@DesktopWidth * 0.2), (@DesktopHeight * 0.1), BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont($label, (@DesktopHeight * 0.1), 800)
GUICtrlSetColor($label, 0xFF0000)

; Check if it's day or night and set background color
If @HOUR >= 18 Or @HOUR < 6 Then
    GUISetBkColor(0x000000) ; Black background for night
Else
    GUISetBkColor(0xFFFFFF) ; White background for day
EndIf

GUISetState(@SW_MAXIMIZE) ; Show the GUI maximized

; Countdown loop
For $i = $countdownTime To 1 Step -1
    GUICtrlSetData($label, $i)
    Sleep(1000)
Next

Shutdown($SD_STANDBY) ; Put the system to sleep when the countdown reaches 0

; Function to cancel the sleep if ESC key is pressed
Func CancelSleep()
    Exit
EndFunc   ;==>CancelSleep

 

I know that I know nothing

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...