Post by Brandon Parker on Nov 13, 2021 14:32:33 GMT -5
Here is a method that could be employed to allow some more control for what one wants from a PopUpMenu. You just have to set up the selectMenuOption subroutine with the handler numbers you set up when you call the PopMenu subroutine. Pay attention to the comments in the code for explanation.
{:0)
Brandon Parker
'NoMainWin
'Create a structure so that we can keep track of our
'popup information
Struct PopMenuData, menuItems As ptr, _
menuItemHandlers As ptr, _
menuItemCount As long, _
menuHitTest As long, _
windowHeight As long, _
menuItemHeight As long, _
maxMenuItemWidth As long
'Create a string to pass the menu items we want to show
'The pipe character "|" will cause a separator line to be drawn between menu items
menuItems$ = "Hello World!";chr$(3);"|";chr$(3);"Goodbye World!";chr$(3);"Hello World 1!";chr$(3);"|";chr$(3);"Goodbye World 1!"
'Create a string that will be passed representing the menu item handler numbers
'Pass -1 in the location where a menu bar is placed since it will not have a handler number
'These values are required to be set up in the selectMenuOption Subroutine below; One Case X for each menu
'This one would be:
'Select Case
' Case 0
'
' Case 2
'
' Case 3
'
' Case 5
'
'Select Case
menuItemHandlers$ = "0";chr$(3);"-1";chr$(3);"2";chr$(3);"3";chr$(3);"-1";chr$(3);"5"
'Call the PopMenu subroutine with the following parameters: X Location, Y Location, menuItems, menuItemHandlers
Call PopMenu 50, 50, menuItems$, menuItemHandlers$
'Let's do some waiting to prevent attemping opening the popup menu again
'ONLY ONE popup menu can currently be opened at a time, but this could be changed although
'I do not see a need for it.
'Hit the ENTER key after selecting a popup menu to move on
Input a
'Create a string to pass the menu items we want to show
'The pipe character "|" will cause a separator line to be drawn between menu items
menuItems$ = "Test 2!";chr$(3);"|";chr$(3);"Test 2a!"
'Create a string that will be passed representing the menu item handler numbers
'Pass -1 in the location where a menu bar is placed since it will not have a handler number
'These values are required to be set up in the selectMenuOption Subroutine below; One Case X for each menu
'This one would be:
'Select Case
' Case 6
'
' Case 8
'
'Select Case
menuItemHandlers$ = "6";chr$(3);"-1";chr$(3);"8"
'Call the PopMenu subroutine with the following parameters: X Location, Y Location, menuItems, menuItemHandlers
Call PopMenu 500, 50, menuItems$, menuItemHandlers$
Wait
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Sub PopMenu xLoc, yLoc, menuItems$, menuItemHandlers$
UpperLeftX = xLoc
UpperLeftY = yLoc
PopMenuData.menuHitTest.struct = -1
PopMenuData.menuItems.struct = menuItems$
PopMenuData.menuItemHandlers.struct = menuItemHandlers$
PopMenuData.menuItemHeight.struct = 20
Call getMaxMenuWidth
Call getMenuItemCount
PopMenuData.windowHeight.struct = ((PopMenuData.menuItemHeight.struct * PopMenuData.menuItemCount.struct) + 6)
WindowWidth = (PopMenuData.maxMenuItemWidth.struct + 42)
WindowHeight = PopMenuData.windowHeight.struct
GraphicBox #PopMenu.gBoxPopUp, -1, -1, (WindowWidth + 2), (WindowHeight + 2)
StyleBits #PopMenu, _NULL, _WS_CAPTION Or _WS_SIZEBOX Or _WS_MINIMIZEBOX Or _WS_MAXIMIZEBOX Or _WS_VISIBLE, _WS_EX_TOPMOST, _NULL
Open "PopMenu" For Window_Popup As #PopMenu
#PopMenu.gBoxPopUp "AutoResize"
#PopMenu.gBoxPopUp "Down; Font Bold 8; BackColor Buttonface; Fill ButtonFace"
#PopMenu.gBoxPopUp "When leftButtonUp selectMenuOption"
#PopMenu.gBoxPopUp "When mouseMove menuHighlight"
For i = 1 To PopMenuData.menuItemCount.struct
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), i, chr$(3))
If (currentItem$ <> "|") Then
#PopMenu.gBoxPopUp "Color Black"
#PopMenu.gBoxPopUp "Place 20 ";((PopMenuData.menuItemHeight.struct * i) + 0)
#PopMenu.gBoxPopUp "|";currentItem$
Else
lineYPosition = ((PopMenuData.menuItemHeight.struct * i) + 0) - (PopMenuData.menuItemHeight.struct/ 5)
#PopMenu.gBoxPopUp "Color LightGray; Line 20 ";lineYPosition;" ";(PopMenuData.maxMenuItemWidth.struct + 34);" ";lineYPosition
End If
Next i
#PopMenu.gBoxPopUp "Place 1 1; Size 2; Color LightGray; Box ";(PopMenuData.maxMenuItemWidth.struct + 42);" ";(PopMenuData.windowHeight.struct)
#PopMenu.gBoxPopUp "Flush PopMenu"
#PopMenu "Show"
End Sub
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Sub selectMenuOption handle$, menuMouseX, menuMouseY
Close #PopMenu
selection = Val(Word$(Winstring(PopMenuData.menuItemHandlers.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3)))
Select Case selection
Case 0
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
Case 2
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
Case 3
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
Case 5
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
Case 6
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
Case 8
Print "Selected ";Word$(Winstring(PopMenuData.menuItems.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3))
'Keep adding your cases for other Popup menus as new Case X statements here...
Case Else
Print "Clicked on a bar or is outside the calculated area"
End Select
End Sub
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Sub menuHighlight handle$, menuMouseX, menuMouseY
selection = Val(Word$(Winstring(PopMenuData.menuItemHandlers.struct), (Int(menuMouseY/ PopMenuData.menuItemHeight.struct) + 1), chr$(3)))
newHit = Int(menuMouseY/ PopMenuData.menuItemHeight.struct)
If (newHit <> PopMenuData.menuHitTest.struct) Then
If (selection <> -1) And (newHit < PopMenuData.menuItemCount.struct) Then
PopMenuData.menuHitTest.struct = Int(menuMouseY/ PopMenuData.menuItemHeight.struct)
#PopMenu.gBoxPopUp "DelSegment menuHighlight"
#PopMenu.gBoxPopUp "Redraw PopMenu"
startYPosition = ((PopMenuData.menuItemHeight.struct * (Int(menuMouseY/ PopMenuData.menuItemHeight.struct))) + 3)
#PopMenu.gBoxPopUp "Place 3 ";startYPosition
#PopMenu.gBoxPopUp "Color 145 201 247; BackColor 145 201 247; BoxFilled ";(PopMenuData.maxMenuItemWidth.struct + 40);" ";(startYPosition + PopMenuData.menuItemHeight.struct + 1)
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), (PopMenuData.menuHitTest.struct + 1), chr$(3))
#PopMenu.gBoxPopUp "Color Black; BackColor 145 201 247;Font Bold 9"
#PopMenu.gBoxPopUp "Place 15 ";((PopMenuData.menuItemHeight.struct * PopMenuData.menuHitTest.struct) + 17)
#PopMenu.gBoxPopUp "|";currentItem$
#PopMenu.gBoxPopUp "Flush menuHighlight"
Else
PopMenuData.menuHitTest.struct = -1
#PopMenu.gBoxPopUp "DelSegment menuHighlight"
#PopMenu.gBoxPopUp "Redraw PopMenu"
End If
End If
End Sub
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Sub getMenuItemCount
PopMenuData.menuItemCount.struct = 0
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), (PopMenuData.menuItemCount.struct + 1), chr$(3))
While (currentItem$ <> "")
PopMenuData.menuItemCount.struct = (PopMenuData.menuItemCount.struct + 1)
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), (PopMenuData.menuItemCount.struct + 1), chr$(3))
Wend
End Sub
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Sub getMaxMenuWidth
GraphicBox #PopMenu.gBoxPopUp, 0, 0, 0, 0
StyleBits #PopMenu, _NULL, _WS_VISIBLE, _NULL, _NULL
Open "PopMenu" For Window_Popup As #PopMenu
#PopMenu.gBoxPopUp "Down; Font Bold 8"
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), (i + 1), chr$(3))
While (currentItem$ <> "")
#PopMenu.gBoxPopUp "StringWidth? currentItem$ widthInPixels"
PopMenuData.maxMenuItemWidth.struct = Max(PopMenuData.maxMenuItemWidth.struct, widthInPixels)
i = (i + 1)
currentItem$ = Word$(Winstring(PopMenuData.menuItems.struct), (i + 1), chr$(3))
Wend
Close #PopMenu
End Sub
{:0)
Brandon Parker