Any suggestions or improvements then please post below. Thanks.
Example:

UDF:
#include-once ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ; #INDEX# ======================================================================================================================= ; Title .........: _GUICtrlMenu_Recent ; AutoIt Version : v3.2.2.0 or higher ; Language ......: English ; Description ...: Create a recent menu to add previously opened files or other text items to. ; Author(s) .....: guinness ; =============================================================================================================================== ; #INCLUDES# ========================================================================================================= #include <WinAPI.au3> #include <WindowsConstants.au3> ; #GLOBAL VARIABLES# ================================================================================================= ; None ; #CURRENT# ===================================================================================================================== ; _GUICtrlMenu_RecentAdd: Add an item to the recent menu that was created with _GUICtrlMenu_RecentCreate. ; _GUICtrlMenu_RecentCreate: Create a recent menu that is added to an existing menu created with GUICtrlCreateMenu. ; _GUICtrlMenu_RecentDelete: Delete an item in the recent menu that was created with _GUICtrlMenu_RecentCreate. ; _GUICtrlMenu_RecentIsInMenu: Search if a filepath or text is currently in the menu list. ; _GUICtrlMenu_RecentSelected: Retrieve the text of a selected menu item and delete from the menu list. ; _GUICtrlMenu_RecentTotal: Change the total number of items to be displayed in the recent menu. ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ; __GUICtrlMenu_RecentDelete ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentAdd ; Description ...: Add an item to the recent menu that was created with _GUICtrlMenu_RecentCreate. ; Syntax ........: _GUICtrlMenu_RecentAdd(Byref $aAPI, $sText) ; Parameters ....: $aAPI - [in/out] API created by _GUICtrlMenu_RecentCreate. ; $sText - Text to be added to the recent menu. ; $fShortText - [optional] Display the text as short text e.g. 'C:Example...Test.au3'. Default is False. ; Return values .: Success - ControlID from GUICtrlCreateMenuItem ; Failure - Returns 0 & sets @error to none-zero. ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentAdd(ByRef $aAPI, $sText, $fShortText = False) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax #forceref $iAPIMax If $aAPI[0][$iAPIMenu] = -1 Then Return SetError(1, 0, 0) EndIf _GUICtrlMenu_RecentIsInMenu($aAPI, $sText) Local Const $iIndex = @extended If $iIndex Then __GUICtrlMenu_RecentDelete($aAPI, $aAPI[0][$iAPITotal], $iIndex, $aAPI[0][$iAPICount], $aAPI[0][$iAPIColumns]) EndIf If $aAPI[0][$iAPICount] = $aAPI[0][$iAPITotal] Then __GUICtrlMenu_RecentDelete($aAPI, $aAPI[0][$iAPITotal], 1, $aAPI[0][$iAPICount], $aAPI[0][$iAPIColumns]) EndIf Local $sShortText = $sText If $fShortText Then $sShortText = StringRegExpReplace($sText, '(^.{3,11}|.{11})(.*)(.{6,27}|.{27})$', '1...3') ; Thanks to AZJIO for the regular expression. EndIf $aAPI[0][$iAPITotal] += 1 $aAPI[$aAPI[0][$iAPITotal]][0] = GUICtrlCreateMenuItem($sShortText, $aAPI[0][$iAPIMenu], 0) $aAPI[$aAPI[0][$iAPITotal]][1] = $sText $aAPI[$aAPI[0][$iAPITotal]][2] = $sShortText Return $aAPI[$aAPI[0][$iAPITotal]][0] EndFunc ;==>_GUICtrlMenu_RecentAdd ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentCreate ; Description ...: Create a recent menu that is added to an existing menu created with GUICtrlCreateMenu. ; Syntax ........: _GUICtrlMenu_RecentCreate(Byref $aAPI, $iTotal, $iMenu, $sTitle[, $iMenuEntry = -1]) ; Parameters ....: $aAPI - [in/out] Variable to be used to store the API information. ; $iTotal - Maximum total number of recent menu items to be displayed. If this is exceeded then old values will be overwritten. ; $iMenu - Existing menu id created with GUICtrlCreateMenu. ; $sTitle - Title of the recent menu. ; Return values .: Success - API to be passed to _GUICtrlMenu_RecentAdd, _GUICtrlMenu_RecentIsInMenu or _GUICtrlMenu_RecentSelected. ; Failure - None ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentCreate(ByRef $aAPI, $iTotal, $iMenu, $sTitle) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax If $iTotal < 1 Then $iTotal = 10 EndIf Local $aArray[$iTotal + 1][$iAPIMax] $aArray[0][$iAPICount] = $iTotal ; Total number of rows. $aArray[0][$iAPIColumns] = $iAPIMax ; Total number of columns. $aArray[0][$iAPITotal] = 0 ; Total number of items. $aArray[0][$iAPIMenu] = $iMenu ; Menu controlid for the recent list. $aArray[0][$iAPIMenu] = GUICtrlCreateMenu($sTitle, $aArray[0][$iAPIMenu]) $aAPI = $aArray EndFunc ;==>_GUICtrlMenu_RecentCreate ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentDelete ; Description ...: Delete an item in the recent menu that was created with _GUICtrlMenu_RecentCreate. ; Syntax ........: _GUICtrlMenu_RecentDelete(Byref $aAPI, $vIndex) ; Parameters ....: $aAPI - [in/out] API created by _GUICtrlMenu_RecentCreate. ; $vIndex - Value to be deleted. This can either be an integer of the API index (see _GUICtrlMenu_RecentIsInMenu and remarks) ; or a string value of the item. ; Return values .: Success - True ; Failure - False and sets @error to non-zero. ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentDelete(ByRef $aAPI, $vIndex) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax #forceref $iAPIMenu, $iAPIMax If IsString($vIndex) Then _GUICtrlMenu_RecentIsInMenu($aAPI, $vIndex) $vIndex = @extended EndIf If $vIndex <= 0 Or $vIndex > $aAPI[0][$iAPICount] Then Return SetError(1, 0, False) EndIf Return __GUICtrlMenu_RecentDelete($aAPI, $aAPI[0][$iAPITotal], $vIndex, $aAPI[0][$iAPICount], $aAPI[0][$iAPIColumns]) EndFunc ;==>_GUICtrlMenu_RecentDelete ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentIsInMenu ; Description ...: Search if a filepath or text is currently in the menu list. ; Syntax ........: _GUICtrlMenu_RecentIsInMenu(Byref $aAPI, $sText) ; Parameters ....: $aAPI - [in/out and const] API created by _GUICtrlMenu_RecentCreate. ; $sText - Text to search for. This is a case-sensitive search. ; Return values .: Success - True ; Failure - False ; Author ........: guinness ; Remarks .......: @extended is set to the index position in the API handle. This is useful if using GUICtrlMenu_RecentDelete. ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentIsInMenu(ByRef Const $aAPI, $sText) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax #forceref $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax For $i = 1 To $aAPI[0][$iAPICount] If ($sText == $aAPI[$i][1]) Or ($sText == $aAPI[$i][2]) Then Return SetExtended($i, True) EndIf Next Return SetExtended(0, False) EndFunc ;==>_GUICtrlMenu_RecentIsInMenu ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentSelected ; Description ...: Retrieve the text of a selected menu item and delete from the menu list. ; Syntax ........: _GUICtrlMenu_RecentSelected(Byref $aAPI, $iMsg) ; Parameters ....: $aAPI - [in/out] API created by _GUICtrlMenu_RecentCreate. ; $iMsg - Message id returned by GUIGetMsg. ; Return values .: Success - Text of the selected menu. ; Failure - Blank string and sets @error to non-zero. ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentSelected(ByRef $aAPI, $iMsg) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax #forceref $iAPIMenu, $iAPIMax If $iMsg <= 0 Then Return SetError(1, 0, '') EndIf Local $sFilePath = '' For $i = 1 To $aAPI[0][$iAPICount] If $iMsg = $aAPI[$i][0] Then $sFilePath = $aAPI[$i][1] __GUICtrlMenu_RecentDelete($aAPI, $aAPI[0][$iAPITotal], $i, $aAPI[0][$iAPICount], $aAPI[0][$iAPIColumns]) ExitLoop EndIf Next Return $sFilePath EndFunc ;==>_GUICtrlMenu_RecentSelected ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GUICtrlMenu_RecentTotal ; Description ...: Change the total number of items to be displayed in the recent menu. ; Syntax ........: _GUICtrlMenu_RecentTotal(Byref $aAPI, $iTotal) ; Parameters ....: $aAPI - [in/out] API created by _GUICtrlMenu_RecentCreate. ; $iTotal - Maximum total number of recent menu items to be displayed. If this is exceeded then old values will be overwritten. ; Return values .: Success - True ; Failure - False and sets @error to non-zero. ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GUICtrlMenu_RecentTotal(ByRef $aAPI, $iTotal) Local Enum $iAPICount, $iAPIColumns, $iAPITotal, $iAPIMenu, $iAPIMax #forceref $iAPIMenu, $iAPIMax If $iTotal < 1 Or $iTotal = $aAPI[0][$iAPICount] Then Return SetError(1, 0, False) EndIf If $iTotal < $aAPI[0][$iAPICount] Then For $i = 1 To ($aAPI[0][$iAPITotal] - $iTotal) GUICtrlDelete($aAPI[$i][0]) Next Local $iIndex = ($aAPI[0][$iAPITotal] - $iTotal) + 1 For $i = 1 To $iTotal For $j = 0 To $aAPI[0][$iAPIColumns] - 1 $aAPI[$i][$j] = $aAPI[$iIndex][$j] Next $iIndex += 1 Next $aAPI[0][$iAPITotal] = $iTotal EndIf $aAPI[0][$iAPICount] = $iTotal ReDim $aAPI[$aAPI[0][$iAPICount] + 1][$aAPI[0][$iAPIColumns]] Return True EndFunc ;==>_GUICtrlMenu_RecentTotal ; #INTERNAL_USE_ONLY#============================================================================================================ Func __GUICtrlMenu_RecentDelete(ByRef $aAPI, ByRef $iTotal, $iIndex, $iCount, $iColumns) Local $iReturn = GUICtrlDelete($aAPI[$iIndex][0]) For $i = $iIndex To $iCount - 1 For $j = 0 To $iColumns - 1 $aAPI[$i][$j] = $aAPI[$i + 1][$j] $aAPI[$i + 1][$j] = '' Next Next $iTotal -= 1 Return $iReturn = 1 EndFunc ;==>__GUICtrlMenu_RecentDelete
Example 1:
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <GUIConstantsEx.au3> #include '_GUICtrlMenu_Recent.au3' Example() Func Example() Local $sFilePath = '' Local $hGUI = GUICreate('_GUICtrlMenu_Recent() Example', 300, 200) Local $iFileMenu = GUICtrlCreateMenu('&File') Local $iOpen = GUICtrlCreateMenuItem('Open', $iFileMenu) GUICtrlSetState(-1, $GUI_DEFBUTTON) ; Create a variable to store the recent menu API information. Local $hRecentMenu = 0 _GUICtrlMenu_RecentCreate($hRecentMenu, 5, $iFileMenu, 'Recent Files') ; Create a recent menu with a maximum of 5 displayed items. GUICtrlCreateMenuItem('', $iFileMenu) ; Seperator Line. GUICtrlCreateMenuItem('Save', $iFileMenu) GUICtrlSetState(-1, $GUI_DISABLE) Local $iExit = GUICtrlCreateMenuItem('Exit', $iFileMenu) Local $iOpenFile = GUICtrlCreateButton('Open File', 5, 10, 85, 25) Local $iIncreaseItems = GUICtrlCreateButton('Increase Recent List', 5, 35, 115, 25) GUISetState(@SW_SHOW, $hGUI) Local $iMsg = 0 While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE, $iExit ExitLoop Case $iOpenFile, $iOpen $sFilePath = FileOpenDialog('Choose File...', @ScriptDir, 'All (*.*)') If @error = 0 Then ; Check whether or not the filepath is currently in the recent menu. If it isn't then add to the recent menu. If Not _GUICtrlMenu_RecentIsInMenu($hRecentMenu, $sFilePath) Then ; If the button or open menu items are selected then add a filepath to the recent menu. _GUICtrlMenu_RecentAdd($hRecentMenu, $sFilePath, True) ; Display the text as short text. EndIf EndIf Case $iIncreaseItems If _GUICtrlMenu_RecentTotal($hRecentMenu, 20) Then MsgBox(4096, '', 'The recent menu list was increased to a maximum of 20 items.', 0, $hGUI) Else MsgBox(4096, '', 'The recent menu list wasn''t increased to a maximum of 20 items. An error occurred.', 0, $hGUI) EndIf Case Else ; Check if the message id returned by GUIGetMsg is present in the menu list. $sFilePath = _GUICtrlMenu_RecentSelected($hRecentMenu, $iMsg) If Not @error And $sFilePath Then MsgBox(4096, '', 'The recent menu was clicked and the following item was selected.' & @CRLF & _ @CRLF & _ $sFilePath, 0, $hGUI) EndIf EndSwitch WEnd GUIDelete($hGUI) EndFunc ;==>Example
All of the above has been included in a ZIP file.
GUICtrlMenu_Recent.zip 3.4K
75 downloads
Edited by guinness, 23 October 2012 - 09:52 PM.








