Jump to content

Enable / Disable GUICtrlCreateMenuItem with file download


DigDeep
 Share

Recommended Posts

Hi,

I have a file which is of huge size and I need it to start the download when the GUI is launched and keep downloading while GUI up. I don't want the GUI to wait for the download to be completed and then launch. Once the download is completed, $OpenMenu should be auto enabled.

But with the below way, even if the file is downloaded after GUI is launched the $OpenMenu remains disabled. That might be the reason I am using a single check when GUI is getting launched.

Is there a way when application is initiated:

1. File will start downloading

2. GUI will launch as per normal time and disable the $OpenMenu if file does not exist.

3. Once the download is completed, $Open menu will auto enable.

I have given VLC player download as a sample here...

#RequireAdmin

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Inetget('http://mirror.amberit.com.bd/videolan/vlc/3.0.1/win32/vlc-3.0.1-win32.exe', @DesktopDir & '\vlc.exe', 1, 1)

$Form1 = GUICreate("Form1", 198, 220, 520, 224)
$MenuItem1 = GUICtrlCreateMenu("File")

Local $VLCLocal = @DesktopDir & '\vlc.exe'
If Not FileExists($VLCLocal) Then
$OpenMenu = GUICtrlCreateMenuItem("Open", $MenuItem1)
GUICtrlSetState($OpenMenu, $GUI_DISABLE) ; Disable the menu until file is downloaded
Else
GUICtrlSetState($OpenMenu, $GUI_ENABLE) ; Enable the menu when file is downloaded
EndIf

$ExitMenu = GUICtrlCreateMenuItem("Exit", $MenuItem1)
$ToolsMenu = GUICtrlCreateMenu("Tools")
$AboutMenu = GUICtrlCreateMenu("About")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

 

 

Link to comment
Share on other sites

This works for me.

#RequireAdmin

#include <GUIConstantsEx.au3>
#include <InetConstants.au3>
#include <WindowsConstants.au3>

Local $VLCLocal = @DesktopDir & '\vlc.exe'
Global $_g_hVLCRemote = 0
Global $_g_sVLCUrl = 'http://mirror.vorboss.net/videolan/vlc/3.0.1/win32/vlc-3.0.1-win32.exe' ; changed for faster download

$Form1 = GUICreate("Form1", 198, 220, 520, 224)
$MenuItem1 = GUICtrlCreateMenu("File")
$OpenMenu = GUICtrlCreateMenuItem("Open", $MenuItem1)
$ExitMenu = GUICtrlCreateMenuItem("Exit", $MenuItem1)
$ToolsMenu = GUICtrlCreateMenu("Tools")
$AboutMenu = GUICtrlCreateMenu("About")

; download if file wasn't found or file size mismatch
If Not FileExists($VLCLocal) Or FileGetSize($VLCLocal) <> InetGetSize($_g_sVLCUrl) Then VLC_Download()

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OpenMenu
            Run($VLCLocal)
    EndSwitch
WEnd

Func VLC_Download()
    GUICtrlSetState($OpenMenu, $GUI_DISABLE) ; Disable the menu until file is downloaded
    $_g_hVLCRemote = InetGet($_g_sVLCUrl, $VLCLocal, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
    AdlibRegister('VLC_CheckForFile', 2000) ; check for downloaded file
EndFunc   ;==>VLC_Download

Func VLC_CheckForFile()
    If InetGetInfo($_g_hVLCRemote, $INET_DOWNLOADCOMPLETE) Then
        GUICtrlSetState($OpenMenu, $GUI_ENABLE)
        AdlibUnRegister('VLC_CheckForFile') ; stop checking
    ElseIf InetGetInfo($_g_hVLCRemote, $INET_DOWNLOADERROR) Then
        ; add some feedback for failed download here, if required
        GUICtrlSetState($OpenMenu, $GUI_DISABLE)
        AdlibUnRegister('VLC_CheckForFile') ; stop checking
    EndIf
EndFunc   ;==>VLC_CheckForFile

 

Link to comment
Share on other sites

As Bilgus mentioned.  You can have the menu text display what you want. It could also be changed if an error occurred with the download or just be left disabled

#RequireAdmin

#include <GUIConstantsEx.au3>
#include <InetConstants.au3>
#include <WindowsConstants.au3>

Local $VLCLocal = @DesktopDir & '\vlc.exe'
Global $_g_hVLCRemote = 0

$Form1 = GUICreate("Form1", 198, 220, 520, 224)
$MenuItem1 = GUICtrlCreateMenu("File")
$OpenMenu = GUICtrlCreateMenuItem("Open", $MenuItem1)
GUICtrlSetState(-1, $GUI_DISABLE)
$ExitMenu = GUICtrlCreateMenuItem("Exit", $MenuItem1)
$ToolsMenu = GUICtrlCreateMenu("Tools")
$AboutMenu = GUICtrlCreateMenu("About")

VLC_Download()

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OpenMenu
            Run($VLCLocal)
    EndSwitch
WEnd

Func VLC_Download()
    Local $_g_sVLCUrl = 'http://mirror.vorboss.net/videolan/vlc/3.0.1/win32/vlc-3.0.1-win32.exe' ; changed for faster download

    ; download if file wasn't found or file size mismatch
    If Not FileExists($VLCLocal) Or FileGetSize($VLCLocal) <> InetGetSize($_g_sVLCUrl) Then
;~      GUICtrlSetData($OpenMenu, 'Downloading...')
;~      GUICtrlSetData($OpenMenu, 'Open - Downloading..')
        GUICtrlSetData($OpenMenu, 'Open - in progress')
        GUICtrlSetState($OpenMenu, $GUI_DISABLE) ; Disable the menu until file is downloaded
        $_g_hVLCRemote = InetGet($_g_sVLCUrl, $VLCLocal, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
        AdlibRegister('VLC_CheckForFile', 2000) ; check for downloaded file
    Else
        GUICtrlSetData($OpenMenu, 'Open')
        GUICtrlSetState($OpenMenu, $GUI_ENABLE) ; Disable the menu until file is downloaded
    EndIf
EndFunc   ;==>VLC_Download

Func VLC_CheckForFile()
    If InetGetInfo($_g_hVLCRemote, $INET_DOWNLOADCOMPLETE) Then
        GUICtrlSetData($OpenMenu, 'Open')
        GUICtrlSetState($OpenMenu, $GUI_ENABLE)
        AdlibUnRegister('VLC_CheckForFile') ; stop checking
    ElseIf InetGetInfo($_g_hVLCRemote, $INET_DOWNLOADERROR) Then
        ; add some feedback for failed download here, if required
        GUICtrlSetData($OpenMenu, 'Open')
        GUICtrlSetState($OpenMenu, $GUI_DISABLE)
        AdlibUnRegister('VLC_CheckForFile') ; stop checking
    EndIf
EndFunc   ;==>VLC_CheckForFile

 

Link to comment
Share on other sites

Or if you wanted some feedback on the download progress you could use something like this

#RequireAdmin

#include <GUIConstantsEx.au3>
#include <InetConstants.au3>
#include <WindowsConstants.au3>

Local $VLCLocal = @DesktopDir & '\vlc.exe'
Global $_g_hVLCRemote = 0

$Form1 = GUICreate("Form1", 198, 220, 520, 224)
$MenuItem1 = GUICtrlCreateMenu("File")
$OpenMenu = GUICtrlCreateMenuItem("Open", $MenuItem1)
GUICtrlSetState(-1, $GUI_DISABLE)
$ExitMenu = GUICtrlCreateMenuItem("Exit", $MenuItem1)
$ToolsMenu = GUICtrlCreateMenu("Tools")
$AboutMenu = GUICtrlCreateMenu("About")

VLC_Download()

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OpenMenu
            Run($VLCLocal)
    EndSwitch
WEnd

Func VLC_Download()
    Local $_g_sVLCUrl = 'http://mirror.vorboss.net/videolan/vlc/3.0.1/win32/vlc-3.0.1-win32.exe' ; changed for faster download

    ; download if file wasn't found or file size mismatch
    If Not FileExists($VLCLocal) Or FileGetSize($VLCLocal) <> InetGetSize($_g_sVLCUrl) Then
        $_g_hVLCRemote = InetGet($_g_sVLCUrl, $VLCLocal, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
        AdlibRegister('VLC_CheckForFile', 500) ; check for downloaded file
    Else
        GUICtrlSetData($OpenMenu, 'Open')
        GUICtrlSetState($OpenMenu, $GUI_ENABLE) ; Disable the menu until file is downloaded
    EndIf
EndFunc   ;==>VLC_Download

Func VLC_CheckForFile()
    ; get the details of the download
    Local $av_Info = InetGetInfo($_g_hVLCRemote)

    ; download completed successfully
    If $av_Info[$INET_DOWNLOADCOMPLETE] And $av_Info[$INET_DOWNLOADSUCCESS] Then
        GUICtrlSetState($OpenMenu, $GUI_ENABLE)
    Elseif $av_Info[$INET_DOWNLOADERROR] Then
        GUICtrlSetState($OpenMenu, $GUI_DISABLE)
    Else ; assume download is in progress
        GUICtrlSetData($OpenMenu, 'Downloading ' & Round($av_Info[$INET_DOWNLOADREAD] / $av_Info[$INET_DOWNLOADSIZE] * 100) & '%')
        GUICtrlSetState($OpenMenu, $GUI_DISABLE)
        Return
    EndIf

    GUICtrlSetData($OpenMenu, 'Open')
    AdlibUnRegister('VLC_CheckForFile') ; stop checking
EndFunc   ;==>VLC_CheckForFile

 

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...