Like several people, I've come to wish that ProgressOn had a cancel/close button, but alas, it does not. I have a rather complex Form w/ multiple objects/tabs/etc and having a GUICtrlCreateProgress in the same form isn't ideal, so I was attempting to bring up a second GUI window just to show the progress, and then have it close.
For this test I was attempting, I used guiness' _InetGetGUI UDF with some obvious modifications attempting to get it to work with 2 seperate GUI's.
Here's what I have so far:
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> main() Func main() $Form = GUICreate("Download Test Using 2 GUIs", 372, 56, -1, 116) $Button1 = GUICtrlCreateButton("Download", 40, 8, 299, 41) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 GUISetState(@SW_DISABLE, $Form) _progress() GUISetState(@SW_ENABLE, $Form) EndSwitch WEnd EndFunc Func _progress() Local $sFilePathURL = "http://ftp.opera.com/pub/opera/win/1152/en/Opera_1152_en_Setup.exe" Local $hGUI, $iButton, $iLabel, $iProgressBar, $sFilePath $hGUI = GUICreate("_InetGetGUI()", 370, 90, -1, 0) $iLabel = GUICtrlCreateLabel("", 5, 5, 270, 40) $iButton = GUICtrlCreateButton("&Cancel", 275, 2.5, 90, 25) $iProgressBar = GUICtrlCreateProgress(5, 60, 360, 20) GUISetState(@SW_SHOW, $hGUI) $sDirectory = @ScriptDir $sFilePath = _InetGetGUI($sFilePathURL, $iLabel, $iProgressBar, $iButton, $sDirectory, $hGUI) #cs While 1 <==================== don't think I need this section Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUIDelete($hGUI) Case $iButton GUIDelete($hGUI) EndSwitch WEnd #ce EndFunc Func _InetGetGUI($sURL, $iLabel, $iProgress, $iButton, $sDirectory, $hGUI) Local $hDownload, $iBytesRead = 0, $iFileSize, $iPercentage, $iSpeed = 0, $iTimer = 0, $sFilePath, $sProgressText, $sSpeed $sFilePath = StringRegExpReplace($sURL, "^.*/", "") $sDirectory = StringRegExpReplace($sDirectory, "[\\/]+\z", "") & "\" & $sFilePath $iFileSize = InetGetSize($sURL, 1) $hDownload = InetGet($sURL, $sDirectory, 0, 1) $sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s" $iTimer = TimerInit() While InetGetInfo($hDownload, 2) = 0 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $iButton MsgBox(48,"","Download Cancelled") ExitLoop EndSwitch $iBytesRead = InetGetInfo($hDownload, 0) $iPercentage = $iBytesRead * 100 / $iFileSize $sProgressText = "Downloading " & _ByteSuffix($iBytesRead, 0) & " Of " & _ByteSuffix($iFileSize, 0) & @LF & $sSpeed GUICtrlSetData($iLabel, $sProgressText) GUICtrlSetData($iProgress, $iPercentage) If TimerDiff($iTimer) > 1000 Then $sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s" $iSpeed = $iBytesRead $iTimer = TimerInit() EndIf Sleep(100) WEnd InetClose($hDownload) GUIDelete($hGUI) ;main() <======== closest to a solution that I've found, but it launches multiple instances of $Form which is not desirable EndFunc Func _ByteSuffix($iBytes, $iRound = 2) Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"] While $iBytes > 1023 $A += 1 $iBytes /= 1024 WEnd Return Round($iBytes, $iRound) & $aArray[$A] EndFunc
It's semi-functional at best, the only way I can get it to stay open is using the commented out portion in _InetGetGUI - which launches multiple instances of $Form. I have a feeling that I'm close to a solution, but after multiple attemps I still can't get it to work as intended.