Page 1 of 2

Icons in submenues?

Posted: Fri Sep 02, 2005 4:54 pm
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!

Posted: Fri Sep 02, 2005 5:07 pm
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"

Posted: Fri Sep 02, 2005 9:01 pm
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 :)

Posted: Sat Sep 03, 2005 2:35 am
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. ;)

Posted: Sat Sep 03, 2005 3:06 am
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

Posted: Sat Sep 03, 2005 9:44 am
by Jellybean
The submenu IS what I put icons into. But I understand what you want now.

Posted: Sat Sep 03, 2005 11:12 am
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

Posted: Sat Sep 03, 2005 11:29 am
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.

Posted: Sat Sep 03, 2005 11:59 am
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.

Posted: Sat Sep 03, 2005 4:29 pm
by ricardo
Thanks!!

Posted: Sun Sep 04, 2005 3:46 pm
by Sparkie
Thanks GreenGiant. :)

Posted: Sun Sep 04, 2005 5:27 pm
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?

Posted: Sun Sep 04, 2005 7:27 pm
by Jellybean
I think Office-style menus are ownerdrawn. But that's just what I think.

Posted: Sun Sep 04, 2005 7:43 pm
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. ;)

Posted: Sun Sep 04, 2005 7:48 pm
by ricardo
Thanks!!

I guess there is much room for shareware PB libs on all related to GUI.