Jump to content

GUICtrlCreateMenuItem issues


nlgma
 Share

Recommended Posts

Sup all ! I could use some help with this code. When you click on File > Open and select a file. On line 45: If @error <> 1 Then GUICtrlCreateMenuItem($file, $recentfilesmenu). This adds the file path to File > Recent Files. I would like to be able click on the Recent File > File path and reopen the file.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $defaultstatus, $status, $filemenu, $fileitem, $helpmenu, $saveitem
    Local $infoitem, $exititem, $recentfilesmenu, $separator1, $viewmenu
    Local $viewstatusitem, $okbutton, $cancelbutton, $statuslabel, $msg, $file
    
    GUICreate("My GUI menu", 300, 200)

    Global $defaultstatus = "Ready"
    Global $status

    $filemenu = GUICtrlCreateMenu("&File")
    $fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
    $helpmenu = GUICtrlCreateMenu("?")
    $saveitem = GUICtrlCreateMenuItem("Save", $filemenu)
    GUICtrlSetState(-1, $GUI_DISABLE)
    $infoitem = GUICtrlCreateMenuItem("Info", $helpmenu)
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    $recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu, 1)

    $separator1 = GUICtrlCreateMenuItem("", $filemenu, 2) ; create a separator line

    $viewmenu = GUICtrlCreateMenu("View", -1, 1) ; is created before "?" menu
    $viewstatusitem = GUICtrlCreateMenuItem("Statusbar", $viewmenu)
    GUICtrlSetState(-1, $GUI_CHECKED)
    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)
    GUICtrlSetState(-1, $GUI_FOCUS)
    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    $statuslabel = GUICtrlCreateLabel($defaultstatus, 0, 165, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

    GUISetState()
    While 1
        $msg = GUIGetMsg()
        
        If $msg = $fileitem Then
            $file = FileOpenDialog("Choose file...", @TempDir, "All (*.*)")
            If @error <> 1 Then GUICtrlCreateMenuItem($file, $recentfilesmenu)
        EndIf
        If $msg = $viewstatusitem Then
            If BitAND(GUICtrlRead($viewstatusitem), $GUI_CHECKED) = $GUI_CHECKED Then
                GUICtrlSetState($viewstatusitem, $GUI_UNCHECKED)
                GUICtrlSetState($statuslabel, $GUI_HIDE)
            Else
                GUICtrlSetState($viewstatusitem, $GUI_CHECKED)
                GUICtrlSetState($statuslabel, $GUI_SHOW)
            EndIf
        EndIf
        If $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton Or $msg = $exititem Then ExitLoop
        If $msg = $infoitem Then MsgBox(0, "Info", "Only a test...")
    WEnd
    GUIDelete()
EndFunc  ;==>Example
Link to comment
Share on other sites

  • Moderators

nlgma,

This should get you part of the way. It adds the opened files to the Recent menu and then chooses the correct filename when you click on it. I have added some code to remove the item from the menu and the arrays if you want to do that - for example, you might well want to add that file again at the bottom of the list later:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#Include <Array.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $defaultstatus, $status, $filemenu, $fileitem, $helpmenu, $saveitem
    Local $infoitem, $exititem, $recentfilesmenu, $separator1, $viewmenu
    Local $viewstatusitem, $okbutton, $cancelbutton, $statuslabel, $msg, $file
    
    GUICreate("My GUI menu", 300, 200)

    Global $defaultstatus = "Ready"
    Global $status

    $filemenu = GUICtrlCreateMenu("&File")
        $fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
            GUICtrlSetState(-1, $GUI_DEFBUTTON)
        $recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu)
        GUICtrlCreateMenuItem("", $filemenu); create a separator line
        $saveitem = GUICtrlCreateMenuItem("Save", $filemenu)
        $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
            GUICtrlSetState(-1, $GUI_DISABLE)
    $viewmenu = GUICtrlCreateMenu("View")
        $viewstatusitem = GUICtrlCreateMenuItem("Statusbar", $viewmenu)
                GUICtrlSetState(-1, $GUI_CHECKED)
    $helpmenu = GUICtrlCreateMenu("?")
        $infoitem = GUICtrlCreateMenuItem("Info", $helpmenu)
    
    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)
        GUICtrlSetState(-1, $GUI_FOCUS)
    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    $statuslabel = GUICtrlCreateLabel($defaultstatus, 0, 165, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

    GUISetState()
    
    Global $Recent_Handle[1] = [0]
    Global $Recent_Filename[1] = [0]
    
    While 1
        $msg = GUIGetMsg()
        
        Switch $msg
        
            Case $fileitem
                $file = FileOpenDialog("Choose file...", "M:\Program\Au3 Scripts", "All (*.*)")
                If @error <> 1 Then
                    Local $handle = GUICtrlCreateMenuItem($file, $recentfilesmenu)
                    _ArrayAdd($Recent_FileName, $file)
                    $Recent_FileName[0] += 1
                    _ArrayAdd($Recent_Handle, $handle)
                EndIf
            
            Case $viewstatusitem
                If BitAND(GUICtrlRead($viewstatusitem), $GUI_CHECKED) = $GUI_CHECKED Then
                    GUICtrlSetState($viewstatusitem, $GUI_UNCHECKED)
                    GUICtrlSetState($statuslabel, $GUI_HIDE)
                Else
                    GUICtrlSetState($viewstatusitem, $GUI_CHECKED)
                    GUICtrlSetState($statuslabel, $GUI_SHOW)
                EndIf
            
        Case $GUI_EVENT_CLOSE, $cancelbutton, $exititem
            ExitLoop
            
        Case $infoitem
            MsgBox(0, "Info", "Only a test...")
            
        Case Else
            If $msg > 0 Then 
                Local $FileToOpen = ""
                For $i = 1 To $Recent_FileName[0]
                    If $msg = $Recent_Handle[$i] Then
                        $FileToOpen = $Recent_FileName[$i]
                    ; Remove item from menu and both arrays if required
                        GUICtrlDelete($Recent_Handle[$i])
                        _ArrayDelete($Recent_FileName, $i)
                        _ArrayDelete($Recent_Handle, $i)
                        ExitLoop
                    EndIf
                Next
                
                If $FileToOpen <> "" Then ConsoleWrite($FileToOpen & @CRLF); Open the file
            EndIf   
        EndSwitch
    WEnd
    
    GUIDelete()
    
EndFunc ;==>Example

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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