Once, long ago and far away, Dav reminded me that I'd wrote a popup menu system for QB64. Unfortunately, time and loss of a few drives of mine (and lost of [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]) has resulted in that old work just disappearing and being lost to the ether. :(
Fortunately, since I wrote it once, it wasn't that hard to go back and rewrite the code once again for us!
DIM SHARED Options
(1 TO 100, 1 TO 100) AS STRING 'menu captions.. fist value is the menu number, second value is the captions
MainMenu = RegisterMenu
SetMenuBackground MainMenu, Blue, 2, Silver 'blue background with 2 pixel wide silver border
AddOption MainMenu, "File"
AddOption MainMenu, "Edit"
AddOption MainMenu, "View"
AddOption MainMenu, "Search"
AddOption MainMenu, "---" 'a divider
AddOption MainMenu, "Run"
AddOption MainMenu, "Options"
AddOption MainMenu, "---" 'a divider
AddOption MainMenu, "Help"
RMB = -1
RMB = 0
result = CheckMenu(MainMenu)
ClearMenu i 'make certain all old options are erased and blank
Menu(i).Handle = i
RegisterMenu = i 'assign a free handle to create a menu
END FUNCTION 'Return 0 if there's no open menu handles to work with
Menu(Handle).Handle = 0
Menu(Handle).Xpos = 0
Menu(Handle).Ypos = 0
Menu(Handle).Visible = 0
Menu(Handle).NumOptions = 0
Menu(Handle).BackGround = &HFF000000&&
Menu(Handle).TextColor = &HFFFFFFFF&&
FOR j
= 1 TO 100: Options
(Handle
, j
) = "":
NEXT
IF Options
(Handle
, j
) = "" THEN Menu(Handle).NumOptions = Menu(Handle).NumOptions + 1
Options(Handle, j) = Options$
Menu(Handle).BackGround = Background
Menu(Handle).BorderSize = Bordersize
Menu(Handle).BorderColor = BorderColor
IF _PIXELSIZE = 2 THEN EXIT SUB 'not converted to text coordinate system yet. Graphic screen menus only, at this time
X = Menu(Handle).Xpos
Y = Menu(Handle).Ypos
'calculate printwidth and printheight
FOR i
= 1 TO Menu
(Handle
).NumOptions
pw
= pw
+ 2 * _FONTWIDTH 'a border around either side IF X
+ pw
+ Menu
(Handle
).BorderSize
> _WIDTH THEN X
= _WIDTH - pw
- Menu
(Handle
).BorderSize
IF Menu
(Handle
).BackGround
> -1 THEN 'use a solid color as the backdrop to the menu LINE (X
, Y
)-STEP(pw
, ph
), Menu
(Handle
).BackGround
, BF
ELSE 'use an image as the backdrop to the menu FOR i
= 1 TO Menu
(Handle
).BorderSize
j = i - 1
LINE (X
- j
, Y
- j
)-STEP(pw
+ j
* 2, ph
+ j
* 2), Menu
(Handle
).BorderColor
, B
COLOR Menu
(Handle
).TextColor
, 0 FOR i
= 1 TO Menu
(Handle
).NumOptions
Caption$ = Options(Handle, i)
LINE (X
, Y
+ (i
- .5) * _FONTHEIGHT - Menu
(Handle
).BorderSize \
2)-STEP(pw
, Menu
(Handle
).BorderSize
), Menu
(Handle
).BorderColor
, BF
IF MX
>= X
AND MX
<= X
+ pw
AND MY
>= Y
AND MY
<= Y
+ ph
THEN 'the mouse is inside the menu area Where
= (MY
- Y
) \
_FONTHEIGHT + 1 'Which menu index are we currently hovering over? IF Options
(Handle
, Where
) <> "---" THEN CheckMenu = Where
done = -1
CheckMenu = 1000 + Where
done = -1
ELSE 'we clicked outside the designated menu area done = -1
done = -1
Note that this is currently a work-in-progress (is anything I ever work on ever really a finished product??), and is subject to alteration/expansion/development (and new bugs) in future releases.
Using it seems fairly self-explanatory, but I'll walk through the basics anyway...
MainMenu = RegisterMenu
Register a menu that you want to create
SetMenuBackground MainMenu, Blue, 2, Silver 'blue background with 2 pixel wide silver border
Set a few simple options for that menu to make it look pretty and fit into your program design.
AddOption MainMenu, "File"
AddOption MainMenu, "Edit"
AddOption MainMenu, "View"
AddOption MainMenu, "Search"
AddOption MainMenu, "---" 'a divider
AddOption MainMenu, "Run"
AddOption MainMenu, "Options"
AddOption MainMenu, "---" 'a divider
AddOption MainMenu, "Help"
Set the menu options that you want to appear on your menu.
result = CheckMenu(MainMenu)
And then later in the program, call that menu and get a result for it.
And that's the basics of the whole process! :D
For the demo, you call up the menu with a right-click anywhere on the screen, and then......
...I guess if you need me to tell you how to interact with a menu, there's no need for me to explain anything more! How the heck did you even manage to connect to the internet and find your way to this forum and this topic??! :P
More options will come in the future (such as keyboard support, custom text colors, highlighting, shadows, and other such things), but what's here now should be sufficient enough for people to play around with an make use of. (I probably need to add in the routine to free a menu handle sometime soon as well -- you can't do that yet either...)
Currently you can create and play around with up to 100 pop-up menus, with up to 100 options each (if the screen will display that many -- there's no error checking for if it won't!!), so have fun with it, test it out, and report if anything seems buggy or acts oddly at this point in development of it. ;)