jmp Posted November 13 Share Posted November 13 I make shortcut for immediately sleep my computer, its working well but when i click 'X' button on Gui windows, its not close. expandcollapse popup#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 Link to comment Share on other sites More sharing options...
pixelsearch Posted November 13 Share Posted November 13 GUISetOnEvent($GUI_EVENT_CLOSE, "_Close") Where is the _Close function ? Werty 1 Link to comment Share on other sites More sharing options...
pixelsearch Posted November 13 Share Posted November 13 @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 : expandcollapse popup#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 Link to comment Share on other sites More sharing options...
jmp Posted November 14 Author Share Posted November 14 @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: expandcollapse popup#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 Link to comment Share on other sites More sharing options...
Nine Posted November 14 Share Posted November 14 (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 November 14 by Nine typo “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
jmp Posted November 14 Author Share Posted November 14 2 hours ago, Nine said: 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. @Nine tried this, But still not showing in center Link to comment Share on other sites More sharing options...
Nine Posted November 14 Share Posted November 14 I tried and it is working perfectly. Remember to remove the : GUICtrlSetPos($label, ....) otherwise it will change the position of the label incorrectly. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
jmp Posted November 14 Author Share Posted November 14 (edited) 9 minutes ago, 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. @Nine Its not working for me, I tried this code: expandcollapse popup#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 November 14 by jmp grammer mistake Link to comment Share on other sites More sharing options...
Nine Posted November 14 Share Posted November 14 1 hour ago, jmp said: Its not working for me Not much of an explanation. I ran your code and it is displayed in the middle of the screen. What is it that you want ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
ioa747 Posted November 14 Share Posted November 14 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 expandcollapse popup; 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now