Easy custom menu item icons/bitmaps (no need for callback)

Share your advanced PureBasic knowledge/code with the community.
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Easy custom menu item icons/bitmaps (no need for callback)

Post by kawasaki »

Code updated For 5.20+

Stumbled across this API command whilst looking for something else... No doubt someone else has pointed it out... But just in case;

SetMenuItemBitmaps_(MenuID,ItemID,Flags,ImgID1,ImgID2)

MenuID : is what it says.. MenuID(),
ItemID : Using #MF_BYCOMMAND as the flag, can be the item number,
Flags : Flags for the procedure (recommend using #MF_BYCOMMAND),
ImgID1 : The image to show when the item is checked,
ImgID2 : The image to show when the item is unchecked.

I dont know what the maximum size image allowed is, but i stick to 16x16.

Example;

Code: Select all

; Custom menu item image
; Example by Michael R King

OpenWindow(0,0,0,640,480,"Test",#PB_Window_ScreenCentered)
CreateMenu(0,WindowID(0))
MenuTitle("Test")
MenuItem(0,"Test 1")
MenuItem(1,"Test 2")
MenuItem(2,"Test 3")

;- Create Unchecked Image
CreateImage(0,16,16)
StartDrawing(ImageOutput(0))
For x=0 To 15
  For y=0 To 15
    rs=Random(255)
    Plot(x,y,RGB(rs,0,0))
  Next
Next
StopDrawing()

;- Create Checked Image
CreateImage(1,16,16)
StartDrawing(ImageOutput(1))
For x=0 To 15
  For y=0 To 15
    gs=Random(255)
    Plot(x,y,RGB(0,gs,0))
  Next
Next
StopDrawing()

SetMenuItemBitmaps_(MenuID(0),0,#MF_BYCOMMAND,ImageID(0),ImageID(1))
SetMenuItemBitmaps_(MenuID(0),1,#MF_BYCOMMAND,ImageID(0),ImageID(1))
SetMenuItemBitmaps_(MenuID(0),2,#MF_BYCOMMAND,ImageID(0),ImageID(1))

Repeat
  Select WindowEvent()
    Case #PB_Event_Menu
      For Item=0 To 2
        If EventMenu()=Item
          If GetMenuItemState(0,Item)=0
            SetMenuItemState(0,Item,1)
          Else
            SetMenuItemState(0,Item,0)
          EndIf
        EndIf
      Next
    Case #PB_Event_CloseWindow
      End
  EndSelect
ForEver

User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Yup this one has been posted before. Actually, youve kind of prompted me to add this to my app I'm working on...

Youre the same Michael King on LLRGT right?
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post by kawasaki »

Yeah, this is the same one :wink:
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Hey does this method work with loaded images? Because I cant get it to work with bitmaps OR icons using catch() OR load() image.
Post Reply