Icons in submenues?

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Icons in submenues?

Post by ricardo »

Using sparkie's code below i can add icons to menus... but cant fin a way to add it to submenus, any idea?

Code: Select all

; --> get default menu check-mark bitmap size 
  menuImageWidth = GetSystemMetrics_(#SM_CXMENUCHECK) 
  menuImageHeight = GetSystemMetrics_(#SM_CYMENUCHECK) 
  #MIIM_CHECKMARKS = 8 
  Structure myMENUITEMINFO 
    cbSize.l 
    fMask.l 
    fType.l 
    fState.l 
    wID.l 
    hSubMenu.l 
    hbmpChecked.l 
    hbmpUnchecked.l 
    dwItemData.l 
    dwTypeData.l 
    cch.l 
    hbmpItem.l 
  EndStructure 
  myMenu.myMENUITEMINFO 
  myMenu\cbSize = SizeOf(myMENUITEMINFO) 
  myMenu\fMask = #MIIM_CHECKMARKS 
  Global myMenu, menuImageWidth, menuImageHeight 
  Procedure menuImage(subMenu, iID, ico) 
    ; --> Extract an icon 
    ExtractIconEx_("shell32.dll", ico, 0, @smIcons, 1) 
    ; --> Draw the icon onto a 16 x 16 blank image 
    CreateImage(iID, 16, 16) 
    himg = StartDrawing(ImageOutput()) 
    ; --> White background to match menu background 
    Box(0, 0, 16, 16, RGB(255, 255, 255)) 
    ; --> Draw the icon 
    DrawImage(smIcons, 0, 0, 16, 16) 
    StopDrawing() 
    ; --> Resize to fit the menu 
    itemImage = ResizeImage(iID, menuImageWidth, menuImageHeight) 
    ; --> Add the image to myMENUITEMINFO structure 
    myMenu\hbmpUnchecked = itemImage 
    ; --> Send the info to the menu item 
    SetMenuItemInfo_(subMenu, iID, 0, myMenu) 
    ; --> Clean up 
    DestroyIcon_(smIcons) 
  EndProcedure 
  If OpenWindow(0, 200, 200, 400, 110, #PB_Window_SystemMenu, "Set Menu Item Images") And CreateGadgetList(WindowID(0)) 
    hMenu = CreateMenu(0, WindowID()) 
    MenuTitle("Title 1") 
    MenuItem(0, "Testing 1") 
    MenuItem(1, "Testing 2") 
    MenuItem(2, "Testing 3") 
    MenuTitle("Title 2") 
    MenuItem(3, "Testing 4") 
    MenuItem(4, "Testing 5") 
    MenuItem(5, "Testing 6") 
    ; --> Set images for Title 1 sub items 
    hsMenu0 = GetSubMenu_(hMenu, 0) 
    menuImage(hsMenu0, 0, 4) 
    menuImage(hsMenu0, 1, 130) 
    menuImage(hsMenu0, 2, 194) 
    ; --> Set images for Title 2 sub items 
    hsMenu1 = GetSubMenu_(hMenu, 1) 
    menuImage(hsMenu1, 3, 43) 
    menuImage(hsMenu1, 4, 10) 
    menuImage(hsMenu1, 5, 90) 
    Repeat 
      event = WaitWindowEvent() 
    Until event = #PB_Event_CloseWindow 
  EndIf 
  End 
Thanks in advance!
ARGENTINA WORLD CHAMPION
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

I didn't bother to figure out how Sparkie's code works, because I already have my own approach that is a bit different sort of, I hope you don't mind. It mimics the standard menu example, only it adds icons.

Code: Select all

Procedure MenuItemEx(MenuItemID, Text$, MemoryAddress)
  MenuItem(MenuItemID, Text$)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    If MemoryAddress <> 0
      myBitmap = CatchImage(#PB_Any, MemoryAddress)
      myBitmap = ResizeImage(myBitmap, GetSystemMetrics_(#SM_CXMENUCHECK), GetSystemMetrics_(#SM_CYMENUCHECK), #PB_Image_Smooth)
      SetMenuItemBitmaps_(MenuID(), MenuItemID, 0, myBitmap, myBitmap)
    EndIf
  CompilerEndIf
EndProcedure

OpenWindow(0, 150, 150, 320, 240, #PB_Window_SystemMenu, "PureBasic - Windows BMP Menu")

If CreateMenu(0, WindowID())
  MenuTitle("&File")
    MenuItemEx( 1, "&Load...",    ?IconOpen)
    MenuItemEx( 2, "&Save",       ?IconSave)
    MenuItemEx( 3, "Save &As...", 0)
    MenuBar()
    MenuItemEx( 4, "&Quit",       ?IconQuit)
  MenuTitle("&Edit")
    OpenSubMenu("Basic")
      MenuItemEx( 5, "Cut",       ?IconCut)
      MenuItemEx( 6, "Copy",      ?IconCopy)
      MenuItemEx( 7, "Paste",     ?IconPaste)
    CloseSubMenu()
    OpenSubMenu("Font")
      MenuItemEx( 8, "Tahoma",    0)
  MenuTitle("&Information")
    MenuItemEx(11, "&About",      ?IconAbout)
EndIf

Repeat
  Select WaitWindowEvent()
    Case #PB_EventMenu
      Select EventMenuID()
        Case 11 ;About
          MessageRequester("About", "Really Cool Menu Example", 64)
        Case 7 ;Quit
          Quit = 1       
        Default
          MessageRequester("Info", "MenuItem: "+Str(EventMenuID()), 0)
      EndSelect

      Case #PB_Event_CloseWindow
        Quit = 1
  EndSelect
Until Quit = 1

End

IconSave: IncludeBinary "save.bmp"
IconOpen: IncludeBinary "open.bmp"
IconQuit: IncludeBinary "quit.bmp"
IconAbout:IncludeBinary "about.bmp"
IconCut:  IncludeBinary "cut.bmp"
IconCopy: IncludeBinary "copy.bmp"
IconPaste:IncludeBinary "paste.bmp"
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Thanks for you reply, but i dont see that submenus get an ico and thats what im looking for.

I need to put some image/icon, not in the item of the submenue, but in the own submenu.

See this example, they are showing some folder ico in the submenu, and webpage ico in the submenu items... well, i know how to put icons in the items, but not in the submenu itself.

Image

I hope i explain myself, and thanks again for your reply :)
ARGENTINA WORLD CHAMPION
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

@ricardo: I'll see if I can refresh my memory with that old code of mine and maybe come up with something that works for you. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Sparkie wrote:@ricardo: I'll see if I can refresh my memory with that old code of mine and maybe come up with something that works for you. ;)
Thanks!! :D
ARGENTINA WORLD CHAMPION
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

The submenu IS what I put icons into. But I understand what you want now.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Try this code

Code: Select all

; --> get default menu check-mark bitmap size
  menuImageWidth = GetSystemMetrics_(#SM_CXMENUCHECK)
  menuImageHeight = GetSystemMetrics_(#SM_CYMENUCHECK)
  Global menuImageWidth, menuImageHeight

  Procedure menuImage(subMenu, iID, ico)
    ; --> Extract an icon
    ExtractIconEx_("shell32.dll", ico, 0, @smIcons, 1)
    ; --> Draw the icon onto a 16 x 16 blank image
    im=CreateImage(#PB_Any, 16, 16)
    himg = StartDrawing(ImageOutput())
    ; --> White background to match menu background
    Box(0, 0, 16, 16, RGB(255, 255, 255))
    ; --> Draw the icon
    DrawImage(smIcons, 0, 0, 16, 16)
    StopDrawing()
    ; --> Resize to fit the menu
    itemImage = ResizeImage(im, menuImageWidth, menuImageHeight)
    ; --> Add the image to myMENUITEMINFO structure
    SetMenuItemBitmaps_(subMenu,iID,#MF_BYPOSITION,itemImage,0)
    ; --> Clean up
    DestroyIcon_(smIcons)
  EndProcedure
  If OpenWindow(0, 200, 200, 400, 110, #PB_Window_SystemMenu, "Set Menu Item Images") And CreateGadgetList(WindowID(0))
    hMenu = CreateMenu(0, WindowID())
    MenuTitle("Title 1")
    MenuItem(0, "Testing 1")
    MenuItem(1, "Testing 2")
    OpenSubMenu("Submenu")
      MenuItem(2, "Testing 3")
    MenuTitle("Title 2")
    MenuItem(3, "Testing 4")
    MenuItem(4, "Testing 5")
    MenuItem(5, "Testing 6")
    
    hsMenu0 = GetSubMenu_(hMenu, 0)
      menuImage(hsMenu0, 0, 4)
      menuImage(hsMenu0, 1, 130)
      menuImage(hsMenu0, 2, 194)
    hsMenu1=GetSubMenu_(hsMenu0,2)
      menuImage(hsMenu1,0,4)
    hsMenu2 = GetSubMenu_(hMenu, 1)
      menuImage(hsMenu2, 0, 43)
      menuImage(hsMenu2, 1, 10)
      menuImage(hsMenu2, 2, 90)
    Repeat
      event = WaitWindowEvent()
    Until event = #PB_Event_CloseWindow
  EndIf
  End
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

GreenGiant,

This works except that depending on the icon number in the stock, it can crash, so I just changed by testing smIcons :

Code: Select all

    If smIcons
    DrawImage(smIcons, 0, 0, 16, 16)
    Else
    DrawText("?")
    EndIf
Your code is nice.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Thanks fweil, didn't spot that. I can't really take credit for it being nice code though, I used Sparkie's from above and then modified it.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Thanks!!
ARGENTINA WORLD CHAMPION
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Thanks GreenGiant. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Sparkie wrote:Thanks GreenGiant. :)
Sparkie... i see that you have a lot of knowledge about this kind of stuff.
How can i make a menu Office style flavour?
ARGENTINA WORLD CHAMPION
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

I think Office-style menus are ownerdrawn. But that's just what I think.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Jellybean is right about using ownerdrawn menus. There is an example on the CodeArchive that might help you get started. http://www.purearea.net/pb/CodeArchiv/M ... or+Icon.pb

If I have some free time in the near future, I'll see if I can work something out for Office style menus. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Thanks!!

I guess there is much room for shareware PB libs on all related to GUI.
ARGENTINA WORLD CHAMPION
Post Reply