GrahameKing
Sep 26, 2006
- "wrote explanatory intro"
You can quickly try this code by pasting it into your LB IDE and running it. The menu that popsup when you right-click in the graphicbox should be a list of all the folders in the LB distribution. A better test would be to put it in a folder of its own and create subfolders with the names you want to appear on the menu. This is just one way of making popup menus with the Windows API #user32 dlls. The advantages over the LB popupmenu command are: If the users changes his mind (I say "his" because women don't do this!) and clicks somewhere else, then the old menu disappears and a new one appears. With the LB command the user has to click one more time. The main advantage is you can load menus dynamically as they are not "hard coded". So your menus can change to reflect the current state of affairs while the program is running. [[code format="vbnet"]] ' program demonstrating API popupmenus - Grahame King Oct 2006global true, false true = (1=1) : false = not(true)' Released to public domain - September 27th 2006 dim fi$(10,10) FolderDir$ = DefaultDir$ open "API popups demo" for graphics as #main #main "trapclose quit.main" struct popupPoint, x as long, y as long global popupItems ' number of items in menu call GetItemsToPopup #main "when rightButtonUp PopsUpMenu" wait end sub quit.main handle$closeclose #handle$endend end sub sub AddMenuItem hndle,MenuRetCode,flag,MenuString$ ifflag = 0flag=0 thenflag = _MF_STRINGflag=_MF_STRING calldll #user32,"AppendMenuA",_hndlehndle as ulong,_flagflag as long,_MenuRetCodeMenuRetCode as long,_MenuString$MenuString$ as ptr,_rr as long ' return unused end sub sub GetItemsToPopup files FolderDir$, fi$() numFiles = val(fi$(0,0)) numFolders = val(fi$(0,1)) ' redim and load array popupItems = numFolders redim popupArray$(popupItems) popupArray$(0) = "Folders:}folderMenu" ' header}name of popup for i = 1 to popupItemspopupArray$(i)popupArray$(i) = fi$(i+numFiles,1) next i sort popupArray$(), 1 , popupItems end sub sub PopsUpMenu Handle$,x,y ' actually pops up menu in popupArray$() at point x,y in window or control, #Handle$ ' and converts response to action calldll #user32,"CreatePopupMenu",hpopup as ulong heading$ = word$(popupArray$(0),1,"}") if heading$<>"}" thencallcall AddMenuItem hpopup,0,_MF_DISABLED,heading$callcall AddMenuItem hpopup,0,_MF_SEPARATOR,"" end if flag = 0 for i = 1 to popupItemscallcall AddMenuItem hpopup,i,flag,popupArray$(i) next i hWnd = hwnd(#Handle$) popupPoint.x.struct = x popupPoint.y.struct = y calldll #user32, "ClientToScreen",_hWndhWnd as long,_ ' handle of control or windowpopupPointpopupPoint as struct,_ 'name of struct containing client/screen coorinatesrr as long tempx = popupPoint.x.struct tempy = popupPoint.y.struct flags = _TPM_TOPALIGN OR _TPM_LEFTALIGN OR _TPM_RETURNCMD CallDLL #user32, "TrackPopupMenu",_hpopuphpopup as uLong,_flagsflags As Long,_tempxtempx As Long,_tempytempy As Long,_00 As Long,_hWndhWnd as uLong,_00 As Long,_rere as long ' user's selection (because _TPM_RETURNCMD flag set)calldllcalldll #user32,"DestroyMenu",hpopup as long 'take action on user's selection popupID$ = word$(popupArray$(0),2,"}") select case popupID$casecase "folderMenu"ifif re<>0 thennoticenotice "You selected "+popupArray$(re);resp$else endelse end if'' other menus if you have them could go here'' but you would have to have a way to introduce the menu into popupArraycasecase "otherMenu"casecase else end select end sub [[code]]