08-19-2023, 10:45 PM
@ TerryRitchie
there were a number of places in your code that seemed to needed to be _offset instead of a long, plus you missed hbmpItem in the Type MENUITEMINFO
here is your altered code, it successfully adds the menu item and silently crashes or simply vanishes
there were a number of places in your code that seemed to needed to be _offset instead of a long, plus you missed hbmpItem in the Type MENUITEMINFO
here is your altered code, it successfully adds the menu item and silently crashes or simply vanishes
Code: (Select All)
DefLng A-Z
Const MIIM_STATE = &H1
Const MIIM_ID = &H2
Const MIIM_TYPE = &H10
Const MFT_SEPARATOR = &H800
Const MFT_STRING = &H0
Const MFS_ENABLED = &H0
Const MFS_CHECKED = &H8
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
'-----------------------------------------------------------------------------------
Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As _Offset
hbmpChecked As _Offset
hbmpUnchecked As _Offset
dwItemData As _Offset
dwTypeData As _Offset
cch As Long
hbmpItem As _Offset
End Type
Declare Library
Function FindWindow& (ByVal ClassName As _Offset, WindowName$) ' To get hWnd handle
End Declare
Declare Dynamic Library "user32"
Function CreateMenu%& ()
Function DrawMenuBar (ByVal hWnd As _Offset)
Function SetMenu& (ByVal hWnd As _Offset, Byval hMenu As _Offset)
Function InsertMenuItemA& (ByVal hMenu As _Offset, Byval uItem As _Unsigned Long, Byval fByPosition&, Byval lpmii As _Offset)
Function GetMenuItemCount& (ByVal hMenu As _Offset)
Function GetMenuItemInfoA& (ByVal hMenu As _Offset, Byval uItem As _Unsigned Long, Byval fByPosition&, Byval lpmii As _Offset)
End Declare
Dim hWnd As _Offset
Dim hMenu As _Offset
Dim MenuItem As MENUITEMINFO, BlankMenuItem As MENUITEMINFO
Dim TypeData As String * 1000
_Title "Menu bar API demo"
hWnd = _WindowHandle 'FindWindow(0, "Menu bar API demo" + CHR$(0))
hMenu = CreateMenu: BlankMenuItem.cbSize = Len(BlankMenuItem)
Color 7, 1: Cls
'Add a separator bar
count = GetMenuItemCount(hMenu): Print "MenuItemCount:"; count
MenuItem = BlankMenuItem
MenuItem.fMask = MIIM_ID Or MIIM_TYPE
MenuItem.fType = MFT_SEPARATOR
MenuItem.wID = count
If InsertMenuItemA(hMenu, count, 1, _Offset(MenuItem)) Then Print "Successfully added menu item!" Else Print "Failed to add menu item!": End
'Add a button
MenuItem = BlankMenuItem
count = GetMenuItemCount(hMenu): Print "MenuItemCount:"; count
MenuItem.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
MenuItem.fType = MFT_STRING
MenuItem.fState = MFS_ENABLED
MenuItem.wID = count
TypeData = "&Fire Laser!" + Chr$(0)
MenuItem.dwTypeData = _Offset(TypeData)
MenuItem.cch = Len(MenuItem.dwTypeData)
MyButton = MenuItem.wID
If InsertMenuItemA(hMenu, count, 1, _Offset(MenuItem)) Then Print "Successfully added menu item!" Else Print "Failed to add menu item!": End
If SetMenu(hWnd, hMenu) Then Print "Successfully set menu!": Print "Menu handle is:"; hMenu Else Print "Failed to set menu!": End
Do: _Limit 70
prev_state = new_state
ok = GetMenuItemInfoA(hMenu, MyButton, 1, _Offset(MenuItem))
new_state = MenuItem.fState And 128
If prev_state = 0 And new_state <> 0 Then Print "Ouch! ";
Loop While InKey$ = ""