Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API Questions
#14
@ 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
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$ = ""
Reply


Messages In This Thread
API Questions - by TerryRitchie - 08-18-2023, 07:35 PM
RE: API Questions - by SpriggsySpriggs - 08-18-2023, 07:51 PM
RE: API Questions - by TerryRitchie - 08-18-2023, 07:55 PM
RE: API Questions - by SMcNeill - 08-18-2023, 10:36 PM
RE: API Questions - by SMcNeill - 08-18-2023, 10:45 PM
RE: API Questions - by SpriggsySpriggs - 08-18-2023, 10:48 PM
RE: API Questions - by TerryRitchie - 08-19-2023, 02:06 AM
RE: API Questions - by SpriggsySpriggs - 08-19-2023, 04:32 AM
RE: API Questions - by TerryRitchie - 08-19-2023, 03:32 PM
RE: API Questions - by Dav - 08-19-2023, 03:48 PM
RE: API Questions - by TerryRitchie - 08-19-2023, 03:53 PM
RE: API Questions - by SpriggsySpriggs - 08-19-2023, 09:10 PM
RE: API Questions - by TerryRitchie - 08-19-2023, 09:45 PM
RE: API Questions - by Jack - 08-19-2023, 10:45 PM
RE: API Questions - by TerryRitchie - 08-19-2023, 11:06 PM
RE: API Questions - by a740g - 08-20-2023, 12:01 AM
RE: API Questions - by TerryRitchie - 08-20-2023, 05:47 PM
RE: API Questions - by SMcNeill - 08-20-2023, 06:04 PM
RE: API Questions - by a740g - 08-20-2023, 06:32 PM
RE: API Questions - by SMcNeill - 08-20-2023, 06:08 PM
RE: API Questions - by Jack - 08-20-2023, 06:15 PM
RE: API Questions - by a740g - 08-20-2023, 06:20 PM
RE: API Questions - by SMcNeill - 08-20-2023, 06:22 PM
RE: API Questions - by a740g - 08-20-2023, 06:23 PM
RE: API Questions - by SpriggsySpriggs - 08-20-2023, 11:10 PM



Users browsing this thread: 1 Guest(s)