Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SingleInstance, Force
- #NoEnv
- SetBatchLines, -1
- #Include, Gdip.ahk
- If !pToken := Gdip_Startup() {
- MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
- ExitApp
- }
- DownloadFile("http://download-installer.cdn.mozilla.net/pub/firefox/releases/26.0/win32/en-US/Firefox%20Setup%2026.0.exe", "firefox_setup.exe")
- Gdip_Shutdown(pToken)
- ExitApp
- DownloadFile(UrlToFile, SaveFileAs, Overwrite := True, UseProgressBar := True) {
- ;Check if the file already exists and if we must not overwrite it
- If (!Overwrite && FileExist(SaveFileAs))
- Return
- ;Check if the user wants a progressbar
- If (UseProgressBar) {
- ;Initialize the WinHttpRequest Object
- WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
- ;Download the headers
- WebRequest.Open("HEAD", UrlToFile)
- WebRequest.Send()
- ;Store the header which holds the file size in a variable:
- FinalSize := WebRequest.GetResponseHeader("Content-Length")
- ;Create the progressbar and the timer
- Progress, H80, , Downloading..., %UrlToFile%
- circleObj := CreateCircleProgress(450,60)
- SetTimer, __UpdateProgressBar, 100
- }
- ;Download the file
- UrlDownloadToFile, %UrlToFile%, %SaveFileAs%
- ;Remove the timer and the progressbar because the download has finished
- If (UseProgressBar) {
- Progress, Off
- SetTimer, __UpdateProgressBar, Off
- DestroyCircleProgress(circleObj)
- }
- Return
- ;The label that updates the progressbar
- __UpdateProgressBar:
- ;Get the current filesize and tick
- CurrentSize := FileOpen(SaveFileAs, "r").Length ;FileGetSize wouldn't return reliable results
- CurrentSizeTick := A_TickCount
- ;Calculate the downloadspeed
- Speed := Round((CurrentSize/1024-LastSize/1024)/((CurrentSizeTick-LastSizeTick)/1000)) . " Kb/s"
- ;Save the current filesize and tick for the next time
- LastSizeTick := CurrentSizeTick
- LastSize := FileOpen(SaveFileAs, "r").Length
- ;Calculate percent done
- PercentDone := Round(CurrentSize/FinalSize*100)
- ;Update the ProgressBar
- Progress, %PercentDone%, %PercentDone%`% Done, Downloading... (%Speed%), Downloading %SaveFileAs% (%PercentDone%`%)
- UpdateCircleProgress(circleObj,percentDone)
- Return
- }
- CreateCircleProgress(diameter:=50,thickness:=5,color:=0x99009933,xPos:="center",yPos:="center",guiId:=1) {
- width := height := diameter+thickness*2
- xPos := (xPos=="center" ? A_ScreenWidth/2-diameter/2-thickness : xPos)
- yPos := (yPos=="center" ? A_ScreenHeight/2-diameter/2-thickness : yPos)
- Gui, %guiId%: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
- Gui, %guiId%: Show, NA
- hwnd := WinExist()
- hbm := CreateDIBSection(width, height)
- hdc := CreateCompatibleDC()
- obm := SelectObject(hdc, hbm)
- G := Gdip_GraphicsFromHDC(hdc)
- Gdip_SetSmoothingMode(G, 4)
- pen:=Gdip_CreatePen(color, thickness)
- Gdip_SetCompositingMode(G, 1)
- Return {hwnd:hwnd, hdc:hdc, obm:obm, hbm:hbm, pen:pen, G:G, diameter: diameter, thickness:thickness, xPos:xPos, yPos:yPos, width:width, height:height}
- }
- UpdateCircleProgress(circleObj,percent) {
- Gdip_Drawarc(circleObj.G, circleObj.pen, circleObj.thickness, circleObj.thickness, circleObj.diameter, circleObj.diameter, 270, 360/100*percent)
- UpdateLayeredWindow(circleObj.hwnd, circleObj.hdc, circleObj.xPos, circleObj.yPos, circleObj.width, circleObj.height)
- }
- DestroyCircleProgress(circleObj) {
- Gui % circleObj.hwnd ":Destroy"
- SelectObject(circleObj.hdc, circleObj.obm)
- DeleteObject(circleObj.hbm)
- DeleteDC(circleObj.hdc)
- Gdip_DeleteGraphics(circleObj.G)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement