If you open and close files, then it will be more economical to generate the menu at the time it is opened, at the time the menu is accessed, and not always. It's easier to have a list where you add a file to the beginning or end, and remove it from the other end. And when you request a menu (MF_POPUP), you create it based on the list.
Perhaps you will never open the list during your work session, then why recreate it every time you open/close a file?
Code: Select all
EnableExplicit
#Window = 0
#Menu = 0
Global k = 0
Global hCUI
Procedure WinCallback(hWnd, Msg, wParam, lParam)
Protected info$, ID, Flags
ID = wParam & $FFF ; LoWord
Flags = wParam >> 16 ; HiWord
If Msg = #WM_MENUSELECT
k + 1
SetWindowTitle(#Window, "Call " + k)
If Flags & #MF_CHECKED
info$ + "MF_CHECKED" + #CRLF$
EndIf
If Flags & #MF_DISABLED
info$ + "MF_DISABLED" + #CRLF$
EndIf
If Flags & #MF_GRAYED
info$ + "MF_GRAYED" + #CRLF$
EndIf
If Flags & #MF_HILITE
info$ + "MF_HILITE" + #CRLF$
EndIf
If Flags & #MF_MOUSESELECT
info$ + "MF_MOUSESELECT" + #CRLF$
EndIf
If Flags & #MF_OWNERDRAW
info$ + "MF_OWNERDRAW" + #CRLF$
EndIf
If Flags & #MF_POPUP
info$ + "MF_POPUP" + #CRLF$
EndIf
If Flags & #MF_SYSMENU
info$ + "MF_SYSMENU" + #CRLF$
EndIf
SetGadgetText(1, "handle = " + lParam + #CRLF$ +
"ID = " + ID + #CRLF$ +
"Flags:" + Flags + #CRLF$ +
info$)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
hCUI = OpenWindow(#Window, 5, 5, 590, 270, "WM_MENUSELECT")
If hCUI
TextGadget(0, 10, 5, 300, 94, "The WM_MENUSELECT function is triggered when the main or context menu and its items are selected.")
TextGadget(1, 400, 5, 185, 264, "")
If CreateMenu(#Menu, WindowID(#Window))
MenuTitle("+File")
MenuItem(1, "Open")
MenuItem(2, "Save")
MenuItem(3, "Save as")
MenuBar()
OpenSubMenu("Options")
MenuItem(9, "Window...")
MenuItem(10, "Gadget...")
CloseSubMenu()
MenuItem(4, "Close")
MenuTitle("Help")
MenuItem(5, "Web")
MenuItem(6, "Support")
SetMenuItemState(#Menu , 1 , #True)
DisableMenuItem(#Menu , 2 , #True)
EndIf
SetWindowCallback(@WinCallback())
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Simplified version
Code: Select all
EnableExplicit
#Window = 0
#Menu = 0
Global hSubmenu, Allow = 1, em
Procedure WinCallback(hWnd, Msg, wParam, lParam)
Protected ID, Flags, i
ID = wParam & $FFF ; LoWord
Flags = wParam >> 16 ; HiWord
If Msg = #WM_MENUSELECT
If ID = 4 And Flags & #MF_POPUP And Allow
Debug "menu created"
For i = 10 To 20
DeleteMenu_(hSubmenu, i, #MF_BYCOMMAND)
Next
For i = 10 To 20
InsertMenu_(hSubmenu, -1, #MF_BYCOMMAND, i, "Path_" + Random(9))
Next
DrawMenuBar_(WindowID(0))
Allow = 0
ElseIf Flags = $FFFF
Debug "allow to create menu"
Allow = 1
EndIf
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
If OpenWindow(#Window, 5, 5, 590, 270, "Recent")
If CreateMenu(#Menu, WindowID(#Window))
MenuTitle("File")
MenuItem(1, "Open")
MenuItem(2, "Save")
MenuItem(3, "Save as")
MenuBar()
hSubmenu = OpenSubMenu("Recent")
CloseSubMenu()
MenuItem(5, "Close")
MenuTitle("Help")
MenuItem(6, "Web")
MenuItem(7, "Support")
EndIf
SetWindowCallback(@WinCallback())
Repeat
Select WaitWindowEvent()
Case #PB_Event_Menu
em = EventMenu()
Select em
Case 10 To 20
Debug "Array index = " + Str(em - 10)
EndSelect
Case #PB_Event_CloseWindow
CloseWindow(#Window)
End
EndSelect
ForEver
EndIf
You can add a list modification flag. If it has changed, then you recreate the menu again, otherwise use the previously created one.