Jump to content

Progress Bar with changing color


Lemmens Peter
 Share

Recommended Posts

The progressbar starts counting down when the autoit-window has NO focus. (The autoit-window is not active, another window is active)

When the autoit-window get the focus (Window is activated) the progressbar start all over (Starts from 100% again)

The progressbar changes color when :

higher than 66% : green

between 33% and 66% : yellow

lower than 33% : red

The window flashes 3 times when passing 66% and 33%

The autoit-window closes when it reaches 0 %

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.4.0
Author:      myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------

; #include <GUIConstantsEx.au3>
Global Const $GUI_HIDE = 32
Global Const $GUI_SHOW = 16
Global Const $GUI_EVENT_CLOSE = -3
; #include <ProgressConstants.au3>
Global Const $PBS_VERTICAL = 0x04
Global Const $PBM_SETSTATE = 0x0410
; #Include <WinAPI.au3>
Global Const $tagFLASHWINFO = "uint Size;hwnd hWnd;dword Flags;uint Count;dword TimeOut"
Global Const $__WINAPICONSTANT_FLASHW_CAPTION = 0x00000001
Global Const $__WINAPICONSTANT_FLASHW_TRAY  = 0x00000002
Global Const $__WINAPICONSTANT_FLASHW_TIMER  = 0x00000004
Global Const $__WINAPICONSTANT_FLASHW_TIMERNOFG = 0x0000000C

Dim $o_Percentage

$Form1 = GUICreate("Progress Bar Exemple", 150, 450, -1, -1)
$Progress1 = GUICtrlCreateProgress(10, 10, 10, 400, $PBS_VERTICAL)
$Label1 = GUICtrlCreateLabel ( "---------------", 50, 225)
GUICtrlSetFont ($Label1, 12, 600)
GUISetState(@SW_SHOW)
GUICtrlSetState($Progress1, $GUI_HIDE)
$Start_Time = TimerInit()
$Time_To_Wait = 30000 ; miliseconden
GUICtrlSetData($Progress1, 100)
Sleep(500)
GUICtrlSetState($Progress1, $GUI_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
Sleep(10)
If WinActive($Form1) Then
  $Start_Time = TimerInit()
EndIf
$Current_Time_Difference = TimerDiff($Start_Time)
If $Current_Time_Difference > $Time_To_Wait Then
  ;ProgressSet(100, "Done!")
  Sleep(750)
  ;ProgressOff()
  Exit
Else
  $Percentage = 100 - Round((100/$Time_To_Wait) * $Current_Time_Difference)
  GuiCtrlSetData($Label1, String($Percentage) & " %")
  GUICtrlSetData($Progress1, $Percentage)
  If $o_Percentage >= 100 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 1) ; green
  EndIf
  If $o_Percentage >= 66 and $Percentage < 66 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 3) ; yellow
   _WinAPI_FlashWindowEx($Form1, 3, 3, 200)
  EndIf
  If $o_Percentage >= 33 and $Percentage < 33 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 2) ; red
   _WinAPI_FlashWindowEx($Form1, 3, 3, 100)
  EndIf
  $o_Percentage = $Percentage
EndIf
WEnd

Func _SendMessage($hWnd, $iMsg, $wParam = 0, $lParam = 0, $iReturn = 0, $wParamType = "wparam", $lParamType = "lparam", $sReturnType = "lresult")
Local $aResult = DllCall("user32.dll", $sReturnType, "SendMessageW", "hwnd", $hWnd, "uint", $iMsg, $wParamType, $wParam, $lParamType, $lParam)
If @error Then Return SetError(@error, @extended, "")
If $iReturn >= 0 And $iReturn <= 4 Then Return $aResult[$iReturn]
Return $aResult
EndFunc   ;==>_SendMessage
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_FlashWindowEx
; Description ...: Flashes the specified window
; Syntax.........: _WinAPI_FlashWindowEx($hWnd[, $iFlags = 3[, $iCount = 3[, $iTimeout = 0]]])
; Parameters ....: $hWnd        - Handle to the window to be flashed. The window can be either open or minimized.
;                 $iFlags     - The flash status. Can be one or more of the following values:
;                 |0 - Stop flashing. The system restores the window to its original state.
;                 |1 - Flash the window caption
;                 |2 - Flash the taskbar button
;                 |4 - Flash continuously until stopped
;                 |8 - Flash continuously until the window comes to the foreground
;                 $iCount     - The number of times to flash the window
;                 $iTimeout - The rate at which the window is to be flashed, in  milliseconds.  If 0, the function  uses  the
;                 +default cursor blink rate.
; Return values .: Success    - True
;                 Failure     - False
; Author ........: Yoan Roblet (arcker)
; Modified.......:
; Remarks .......: Typically, you flash a window to inform the user that the window requires attention  but  does  not  currently
;                 have the keyboard focus.  When a window flashes, it appears to change  from  inactive  to  active  status.  An
;                 inactive caption bar changes to an active caption bar; an active caption bar changes to  an  inactive  caption
;                 bar.
; Related .......: _WinAPI_FlashWindow
; Link ..........: @@MsdnLink@@ FlashWindowEx
; Example .......: Yes
; ===============================================================================================================================
Func _WinAPI_FlashWindowEx($hWnd, $iFlags = 3, $iCount = 3, $iTimeout = 0)
Local $tFlash = DllStructCreate($tagFLASHWINFO)
Local $pFlash = DllStructGetPtr($tFlash)
Local $iFlash = DllStructGetSize($tFlash)
Local $iMode = 0
If BitAND($iFlags, 1) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_CAPTION)
If BitAND($iFlags, 2) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TRAY)
If BitAND($iFlags, 4) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TIMER)
If BitAND($iFlags, 8) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TIMERNOFG)
DllStructSetData($tFlash, "Size", $iFlash)
DllStructSetData($tFlash, "hWnd", $hWnd)
DllStructSetData($tFlash, "Flags", $iMode)
DllStructSetData($tFlash, "Count", $iCount)
DllStructSetData($tFlash, "Timeout", $iTimeout)
Local $aResult = DllCall("user32.dll", "bool", "FlashWindowEx", "ptr", $pFlash)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc   ;==>_WinAPI_FlashWindowEx

I use indeed Windows 7 and the progressbar changes color !

@fett8802 and wakillon : Which OS do you use ?

Edited by Lemmens Peter
Link to comment
Share on other sites

The SendMessage message used only works on Vista and above, I don't think it works on XP

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

Link to comment
Share on other sites

To view three color of PB you have to displace the TimerInit from While loop:

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.4.0
Author:   myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; #include <GUIConstantsEx.au3>
Global Const $GUI_HIDE = 32
Global Const $GUI_SHOW = 16
Global Const $GUI_EVENT_CLOSE = -3
; #include <ProgressConstants.au3>
Global Const $PBS_VERTICAL = 0x04
Global Const $PBM_SETSTATE = 0x0410
; #Include <WinAPI.au3>
Global Const $tagFLASHWINFO = "uint Size;hwnd hWnd;dword Flags;uint Count;dword TimeOut"
Global Const $__WINAPICONSTANT_FLASHW_CAPTION = 0x00000001
Global Const $__WINAPICONSTANT_FLASHW_TRAY  = 0x00000002
Global Const $__WINAPICONSTANT_FLASHW_TIMER  = 0x00000004
Global Const $__WINAPICONSTANT_FLASHW_TIMERNOFG = 0x0000000C
Dim $o_Percentage
$Form1 = GUICreate("Progress Bar Exemple", 150, 450, -1, -1)
$Progress1 = GUICtrlCreateProgress(10, 10, 10, 400, $PBS_VERTICAL)
$Label1 = GUICtrlCreateLabel ( "---------------", 50, 225)
GUICtrlSetFont ($Label1, 12, 600)
GUISetState(@SW_SHOW)
GUICtrlSetState($Progress1, $GUI_HIDE)
$Start_Time = TimerInit()
$Time_To_Wait = 30000 ; miliseconden
GUICtrlSetData($Progress1, 100)
Sleep(500)
GUICtrlSetState($Progress1, $GUI_SHOW)
If WinActive($Form1) Then
  $Start_Time = TimerInit()
EndIf
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
Sleep(10)
;If WinActive($Form1) Then
;  $Start_Time = TimerInit()
;EndIf
$Current_Time_Difference = TimerDiff($Start_Time)
If $Current_Time_Difference > $Time_To_Wait Then
  ;ProgressSet(100, "Done!")
  Sleep(750)
  ;ProgressOff()
  Exit
Else
  $Percentage = 100 - Round((100/$Time_To_Wait) * $Current_Time_Difference)
  GuiCtrlSetData($Label1, String($Percentage) & " %")
  GUICtrlSetData($Progress1, $Percentage)
  If $o_Percentage >= 100 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 1) ; green
  EndIf
  If $o_Percentage >= 66 and $Percentage < 66 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 3) ; yellow
   _WinAPI_FlashWindowEx($Form1, 3, 3, 200)
  EndIf
  If $o_Percentage >= 33 and $Percentage < 33 Then
   _SendMessage(GUICtrlGetHandle($Progress1), $PBM_SETSTATE, 2) ; red
   _WinAPI_FlashWindowEx($Form1, 3, 3, 100)
  EndIf
  $o_Percentage = $Percentage
EndIf
WEnd
Func _SendMessage($hWnd, $iMsg, $wParam = 0, $lParam = 0, $iReturn = 0, $wParamType = "wparam", $lParamType = "lparam", $sReturnType = "lresult")
Local $aResult = DllCall("user32.dll", $sReturnType, "SendMessageW", "hwnd", $hWnd, "uint", $iMsg, $wParamType, $wParam, $lParamType, $lParam)
If @error Then Return SetError(@error, @extended, "")
If $iReturn >= 0 And $iReturn <= 4 Then Return $aResult[$iReturn]
Return $aResult
EndFunc   ;==>_SendMessage
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_FlashWindowEx
; Description ...: Flashes the specified window
; Syntax.........: _WinAPI_FlashWindowEx($hWnd[, $iFlags = 3[, $iCount = 3[, $iTimeout = 0]]])
; Parameters ....: $hWnd        - Handle to the window to be flashed. The window can be either open or minimized.
;                $iFlags     - The flash status. Can be one or more of the following values:
;                |0 - Stop flashing. The system restores the window to its original state.
;                |1 - Flash the window caption
;                |2 - Flash the taskbar button
;                |4 - Flash continuously until stopped
;                |8 - Flash continuously until the window comes to the foreground
;                $iCount     - The number of times to flash the window
;                $iTimeout - The rate at which the window is to be flashed, in  milliseconds.  If 0, the function  uses  the
;                +default cursor blink rate.
; Return values .: Success    - True
;                Failure     - False
; Author ........: Yoan Roblet (arcker)
; Modified.......:
; Remarks .......: Typically, you flash a window to inform the user that the window requires attention  but  does  not  currently
;                have the keyboard focus.  When a window flashes, it appears to change  from  inactive  to  active  status.  An
;                inactive caption bar changes to an active caption bar; an active caption bar changes to  an  inactive  caption
;                bar.
; Related .......: _WinAPI_FlashWindow
; Link ..........: @@MsdnLink@@ FlashWindowEx
; Example .......: Yes
; ===============================================================================================================================
Func _WinAPI_FlashWindowEx($hWnd, $iFlags = 3, $iCount = 3, $iTimeout = 0)
Local $tFlash = DllStructCreate($tagFLASHWINFO)
Local $pFlash = DllStructGetPtr($tFlash)
Local $iFlash = DllStructGetSize($tFlash)
Local $iMode = 0
If BitAND($iFlags, 1) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_CAPTION)
If BitAND($iFlags, 2) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TRAY)
If BitAND($iFlags, 4) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TIMER)
If BitAND($iFlags, 8) <> 0 Then $iMode = BitOR($iMode, $__WINAPICONSTANT_FLASHW_TIMERNOFG)
DllStructSetData($tFlash, "Size", $iFlash)
DllStructSetData($tFlash, "hWnd", $hWnd)
DllStructSetData($tFlash, "Flags", $iMode)
DllStructSetData($tFlash, "Count", $iCount)
DllStructSetData($tFlash, "Timeout", $iTimeout)
Local $aResult = DllCall("user32.dll", "bool", "FlashWindowEx", "ptr", $pFlash)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc   ;==>_WinAPI_FlashWindowEx

The point of world view

Link to comment
Share on other sites

The whole point of having the WinActive in the While/Wend loop was so that it would reset the timer if the window gets focus. The progress bar would only change if the windows DOESN'T have focus in the original script, which was the author's original concept.

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

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

×
×
  • Create New...