Sign in to follow this  
Followers 0
Rawox

ContextMenu from File

3 posts in this topic

#1 ·  Posted (edited)

Hi there!

I'm currently working on a little project that allows users to create files (.cgf) that will transform the configuration into a contextmenu.

This is an example of a configuration file:

g stands for group - GUICtrlCreateMenu

i stands for item - GUICtrlCreateMenuItem

s stands for seperator

i0 XFire
s0 -
g0 Games
g1 FPS
i2 Unreal Tournament 2004
i2 Unreal Tournament 3
i2 Mass Effect
g1 Racing
g2 Trackmania
i3 Sunrise
i3 Nations
i1 All Shortcuts
s0 -
g0 Multiple groups
g1 Group 1
i2 Subitem - Group 1
i1 Testitem in Multiple Groups
g1 Group 2
g2 Subgroup 2
g3 Subsubgroup 2
g4 Subsubsubgroup 2
s4 -
i4 subsubsubitem
g5 Subsubsubsubgroup 2
i6 Last item
s1 -
i1 Last item in Multiple groups
i0 Test item
g0 Second group
i1 Test sub-item
s0 -
i0 Close

This is my script which requires the path in the first commandline parameter when you run it.

#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <File.au3>
#include <Array.au3>

Global $Title = "ContextGen", $Version = "1.2", $Ext = ".cgf"

If $cmdline[0] > 0 Then
    If $cmdLine[1] <> "" Then
        If FileExists ( $cmdLine[1] ) Then
            If StringRight ( $cmdLine[1], 4 ) == $Ext Then
                _createMenu ( $cmdLine[1] )
            Else
                MsgBox ( 48, "Wrong file", "The following file is not a " & $Title & " configuration file:" & @CRLF & $cmdLine[1] )
            EndIf
        Else
            MsgBox ( 48, "File not found", "The following " & $Title & " configuration file could not be found:" & @CRLF & $cmdLine[1] )
        EndIf
    EndIf
Else
    MsgBox ( 0,0, "Configuration GUI" )
EndIf

Func _createMenu ( $Path )
    Global  $hMenuGUI = GUICreate ( "ContextGen Menu - " & $Path, 1, 1 )
    Global  $hMenu =    GUICtrlCreateContextMenu ( )

    $i = 1
    $hFile = FileOpen ( $Path )
    Global $Item[_FileCountLines ( $Path ) + 1] = [""]
           $Item[0] = _FileCountLines ( $Path )
    Global $Level[_getMaxLevel($Path) + 1] = [""]
           $Level[0] = $hMenu

    Do

        If StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "s" Then
            If _getLevel($Path, $i) == 0 Then
                $Item[$i] = GUICtrlCreateMenuItem ( "", $hMenu )
            Else
                $Item[$i] = GUICtrlCreateMenuItem ( "", $Item[$Level[_getLevel($Path, $i)-1]] )
            EndIf
        Else
            If StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "g" Then
                If _getLevel($Path, $i) == 0 Then
                    $Item[$i] = GUICtrlCreateMenu ( FileReadLine ( $hFile, $i ), $hMenu )
                Else
                    $Item[$i] = GUICtrlCreateMenu ( FileReadLine ( $hFile, $i ), $Item[$Level[_getLevel($Path, $i)-1]] )
                    $Level[_getLevel($Path, $i)] = $i
                EndIf
            ElseIf StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "i" Then
                If _getLevel($Path, $i) == 0 Then
                    $Item[$i] = GUICtrlCreateMenuItem ( FileReadLine ( $hFile, $i ), $hMenu )
                Else
                    $Item[$i] = GUICtrlCreateMenuItem ( FileReadLine ( $hFile, $i ), $Item[$Level[_getLevel($Path, $i)-1]] )
                EndIf
            EndIf
        EndIf

        $i += 1
    Until $i == _FileCountLines ( $Path ) + 1

    GUIRegisterMsg ( $WM_COMMAND, "WM_COMMAND" )
    GUIRegisterMsg ( $WM_CONTEXTMENU, "WM_CONTEXTMENU" )

    WM_CONTEXTMENU ( $hMenuGUI, 0x007b, $hMenuGUI, 0 )
EndFunc

Func _getLevel ( $Path, $iLine )
    $currentLevel = StringSplit ( FileReadLine ( $Path, $iLine ), " ", 1 )
    $currentLevel = StringTrimLeft ( $currentLevel[1], 1 )

    Return $currentLevel
EndFunc

Func _getMaxLevel ( $Path )
    $j = 1
    $maxLevel = 0
    Do
        $ThisLevel = _getLevel ( $Path, $j ) ;StringMid ( FileReadLine ( $Path, $j ), 2, 1 )
        If $ThisLevel > $maxLevel Then $maxLevel = $ThisLevel
        $j += 1
    Until $j == _FileCountLines ( $Path ) + 1

    Return $maxLevel
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Switch $iwParam
        Case 1
            MsgBox ( 0,0,1 )
        Case $Item[2]
            MsgBox ( 0,0, "Test2" )
        Case $Item[3]
            MsgBox ( 0,0, "Close" )
            Exit
    EndSwitch
EndFunc

Func WM_CONTEXTMENU ( $hWnd, $iMsg, $iwParam, $ilParam )
    $hMenuHandle = GUICtrlGetHandle ( $hMenu )
    $Pos = MouseGetPos ( )
    _GUICtrlMenu_SetItemHighlighted ($hMenuHandle, 0 )
    _GUICtrlMenu_TrackPopupMenu ($hMenuHandle, $hMenuGUI, $Pos[0]-40, $Pos[1]-15 )
EndFunc

For some reason it doesn't sort the list right. It won't switch back to the 'main list'.

It's hard to explain so I think you can better just run it...

Anyone knows why this happens?

Thanks in advance,

Rawox

Edited by Rawox

Share this post


Link to post
Share on other sites



Hi, i'm not too good with arrays myself, but there is many ways this can be done.

Have a look at these links, the second link is probably more on the line of what you are looking for.

hope it helps!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi, i'm not too good with arrays myself, but there is many ways this can be done.

Have a look at these links, the second link is probably more on the line of what you are looking for.

hope it helps!

Thanks for those links! I'll read through them but what I understand so far is that these people need dynamic menu items.

I think this is something else that I need, my script only has to read such a file and then transform it into a contextmenu.

Rawox

Edit

I finally managed to get it working, I only changed the _createMenu function...

Func _createMenu ( $Path )
    Global  $hMenuGUI = GUICreate ( "ContextGen Menu - " & $Path, 1, 1 )
    Global  $hMenu =    GUICtrlCreateContextMenu ( )

    $i = 1
    $hFile = FileOpen ( $Path )
    Global $Item[_FileCountLines ( $Path ) + 1] = [""]
           $Item[0] = _FileCountLines ( $Path )
    Global $Level[_getMaxLevel($Path) + 1] = [""]
           $Level[0] = $hMenu

    Do

        If StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "s" Then
            If _getLevel($Path, $i) == 0 Then
                $Item[$i] = GUICtrlCreateMenuItem ( "", $hMenu )
            Else
                $Item[$i] = GUICtrlCreateMenuItem ( "", $Level[_getLevel($Path, $i)-1] )
            EndIf
        Else
            If StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "g" Then
                If _getLevel($Path, $i) == 0 Then
                    $Level[_getLevel($Path, $i)] = GUICtrlCreateMenu ( FileReadLine ( $hFile, $i ), $hMenu )
                Else
                    $Level[_getLevel($Path, $i)] = GUICtrlCreateMenu ( FileReadLine ( $hFile, $i ), $Level[_getLevel($Path, $i)-1] )
                EndIf
            ElseIf StringLeft ( FileReadLine ( $hFile, $i ), 1 ) == "i" Then
                If _getLevel($Path, $i) == 0 Then
                    $Item[$i] = GUICtrlCreateMenuItem ( FileReadLine ( $hFile, $i ), $hMenu )
                Else
                    $Item[$i] = GUICtrlCreateMenuItem ( FileReadLine ( $hFile, $i ), $Level[_getLevel($Path, $i)-1] )
                EndIf
            EndIf
        EndIf

        $i += 1
    Until $i == _FileCountLines ( $Path ) + 1

    GUIRegisterMsg ( $WM_COMMAND, "WM_COMMAND" )
    GUIRegisterMsg ( $WM_CONTEXTMENU, "WM_CONTEXTMENU" )

    WM_CONTEXTMENU ( $hMenuGUI, 0x007b, $hMenuGUI, 0 )
EndFunc
Edited by Rawox

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  
Followers 0