Jump to content

Favorite menu


jalexj
 Share

Recommended Posts

Hi,

I want to make a menu that could launch anything in a directory.

For the moment the menu list files in my directory.

; Menu User
$UserMenu = GuiCtrlCreateMenu ("User")
$FileList = _FileListToArray(@ScriptDir & "\user\Liens")
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$nb_FileList = $FileList[0] - 1
_ArrayDelete($FileList,0)
_ArraySort($FileList); Sort Ascending
Global $ItemUserMenu[$nb_FileList + 1]
For $i = 0 To $nb_FileList
    $ItemUserMenu[$i] = GuiCtrlCreateMenuitem (StringReplace($FileList[$i],StringRight($FileList[$i],4),""),$UserMenu)
    GUICtrlSetOnEvent(-1,execute_menu($i))
Next
      Func execute_menu($i)
        MsgBox(1,"test","index " & GUICtrlRead ($ItemUserMenu[$i]) & " clicked")
    EndFunc

This code isn't working.

==> Unknown function name.:

GUICtrlSetOnEvent(-1,execute_menu($i))

I don't know how can i pass a variable to a function with GUICtrlSetOnEvent

Please, could someone tell me whats wrong,

THX

Edited by jalexj
Link to comment
Share on other sites

This code isn't working. Could someone tell me what's wrong.

Your problem description is too vague to provide meaningful help w/o trying to run your code and divine your desired intentions. Try to phrase your request for help w/ more specifics, like I expected this variable to contain X, and it is still empty. Ensure that the UDFs that you are calling are being called correctly and that you understand how they are accomplishing their task. Add debug lines or msgboxes to each step where you are getting unexpected results. Note that GUI.... function calls require that a GUI be created - the menu can't exist on its own. Refer to the GUI helpfile for add'l details.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Sorry, here is a working code that create a menu that list my links:

#include <GuiConstants.au3>
#include <file.au3>
#include <Array.au3>
#Include <process.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1); Change to OnEvent mode 
$Titre="Favorites menu"

; GUI
$mainwindow=GuiCreate($Titre, 300, 520, 30)
GuiSetIcon(@SystemDir & "\shell32.dll", 17)
; Menu User
$UserMenu = GuiCtrlCreateMenu ("Favorites")
$FileList = _FileListToArray(@ScriptDir & "\links")
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$nb_FileList = $FileList[0] - 1
_ArrayDelete($FileList,0)
_ArraySort($FileList); Sort Ascending
Dim $ItemUserMenu[$nb_FileList + 1]
For $i = 0 To $nb_FileList
    $ItemUserMenu[$i] = GuiCtrlCreateMenuitem (StringReplace($FileList[$i],StringRight($FileList[$i],4),""),$UserMenu)
;~   GUICtrlSetOnEvent(-1,execute_menu($i))
Next
Func execute_menu($i)
;~   MsgBox(1,"test","index " & GUICtrlRead ($ItemUserMenu[$i]) & " clicked")
EndFunc

Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then
        SetError(1)
        Return ""
    EndIf
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8)="") Then
        SetError(2)
    Return ""
    EndIf
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then
        SetError(3)
        Return ""
    EndIf
    $asFileList[0] = 0
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch=-1 then 
    SetError(0)
    Return ""
    EndIf
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If $iFlag = 1 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        EndIf
        If $iFlag = 2 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        EndIf
        If $sFile = "." Or $sFile = ".." Then ContinueLoop
        ReDim $asFileList[UBound($asFileList) + 1]
        $asFileList[0] = $asFileList[0] + 1
        $asFileList[UBound($asFileList) - 1] = $sFile
    WEnd
    FileClose($hSearch)
    SetError(0)
    If $asFileList[0] = 0 Then Return ""
    Return $asFileList
EndFunc

GUISetState(); display the GUI

GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")
Func OnExit()
    Exit
EndFunc

While 1
  Sleep(1000); Idle around
WEnd

My problem is that i want "execute_menu" function to launch the files, with a "RunWait(@COMSPEC & " /c Start " & $file)" for exemple.

I don't know how to do this, could someone help me?

favorites_menu.zip

Edited by jalexj
Link to comment
Share on other sites

I don't understand what's wrong with this line:

GUICtrlSetOnEvent(-1,execute_menu($FileList[$i]))

execute_menu is launch once and on second loop the debugger tells unknow fonction name.

Here's the code:

#include <GuiConstants.au3>
#include <file.au3>
#include <Array.au3>
#Include <process.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 
$Titre="Favorites menu"

; GUI
$mainwindow=GuiCreate($Titre, 300, 520, 30)
GuiSetIcon(@SystemDir & "\shell32.dll", 17)
; Menu User
$UserMenu = GuiCtrlCreateMenu ("Favorites")
$FileList = _FileListToArray(@ScriptDir & "\links")
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$nb_FileList = $FileList[0] - 1
_ArrayDelete($FileList,0)
_ArraySort($FileList); Sort Ascending
Dim $ItemUserMenu[$nb_FileList + 1]
For $i = 0 To $nb_FileList
    $ItemUserMenu[$i] = GuiCtrlCreateMenuitem (StringReplace($FileList[$i],StringRight($FileList[$i],4),""),$UserMenu)
      GUICtrlSetOnEvent(-1,execute_menu($FileList[$i]))
;~ MsgBox(1,"test","index " & $ItemUserMenu[$i] & " clicked")
Next
Func execute_menu(ByRef $file)
    MsgBox(1,"test","index " & $file & " clicked")
EndFunc

Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then
        SetError(1)
        Return ""
    EndIf
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8)="") Then
        SetError(2)
    Return ""
    EndIf
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then
        SetError(3)
        Return ""
    EndIf
    $asFileList[0] = 0
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch=-1 then 
    SetError(0)
    Return ""
    EndIf
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If $iFlag = 1 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        EndIf
        If $iFlag = 2 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        EndIf
        If $sFile = "." Or $sFile = ".." Then ContinueLoop
        ReDim $asFileList[UBound($asFileList) + 1]
        $asFileList[0] = $asFileList[0] + 1
        $asFileList[UBound($asFileList) - 1] = $sFile
    WEnd
    FileClose($hSearch)
    SetError(0)
    If $asFileList[0] = 0 Then Return ""
    Return $asFileList
EndFunc

GUISetState() ; display the GUI

GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")
Func OnExit()
    Exit
EndFunc

While 1
  Sleep(1000) ; Idle around
WEnd

How can i pass my items variables to my function?

Link to comment
Share on other sites

i start with

Dim $file

; later

$file = Array[$x]

run_prog()

func Run_prog()

Run(@COMSPEC & " /c Start " & $file)

endfunc

by the way, your posted runwait should work, or you can use run

( does not wait for the ran program to exit, like runwait does )

ALSO

GUICtrlSetOnEvent(-1,execute_menu($FileList[$i]))

will not work... try dim or Global for your passing $variables

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

i start with

Dim $file

; later

$file = Array[$x]

run_prog()

func Run_prog()

Run(@COMSPEC & " /c Start " & $file)

endfunc

by the way, your posted runwait should work, or you can use run

( does not wait for the ran program to exit, like runwait does )

ALSO

GUICtrlSetOnEvent(-1,execute_menu($FileList[$i]))

will not work... try dim or Global for your passing $variables

8)

THX, but how can i dynamically run the file corresponding to the item clicked?

I can pass variable to my function but my function doesn't know which item is clicked!

Link to comment
Share on other sites

Yes, i've found! It works!

#include <GuiConstants.au3>
#include <file.au3>
#include <Array.au3>
#Include <process.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 
$Titre="Favorites menu"

; GUI
$mainwindow=GuiCreate($Titre, 300, 520, 30)
GuiSetIcon(@SystemDir & "\shell32.dll", 17)
; Menu User
$UserMenu = GuiCtrlCreateMenu ("Favorites")
$Links_path = @ScriptDir & "\links"
$FileList = _FileListToArray($Links_path)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
$nb_FileList = $FileList[0] - 1
_ArrayDelete($FileList,0)
_ArraySort($FileList); Sort Ascending
Dim $ItemUserMenu[$nb_FileList + 1]
For $i = 0 To $nb_FileList
    $ItemUserMenu[$i] = GuiCtrlCreateMenuitem (StringReplace($FileList[$i],StringRight($FileList[$i],4),""),$UserMenu)
      GUICtrlSetOnEvent(-1,"Run_prog")
Next
func Run_prog()
    $j = @GUI_CtrlId-$i-1
    Run(@COMSPEC & ' /c Start "Running..." "' & $FileList[$j] & '" /HIGH', $Links_path, @SW_HIDE)
endfunc
Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then
        SetError(1)
        Return ""
    EndIf
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8)="") Then
        SetError(2)
    Return ""
    EndIf
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then
        SetError(3)
        Return ""
    EndIf
    $asFileList[0] = 0
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch=-1 then 
    SetError(0)
    Return ""
    EndIf
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If $iFlag = 1 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        EndIf
        If $iFlag = 2 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        EndIf
        If $sFile = "." Or $sFile = ".." Then ContinueLoop
        ReDim $asFileList[UBound($asFileList) + 1]
        $asFileList[0] = $asFileList[0] + 1
        $asFileList[UBound($asFileList) - 1] = $sFile
    WEnd
    FileClose($hSearch)
    SetError(0)
    If $asFileList[0] = 0 Then Return ""
    Return $asFileList
EndFunc

GUISetState() ; display the GUI

GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")
Func OnExit()
    Exit
EndFunc

While 1
  Sleep(1000) ; Idle around
WEnd

favorites_menu.zip

Link to comment
Share on other sites

Hi,

I want to make a menu that could launch anything in a directory.

For the moment the menu list files in my directory.

...

THX

Hi, i made something similar in the past, but i lost attention on it so i never finished it.

it searches for exe-files (change it to your needs) and shows them in a small gui. you can launch them from buttons. If the exe has an icon in it, it will be shown too. it`s another attempt than yours, but perhaps it can help you

#include <GUIConstants.au3>
#include <Array.au3>

$a = _FileSearch("*.exe",1)
If $a[0] > 0 Then
    _ArraySort ($a)
EndIf
$but_height = 20
$but_width = 125
$button_in_a_row = 20

$tool_label_left = 10
$rows = ""
get_rows()

Func get_rows()
    $rows = $a[0] / $button_in_a_row
    $rows_check = StringSplit ($rows, ".")
    If $rows_check[0] > 0 Then
        If $rows  > $rows_check[1] Then $rows = $rows_check[1] + 1
    EndIf
EndFunc

If $rows > 6 Then $button_in_a_row = $button_in_a_row * 1.5

get_rows()

$width_replicator = $a[0]/10

$row_height = $button_in_a_row
If $a[0] < $button_in_a_row Then $row_height = $a[0]

Dim $go[1]

If $rows < 1 Then
    MsgBox (0, "Starter", "no programs found...")
    Exit
EndIf

$label_count = 1
GUICreate ("Starter", 20 + ($but_width) * $rows, $but_height * ($row_height + 2))
    GUICtrlCreateLabel ("choose your favorite tool...", $tool_label_left, 10, 20 + (125 * $width_replicator), 20)
    $label_count = $label_count + 1

    Opt ("GuiCoordMode", 0)
    $j = $button_in_a_row + 1

    For $i = 1 To $a[0] Step 1
        _ArrayAdd ($go, "")
        $new_title = StringSplit ($a[$i], "\")
        $title = StringLeft ($new_title[$new_title[0]], 50)
        $title = StringReplace ($title, ".exe", "")
        $title = StringReplace ($title, "_", " ")
        $title = StringUpper (StringLeft ($title, 1)) & StringTrimLeft ($title, 1)
        $title = StringLeft ($title, 16)
        

        If $i = $j Then
            $tool_label_left = $tool_label_left + $but_width 
            Opt ("GuiCoordMode", 1)
            GUICtrlCreateLabel ("", $tool_label_left, 10, 145, 20)
            $label_count = $label_count + 1
            Opt ("GuiCoordMode", 0)
            $j = $j + $button_in_a_row
        EndIf
        $go[$i] = GUICtrlCreateButton ("...", 0, $but_height, 25, $but_height, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_ICON))
        GUICtrlSetImage ($go[$i], $a[$i], "", 0)
        GUICtrlCreateLabel ($title, 30, 5)
        GUICtrlCreateLabel ("", -30, -5, 1, 1)
            GuiCtrlSetTip (-1, $a[$i])
    Next

GuiSetState()

While 1
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    For $i = 1 to $a[0] Step 1
        If $msg = $go[$i] Then
            $pos_to_scriptdir = StringReplace ($a[$i], @ScriptDir, "")

            $splitted = StringSplit ($pos_to_scriptdir, "\")
            $work_dir = @ScriptDir
            For $j = 1 to $splitted[0] - 1 Step 1
                $work_dir = $work_dir & "\" & $splitted[$j]
                $work_dir = StringReplace ($work_dir, "\\", "\")
            Next

            Run (@ScriptDir & $pos_to_scriptdir, $work_dir & "\")

        EndIf
    Next

Wend
;--------------------------------------------

Func _FileSearch($szMask,$nOption)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]

    If Not StringInStr($szMask,"\") Then
         $szRoot = @SCRIPTDIR & "\"
    Else
         While StringInStr($szMask,"\")
              $szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))
              $szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))
         Wend
    EndIf
    If $nOption = 0 Then
         _FileSearchUtil($szRoot, $szMask, $szReturn)
    Else
         While 1
              $hFile = FileFindFirstFile($szRoot & "*.*")
              If $hFile >= 0 Then
                   $szBuffer = FileFindNextFile($hFile)
                   While Not @ERROR
                        If $szBuffer <> "." And $szBuffer <> ".." And _
                             StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _
                             $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                        $szBuffer = FileFindNextFile($hFile)
                   Wend
                   FileClose($hFile)
              EndIf
              _FileSearchUtil($szRoot, $szMask, $szReturn)
              If $szPathList == "*" Then ExitLoop
              $szPathList = StringTrimLeft($szPathList,1)
              $szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"
              $szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)
         Wend
    EndIf
    If $szReturn = "" Then
         $aNULL[0] = 0
         Return $aNULL
    Else
         Return StringSplit(StringTrimRight($szReturn,1),"*")
    EndIf
EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
         $szBuffer = FileFindNextFile($hFile)
         While Not @ERROR
              If $szBuffer <> "." And $szBuffer <> ".." Then _
                   $RETURN = $RETURN & $ROOT & $szBuffer & "*"
              $szBuffer = FileFindNextFile($hFile)
         Wend
         FileClose($hFile)
    EndIf
EndFunc

greetz, Chris

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