Jump to content

Flickering loop


Recommended Posts

I have created a script to count the amount of weeks, days, hours, minutes, seconds to a given date. As I am not very savy with Autoit I have managed to get it working but the loop causes a lot of flickering. I also want to have the GUI window always on top (i.e. BitOR($WS_POPUP, $WS_BORDER), $WS_EX_TOPMOST) but that causes eccessive flickering. This makes me think that I have scripted it wrong from the start. Would someone please look at what I have done and give me a suggestion on how to improve it. The settings.ini just has two lines:

XMAS

2011/12/25 17:00:00

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=smiley.ico
#AutoIt3Wrapper_outfile=Count2Date.exe
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Date.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Global Const $TRAY_DEFAULT    = 512

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 3)
Opt("TrayIconHide", 0);show(0) or hide(1) tray icon

TraySetClick(16)  ; Only secondary mouse button will show the tray menu.
$foo2 = TrayCreateItem("Exit")
$foo = TrayCreateItem("Restore Window")

TrayItemSetState(-1, $TRAY_DEFAULT)


$file = FileOpen("settings.ini", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
;$FileContents = FileRead($file)
$line1 = FileReadLine($file,1)
$line2 = FileReadLine($file,2)

$Name = ($line1)
$Name2 = ("Count2Date")
$FutureDate = ($line2)
; Calculate the number of ...... to given date
$iDateCalc0 = _DateDiff( 'w',_NowCalc(), $FutureDate)
$iDateCalc1 = _DateDiff( 'D',_NowCalc(), $FutureDate)
$iDateCalc2 = _DateDiff( 'h',_NowCalc(), $FutureDate)
$iDateCalc3 = _DateDiff( 'n',_NowCalc(), $FutureDate)
$iDateCalc4 = _DateDiff( 's',_NowCalc(), $FutureDate)

Countgui()

Func Countgui()
    Local $widthCell, $msg, $iOldOpt

    GUICreate($Name2, 125, 175, @DesktopWidth - 200, @DesktopHeight - 250);, BitOR($WS_POPUP, $WS_BORDER), $WS_EX_TOPMOST); will create a dialog box

    $iOldOpt = Opt("GUICoordMode", 2)

    $widthCell = 125
    $MsgLabel = GUICtrlCreateLabel($Name,10,15, $widthCell)
    GUICtrlSetFont($MsgLabel, 12, 400, 1)
    $MsgLabel = GUICtrlCreateLabel("COUNTDOWN",-1,0)
    GUICtrlSetFont($MsgLabel, 12, 400, 5)
    $label0 = GUICtrlCreateLabel("Weeks:   " & $iDateCalc0, -1, 5)
    $label1 = GUICtrlCreateLabel("Days:   " & $iDateCalc1, -1, 0)
    $label2 = GUICtrlCreateLabel("Hours:   " & $iDateCalc2, -1, 0)
    $label3 = GUICtrlCreateLabel("Minutes:   " & $iDateCalc3, -1, 0)
    $label4 = GUICtrlCreateLabel("Seconds:   " & $iDateCalc4, -1, 0)

    GUISetState()       ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1

    ;Sleep(1000)

        $trayMsg = TrayGetMsg()
    ;If $trayMsg = $foo Then
    ;   GuiSetState(@SW_SHOW);show GUI
    ;   GUISetState(@SW_RESTORE);bring GUI from minimized state
    ;EndIf
    ;If $trayMsg = $foo2 Then
    ;    GUIDelete()
    ;   ExitLoop
    ;EndIf

    Select
        Case $traymsg = $foo
            GuiSetState(@SW_SHOW);show GUI
            GUISetState(@SW_RESTORE);bring GUI from minimized state
        Case $traymsg = $foo2
            GUIDelete()
            ExitLoop
    EndSelect


        $msg = GUIGetMsg()

        $iDateCalc0 = _DateDiff( 'w',_NowCalc(), $FutureDate)
        $iDateCalc1 = _DateDiff( 'D',_NowCalc(), $FutureDate)
        $iDateCalc2 = _DateDiff( 'h',_NowCalc(), $FutureDate)
        $iDateCalc3 = _DateDiff( 'n',_NowCalc(), $FutureDate)
        $iDateCalc4 = _DateDiff( 's',_NowCalc(), $FutureDate)

        ControlSetText($Name2, "", $label0, "Weeks:   " & $iDateCalc0)
        ControlSetText($Name2, "", $label1, "Days:   " & $iDateCalc1)
        ControlSetText($Name2, "", $label2, "Hours:   " & $iDateCalc2)
        ControlSetText($Name2, "", $label3, "Minutes:   " & $iDateCalc3)
        ControlSetText($Name2, "", $label4, "Seconds:   " & $iDateCalc4)

    Select
            ; close button clicked
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete()
                ExitLoop
            ; minimize to system tray
            Case $msg = $GUI_EVENT_MINIMIZE
                GuiSetState(@SW_HIDE);hide GUI
                Opt("TrayIconHide", 0);show tray icon
                TraySetToolTip("Double click to restore window...");Tray icon tool tip
        EndSelect

    WEnd
EndFunc   ;==>Countgui
Edited by Mr.Furious
Link to comment
Share on other sites

The trick is to only set the text in the label when it has to be changed.

Here is one way

; Author: Mr.Furious
; Topic Title: Flickering loop - AutoIt Forums
; Post URL: http://www.autoitscript.com/forum/topic/126656-flickering-loop/page__view__findpost__p__879027

#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=smiley.ico
#AutoIt3Wrapper_outfile=Count2Date.exe
#AutoIt3Wrapper_UseX64=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Date.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Global Const $TRAY_DEFAULT = 512

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 3)
Opt("TrayIconHide", 0);show(0) or hide(1) tray icon

TraySetClick(16) ; Only secondary mouse button will show the tray menu.
$foo2 = TrayCreateItem("Exit")
$foo = TrayCreateItem("Restore Window")

TrayItemSetState(-1, $TRAY_DEFAULT)


$file = FileOpen("settings.ini", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
;$FileContents = FileRead($file)
$line1 = FileReadLine($file, 1)
$line2 = FileReadLine($file, 2)

$Name = ($line1)
$Name2 = ("Count2Date")
$FutureDate = ($line2)
; Calculate the number of ...... to given date
$iDateCalc0 = _DateDiff('w', _NowCalc(), $FutureDate)
$iDateCalc1 = _DateDiff('D', _NowCalc(), $FutureDate)
$iDateCalc2 = _DateDiff('h', _NowCalc(), $FutureDate)
$iDateCalc3 = _DateDiff('n', _NowCalc(), $FutureDate)
$iDateCalc4 = _DateDiff('s', _NowCalc(), $FutureDate)

Countgui()

Func Countgui()
    Local $widthCell, $msg, $iOldOpt

    $hGui = GUICreate($Name2, 125, 175, @DesktopWidth - 200, @DesktopHeight - 250);, BitOR($WS_POPUP, $WS_BORDER), $WS_EX_TOPMOST); will create a dialog box

    $iOldOpt = Opt("GUICoordMode", 2)

    $widthCell = 125
    $MsgLabel = GUICtrlCreateLabel($Name, 10, 15, $widthCell)
    GUICtrlSetFont($MsgLabel, 12, 400, 1)
    $MsgLabel = GUICtrlCreateLabel("COUNTDOWN", -1, 0)
    GUICtrlSetFont($MsgLabel, 12, 400, 5)
    $label0 = GUICtrlCreateLabel("Weeks:   " & $iDateCalc0, -1, 5)
    $label1 = GUICtrlCreateLabel("Days:   " & $iDateCalc1, -1, 0)
    $label2 = GUICtrlCreateLabel("Hours:   " & $iDateCalc2, -1, 0)
    $label3 = GUICtrlCreateLabel("Minutes:   " & $iDateCalc3, -1, 0)
    $label4 = GUICtrlCreateLabel("Seconds:   " & $iDateCalc4, -1, 0)

    GUISetState() ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1

        $trayMsg = TrayGetMsg()

        Select
            Case $trayMsg = $foo
                GUISetState(@SW_SHOW);show GUI
                GUISetState(@SW_RESTORE);bring GUI from minimized state
            Case $trayMsg = $foo2
                GUIDelete()
                ExitLoop
        EndSelect


        $iDateCalc0 = _DateDiff('w', _NowCalc(), $FutureDate)
        $iDateCalc1 = _DateDiff('D', _NowCalc(), $FutureDate)
        $iDateCalc2 = _DateDiff('h', _NowCalc(), $FutureDate)
        $iDateCalc3 = _DateDiff('n', _NowCalc(), $FutureDate)
        $iDateCalc4 = _DateDiff('s', _NowCalc(), $FutureDate)

        SetTextA($hGui, $label0, "Weeks:   " & $iDateCalc0)
        SetTextA($hGui, $label1, "Days:   " & $iDateCalc1)
        SetTextA($hGui, $label2, "Hours:   " & $iDateCalc2)
        SetTextA($hGui, $label3, "Minutes:   " & $iDateCalc3)
        SetTextA($hGui, $label4, "Seconds:   " & $iDateCalc4)

        $msg = GUIGetMsg()
        Select
            ; close button clicked
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete()
                ExitLoop
                ; minimize to system tray
            Case $msg = $GUI_EVENT_MINIMIZE
                GUISetState(@SW_HIDE);hide GUI
                Opt("TrayIconHide", 0);show tray icon
                TraySetToolTip("Double click to restore window...");Tray icon tool tip
        EndSelect

    WEnd
EndFunc   ;==>Countgui

Func SetTextA($hW, $labx, $sText);only set the text if it has changed
    GUISwitch($hW); only needed if there is more than one window in your script
    If GUICtrlRead($labx) <> $sText Then
        GUICtrlSetData($labx, $sText)
    EndIf

EndFunc   ;==>SetTextA
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
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
 Share

  • Recently Browsing   0 members

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