Jump to content

Downloader child gui


Recommended Posts

Hi all!

I wrote a simple downloader with a gui.

#include <INet.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $hGUI_DL, $guiDL_CloseLabel, $guiDL_CurrentFile, $guiDL_CurrentLabel, $guiDL_MinimizeLabel, $guiDL_Progress, $guiDL_Size, $guiDL_Speed, $guiDL_Status

Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 4)

$hGUI = GUICreate("TEST", 100, 24)
GUISetOnEvent(-3, "_Quit")
GUISetOnEvent(-7, "_DragWindow")
GUICtrlCreateButton("   OK   ", 30, 0, 40)
GUICtrlSetOnEvent(-1, "DOWNLOAD")
GUISetState()
While 1
    Sleep(20)
WEnd


Func DOWNLOAD()
    $sURL = "http://www.hardwired.hu/bigdl/3/10/Batman_Arkham_Asylum_PC_muvi_gadgets.wmv"

    $hGUI_DL = GUICreate("Download", 400, 120, Default, Default, 0x80080000, Default, $hGUI)
    GUISetOnEvent(-3, "_Quit")
    GUISetOnEvent(-7, "_DragWindow")

    GUICtrlCreateLabel("Download", (400 - 72) / 2, 1, 72)
    GUICtrlSetFont(-1, 10, 800, 0, "Verdana")

    $guiDL_CloseLabel = GUICtrlCreateLabel("r", 400 - 26, 0, 20, 20, 0x01)
    GUICtrlSetOnEvent(-1, "_Quit")
    GUICtrlSetFont(-1, 12, 600, 0, "Webdings")

    $guiDL_Size = GUICtrlCreateLabel("0.0/0.0 MB (0.0%)", 154, 30, 400 - 164, 20, 0x02)
    $guiDL_CurrentFile = GUICtrlCreateLabel("", 10, 50, 380, 20)
    $guiDL_Progress = GUICtrlCreateProgress(10, 70, 380, 20)
    $guiDL_Speed = GUICtrlCreateLabel("Speed: 0 kB/s", 290, 96, 100, 20, 0x02)
    $guiDL_Status = GUICtrlCreateLabel("Status: Waiting for link... (0 seconds remaining)", 10, 96, 280, 20)

    GUISetState()

    _SimpleDownloader($sURL, "C:\Downloads")

    GUIDelete($hGUI_DL)
EndFunc   ;==>DOWNLOAD


#Region _Quit()
Func _Quit()
    Exit
EndFunc   ;==>_Quit
#EndRegion _Quit()

Func _DragWindow()
    DllCall("user32.dll", "int", "SendMessage", "hWnd", WinGetHandle("[ACTIVE]"), "int", 0xA1, "int", 2, "int", 0) ;$WM_NCLBUTTONDOWN, $HTCAPTION
EndFunc   ;==>_DragWindow

#Region _SimpleDownloader($sURL, $sDownloadDir)
Func _SimpleDownloader($sURL, $sDownloadDir)
    If StringRight($sDownloadDir, 1) <> "\" Then $sDownloadDir &= "\"

    GUICtrlSetData($guiDL_Status, "Status: Downloading...")
    InetGet($sURL, $sDownloadDir & StringRegExpReplace($sURL, "http://.*/(.*)", "$1"), 0, 1)
    $iSize = InetGetSize($sURL)
    Local $iSpeed, $iLastSpeed
    While @InetGetActive
        $iSpeed = @InetGetBytesRead
        ;Download GUI Code
        ;ConsoleWrite(@HOUR&":"&@MIN&":"&@SEC&"."&@MSEC&" > "&$iSpeed&@CR)
        GUICtrlSetData($guiDL_Size, StringFormat("%.2f/%.2f MB (%.1f%%)", $iSpeed / 1024 / 1024, $iSize / 1024 / 1024, $iSpeed / $iSize * 100))
        GUICtrlSetData($guiDL_Progress, $iSpeed / $iSize * 100)
        GUICtrlSetData($guiDL_Speed, "Speed: " & StringFormat("%.0f kB/s", ($iSpeed - $iLastSpeed) / 1024))
        $iLastSpeed = $iSpeed
        Sleep(1000)
    WEnd
EndFunc   ;==>_SimpleDownloader
#EndRegion _SimpleDownloader($sURL, $sDownloadDir)

But if I click on the close button, or try to move the child gui (using onevent functions) it only reacts at the end of the download.

I know the reason is because of the "While @InetGetActive" loop but I have no idea how to make it work within the loop. Any suggest?

Edited by Izebize
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...