Sign in to follow this  
Followers 0
Joemonkey

GUI with button and countdown and auto-restore

8 posts in this topic

#1 ·  Posted (edited)

I have this script that works great for a GUI box with a Yes button that thanks to you guys takes DPI into consideration and also repops to the center of the screen every hour if someone moves it

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

AutoItSetOption("GUIEventOptions", 1)
Global $i_DPI_Ratio = _GetDPI()
$i_DPI_Ratio = $i_DPI_Ratio[2]


$Form1 = GUICreate("Title", 447, 205, -1, -1, Default, $WS_EX_TOPMOST)
    _GUISetFont_Ex(12, 400, 0, 'Arial', $Form1) ;
    GUICtrlCreateLabel("Line 1", 10, 10)
    GUICtrlCreateLabel("Line 2", 10, 28)
    GUICtrlCreateLabel("Line 3 ", 10, 46)
    GUICtrlCreateLabel("YES?", 26, 152)
    $Yesbutton = GUICtrlCreateButton("YES", 274, 152, 75, 25)
    GUISetState(@SW_SHOW)
    
    $wPos = WinGetPos($Form1)
    $timer = TimerInit() ;added initial timestamp
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Yesbutton
            Run("notepad.exe")
            Exit
    EndSelect
    If TimerDiff($timer) > 3600000 Then 
        $newPos = WinGetPos($Form1)
        If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1]) 
        $timer = TimerInit();resets the timestamp
    EndIf
WEnd

Func _GetDPI()
    Local $a1[3]
    Local $iDPI, $iDPIRat, $Logpixelsy = 90, $Form1 = 0
    Local $hDC = DllCall("user32.dll", "long", "GetDC", "long", $Form1)
    Local $aRet = DllCall("gdi32.dll", "long", "GetDeviceCaps", "long", $hDC[0], "long", $Logpixelsy)
    Local $hDC = DllCall("user32.dll", "long", "ReleaseDC", "long", $Form1, "long", $hDC)
    $iDPI = $aRet[0]
    ;; Set a ratio for the GUI dimensions based upon the current DPI value.
    Select
        Case $iDPI = 0
            $iDPI = 96
            $iDPIRat = 94
        Case $iDPI < 84
            $iDPIRat = $iDPI / 105
        Case $iDPI < 121
            $iDPIRat = $iDPI / 96
        Case $iDPI < 145
            $iDPIRat = $iDPI / 95
        Case Else
            $iDPIRat = $iDPI / 94
    EndSelect
    $a1[0] = 2
    $a1[1] = $iDPI
    $a1[2] = $iDPIRat
    ;; Return the array
    Return $a1
EndFunc   ;==>_GetDPI

Func _GUICtrlSetFont_Ex($icontrolID = -1, $iSize = 8.5, $iweight = 400, $iattribute = 0, $sfontname = Default, $iquality = 2)
    GUICtrlSetFont($icontrolID, $iSize / $i_DPI_Ratio, $iweight, $iattribute, $sfontname, $iquality)
EndFunc   ;==>_GUICtrlSetFont_Ex

Func _GUISetFont_Ex($iSize = 12, $iweight = 400, $iattribute = 0, $sfontname = Default, $Form1 = Default, $iquality = 2)
    If Not IsHWnd($Form1) Then $Form1 = WinGetHandle($Form1) ;<<<<<<<<<<<<<<<<<<<<<<<<<<<
    GUISwitch($Form1) ;<<<<<<<<<<<<<<<<<<<<
    ConsoleWrite("GUISwitch(0)" & @TAB & $Form1 & @TAB & WinGetTitle($Form1) & @CRLF)
    ConsoleWrite("WinGetHandle([ACTIVE])" & @TAB & WinGetHandle("[ACTIVE]") & @TAB & WinGetTitle(WinGetHandle("[ACTIVE]")) & @CRLF)
    GUISetFont($iSize / $i_DPI_Ratio, $iweight, $iattribute, $sfontname, $Form1, $iquality)
EndFunc   ;==>_GUISetFont_Ex

I'm trying to add in a countdown that will, when counted down to zero, perform the same action as clicking the YES button. I found lots of good countdown code here by searching but when I try to insert it various places I either get no countdown, or clicking the Yes button doesn't work. I'm trying to inject most of the code found here:

I think my problem is I don't know how to have both the While and Do run simultaneously. Ideally I'd like to have a "Line 4" that displays the countdown in HH:MM:SS and when the time is up does the action assigned to the Yes button. Any help is greatly appreciated!

Edited by Joemonkey

Share this post


Link to post
Share on other sites



If I understand correctly, When TimerDiff($timer) > 3600000 you want it to perform the same action as if clicking yes?

You can simply add the Run() and exit commands to the 'If TimerDiff($timer) > 3600000 Then' block


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

that's not exactly what I want, and now I see a problem with having 2 timers. The TimerDiff section of my existing script is there in case someone moves the popup box off the screen. Every hour it will check for the GUI's coordinates and if they don't match the original it will move it back. I'll probably end up changing that to every 30 minutes, but I'd like to have a timer that counts down from say 2 hours then runs the Yes button command. For example:

case 1: GUI shows up with a 2 hour counting down timer, user clicks Yes, notepad.exe is ran.

case 2: GUI shows up with a 2 hour counting down timer, user moves it off the screen, 30 minutes later it repops to center, user clicks Yes, notepad.exe is ran.

case 3: GUI shows up with a 2 hour counting down timer, user does nothing for 2 hours, notepad.exe is ran

Share this post


Link to post
Share on other sites

Try this:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Global $msg,  $label
Global $hGUI = GUICreate("Test", 300, 300)  ; will create a dialog box that when displayed is centered
Global $button = GUICtrlCreateButton("YES", 10, 250, 50)
$label = GUICtrlCreateLabel("", 100,  120, 100, 30)
;~ GUICtrlSetBkColor(-1, 0x808080)
GUICtrlSetFont($label, 20, 400)
GUISetState()      ; will display an empty dialog box

Global $countdown = 0.2 ;45 minutes
Global $seconds = $countdown * 60

Countdown()
AdlibRegister("Countdown", 1000)
Global $dummy = GUICtrlCreateDummy()
Do
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            ExitLoop
        Case $button, $dummy
            Run("calc.exe")
    EndSwitch
Until False


AdlibUnRegister("Countdown")
GUIDelete($hGUI)
Exit

Func Countdown()
    Local $sec, $min, $hr
    $sec = Mod($seconds, 60)
    $min = Mod($seconds / 60, 60)
    $hr = Floor($seconds / 60 ^ 2)
    GUICtrlSetData($label, StringFormat("%02i:%02i:%02i", $hr, $min, $sec))
    $seconds -= 1
    If $seconds < 0 Then
        GUICtrlSendToDummy($dummy)
        AdlibUnRegister("Countdown")
    EndIf
EndFunc

Based on the code from the link you provided.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks UEZ, I see what you did and it makes sense. However, I've recently found this script

and it's working great! Here's how I have it coded with a 5 minute countdown and a repop to center ever 0.6 minutes:

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Timers.au3>
#include <Date.au3>

Global $iCountdown = 300
AutoItSetOption("GUIEventOptions", 1)
Global $i_DPI_Ratio = _GetDPI()
$i_DPI_Ratio = $i_DPI_Ratio[2]



$Form1 = GUICreate("", 633, 350, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE)
;GUISetBkColor(0x9F79EE)
$Label1 = GUICtrlCreateLabel("Mesage header", 10, 10, 596, 50, $SS_CENTER)
GUICtrlSetFont(-1, 20, 800, 0, "Arial")
;GUICtrlSetColor(-1, 0xffff00);Yellow
$Label2 = GUICtrlCreateLabel('Message Text...' & @CRLF & @CRLF & "and then click the OK button below.", 20, 58, 590, 170)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$Label3 = GUICtrlCreateLabel("(Message Note)", 20, 228, 588, 25, $SS_CENTER)
GUICtrlSetFont(-1, 12, 800, 6, "Arial")
;GUICtrlSetColor(-1, 0x00008B);Dark Blue
$Button = GUICtrlCreateButton("OK", 240, 265, 145, 41, 0)
$Label4 = GUICtrlCreateLabel("                This Window will automatically close and continue the installation in 5 minutes.  Time:  " & _SecsToTime($iCountdown), 24, 325, 588, 25)
GUISetState(@SW_SHOW)

$wPos = WinGetPos($Form1)
$timer = TimerInit() ;added initial timestamp

_Timer_SetTimer($Form1, 1000, '_Countdown')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Button
        Run("notepad.exe")
            Exit
        EndSwitch
        If TimerDiff($timer) > 36000 Then 
            $newPos = WinGetPos($Form1)
            If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1]) 
        $timer = TimerInit();resets the timestamp
    EndIf   
WEnd

Func _Countdown($hWnd, $iMsg, $iIDTimer, $dwTime)
    $iCountdown -= 1
    If $iCountdown > 0 Then
        GUICtrlSetData($Label4, "                 This Window will automatically close and continue the installation in 5 minutes.  Time:  " & _SecsToTime($iCountdown))
    ElseIf $iCountdown = 0 Then
        _Timer_KillTimer($hWnd, $iIDTimer)
        ControlClick($Form1, '', $Button)
    EndIf
EndFunc ;==>_Countdown

Func _SecsToTime($iSecs)
    Local $iHours, $iMins, $iSec_s
    _TicksToTime($iSecs*1000,$iHours,$iMins,$iSec_s)
    Return StringFormat("%02i:%02i",$iMins, $iSec_s)
EndFunc

Func _GetDPI()
    Local $a1[3]
    Local $iDPI, $iDPIRat, $Logpixelsy = 90, $Form1 = 0
    Local $hDC = DllCall("user32.dll", "long", "GetDC", "long", $Form1)
    Local $aRet = DllCall("gdi32.dll", "long", "GetDeviceCaps", "long", $hDC[0], "long", $Logpixelsy)
    Local $hDC = DllCall("user32.dll", "long", "ReleaseDC", "long", $Form1, "long", $hDC)
    $iDPI = $aRet[0]
    ;; Set a ratio for the GUI dimensions based upon the current DPI value.
    Select
        Case $iDPI = 0
            $iDPI = 96
            $iDPIRat = 94
        Case $iDPI < 84
            $iDPIRat = $iDPI / 105
        Case $iDPI < 121
            $iDPIRat = $iDPI / 96
        Case $iDPI < 145
            $iDPIRat = $iDPI / 95
        Case Else
            $iDPIRat = $iDPI / 94
    EndSelect
    $a1[0] = 2
    $a1[1] = $iDPI
    $a1[2] = $iDPIRat
    ;; Return the array
    Return $a1
EndFunc   ;==>_GetDPI

Func _GUICtrlSetFont_Ex($icontrolID = -1, $iSize = 8.5, $iweight = 400, $iattribute = 0, $sfontname = Default, $iquality = 2)
    GUICtrlSetFont($icontrolID, $iSize / $i_DPI_Ratio, $iweight, $iattribute, $sfontname, $iquality)
EndFunc   ;==>_GUICtrlSetFont_Ex

Func _GUISetFont_Ex($iSize = 12, $iweight = 400, $iattribute = 0, $sfontname = Default, $Form1 = Default, $iquality = 2)
    If Not IsHWnd($Form1) Then $Form1 = WinGetHandle($Form1) ;<<<<<<<<<<<<<<<<<<<<<<<<<<<
    GUISwitch($Form1) ;<<<<<<<<<<<<<<<<<<<<
    ConsoleWrite("GUISwitch(0)" & @TAB & $Form1 & @TAB & WinGetTitle($Form1) & @CRLF)
    ConsoleWrite("WinGetHandle([ACTIVE])" & @TAB & WinGetHandle("[ACTIVE]") & @TAB & WinGetTitle(WinGetHandle("[ACTIVE]")) & @CRLF)
    GUISetFont($iSize / $i_DPI_Ratio, $iweight, $iattribute, $sfontname, $Form1, $iquality)
EndFunc   ;==>_GUISetFont_Ex

Share this post


Link to post
Share on other sites

This is a very nice code made by user UEZ, how i can make it count until a specific hour, for example if a executed now it will say

23 days

556 hours

33378 minutes

and if i executed 1 day before new year to say

1 days

556 hours

33378 minutes

etc.

I mean when i close the script the next time i open it to saw the correct hour date until new eve and when is 2 hours before to popup an image or something.

and how could i do it to show that info hours/date etc. only in a balloon in sys tray ?

Thank you people.

Try this:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Global $msg, $label
Global $hGUI = GUICreate("Test", 300, 300) ; will create a dialog box that when displayed is centered
Global $button = GUICtrlCreateButton("YES", 10, 250, 50)
$label = GUICtrlCreateLabel("", 100, 120, 100, 30)
;~ GUICtrlSetBkColor(-1, 0x808080)
GUICtrlSetFont($label, 20, 400)
GUISetState() ; will display an empty dialog box

Global $countdown = 0.2 ;45 minutes
Global $seconds = $countdown * 60

Countdown()
AdlibRegister("Countdown", 1000)
Global $dummy = GUICtrlCreateDummy()
Do
$msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $button, $dummy
            Run("calc.exe")
    EndSwitch
Until False


AdlibUnRegister("Countdown")
GUIDelete($hGUI)
Exit

Func Countdown()
Local $sec, $min, $hr
$sec = Mod($seconds, 60)
$min = Mod($seconds / 60, 60)
$hr = Floor($seconds / 60 ^ 2)
GUICtrlSetData($label, StringFormat("%02i:%02i:%02i", $hr, $min, $sec))
$seconds -= 1
If $seconds < 0 Then
        GUICtrlSendToDummy($dummy)
        AdlibUnRegister("Countdown")
    EndIf
EndFunc

Based on the code from the link you provided.

Br,

UEZ

Share this post


Link to post
Share on other sites

How many posts are you going to make on the same subject? You now have 3 or 4 dealing with your New Year's eve timer.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

noobish,

I count 4 threads at the moment - no more! :naughty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0