Jump to content
Sign in to follow this  
tittoproject

Refresh Tray Menu

Recommended Posts

tittoproject

I'm trying to write a simple tray menu that reads items from a .ini file (called "itemslist.ini") and show them. From this tray menu I can also add or delete items.

Everything works fine, but I've this problem: I can't understand how to refresh the tray menu once a item is added or removed.

The code of the tray menu is:

Opt('TrayMenuMode', 1)

$ItemsList = @ScriptDir & '\itemslist.ini'
If Not FileExists($ItemsList) Then
    IniWrite($ItemsList, 'Items', 'Item1', 'Test')
EndIf

$Items = IniReadSection($ItemsList, 'Items')
$NI = $Items[0][0]

Global $ItemMenu[$NI + 1][4]
For $i = 1 To $NI
    $ItemMenu[$i][1] = TrayCreateMenu($Items[$i][0])
    $ItemMenu[$i][2] = TrayCreateItem('Show', $ItemMenu[$i][1])
    $ItemMenu[$i][3] = TrayCreateItem('Delete', $ItemMenu[$i][1])
    TrayCreateItem('')
Next

$NewItem = TrayCreateItem('New Item')
TrayCreateItem('')
$ShowItemsList = TrayCreateItem('Items List')
TrayCreateItem('')
$ExitItem = TrayCreateItem('Exit')
TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $ExitItem
            ExitLoop
        Case $msg = $NewItem
            $Key = InputBox('New Item', "Enter New Item's Name", '')
            $Value = InputBox('New Item', "Enter New Item's Value", '')
            IniWrite($ItemsList, 'Items', $Key, $Value)
        Case $msg = $ShowItemsList
            Run(@ComSpec & ' /c "' & $ItemsList & '"', '', @SW_HIDE)
        Case Else
            For $i = 1 To $NI
                Select
                    Case $msg = $ItemMenu[$i][2]
                        MsgBox(0, $Items[$i][0], $Items[$i][1])
                    Case $msg = $ItemMenu[$i][3]
                        IniDelete($ItemsList, 'Items', $Items[$i][0])
                EndSelect
            Next
    EndSelect
WEnd

Exit

Share this post


Link to post
Share on other sites
Valuater

just played with it... but this should do the trick

Opt('TrayMenuMode', 1)

Global $ItemsList = @ScriptDir & '\itemslist.ini'
Global $Items, $ItemMenu[1], $ItemsMain[9]

If Not FileExists($ItemsList) Then
    IniWrite($ItemsList, 'Items', 'Main Menu', 'Required - do not remove')
    IniWrite($ItemsList, 'Items', 'Notepad', 'notepad.exe')
EndIf

_SetTrayMenu()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $ItemsMain[2] ; 'New Item'
            _SetTrayMenu(2)
        Case $msg = $ItemsMain[3] ; 'Remove Item'
            _SetTrayMenu(3)
        Case $msg = $ItemsMain[4] ; 'View All Items'
            Run(@ComSpec & ' /c "' & $ItemsList & '"', '', @SW_HIDE)
        Case $msg = $ItemsMain[5] ; 'Exit'
            Exit
        Case Else
            For $i = 2 To UBound($ItemMenu) - 1
                ; If $msg = $ItemMenu[$i]Then Call($Items[$i][1]) ; call value ?
                If $msg = $ItemMenu[$i]Then Run($Items[$i][1]) ; run value ?
                ; If $msg = $ItemMenu[$i]Then DoWhatYouWith($Items[$i][1]) ; ??? value?
            Next
    EndSelect
WEnd

Func _SetTrayMenu($TStart = 0)
    If $TStart = 3 Then
        $Key = InputBox('Remove Item', "Enter Old Item's Name", '')
        If $Key <> "" Then
            For $i = 2 To UBound($ItemMenu) - 1
                If $Key = $Items[$i][0] Then
                    IniDelete($ItemsList, 'Items', $Key)
                    $TStart = 1
                EndIf
            Next
            If $TStart = 3 Then
                MsgBox(64, "Sorry", "the Item *" & $Key & "* was not found in the list   ", 3)
                Return
            EndIf
        Else
            Return
        EndIf
    EndIf
    If $TStart = 2 Then
        $Key = InputBox('New Item', "Enter New Item's Name", '')
        $Value = InputBox('New Item', "Enter New Item's Value", '')
        If $Key <> "" And $Value <> "" Then
            IniWrite($ItemsList, 'Items', $Key, $Value)
            $TStart = 1
        Else
            Return
        EndIf
    EndIf
    If $TStart = 1 Then
        For $x = 1 To UBound($ItemsMain) - 1
            TrayItemDelete($ItemsMain[$x])
        Next
        For $i = 1 To UBound($ItemMenu) - 1
            TrayItemDelete($ItemMenu[$i])
        Next
    EndIf
    $Items = IniReadSection($ItemsList, 'Items')
    ReDim $ItemMenu[UBound($Items)]
    $ItemMenu[1] = TrayCreateItem($Items[1][0])
    $ItemsMain[6] = TrayCreateItem('')
    For $i = 2 To UBound($ItemMenu) - 1
        $ItemMenu[$i] = TrayCreateItem($Items[$i][0])
    Next
    $ItemsMain[7] = TrayCreateItem('')
    $ItemsMain[1] = TrayCreateMenu('Config Menu')
    $ItemsMain[2] = TrayCreateItem('New Item', $ItemsMain[1])
    $ItemsMain[3] = TrayCreateItem('Remove Item', $ItemsMain[1])
    $ItemsMain[4] = TrayCreateItem('View All Items', $ItemsMain[1])
    $ItemsMain[8] = TrayCreateItem('')
    $ItemsMain[5] = TrayCreateItem('Exit')
    TraySetState()
EndFunc   ;==>_SetTrayMenu

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
tittoproject

just played with it... but this should do the trick

Thank you very much!

It's exactly what I need!

Share this post


Link to post
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
Sign in to follow this  

×