Windows 11 style mismatch CreatePopupImageMenu

Just starting out? Need help? Post your questions and find answers here.
Rinzwind
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Windows 11 style mismatch CreatePopupImageMenu

Post by Rinzwind »

Image
Image

(Context) menu's without icons automatically get the newer Windows 11 design, but icon menu's keep the old look. Any workaround?
Last edited by Rinzwind on Tue Feb 18, 2025 2:05 pm, edited 8 times in total.
Rinzwind
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Windows 11 style mismatch CreatePopupImageMenu

Post by Rinzwind »

Apparently a menu with icons in PB is custom drawn. Why? Hence it does not match Windows standard menu appearance anymore.
Rinzwind
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Windows 11 style mismatch CreatePopupImageMenu

Post by Rinzwind »

I got deepseek to take a look at it with the command to replace the PB menu gadget in the 'Image Popup-Menu Example' code in the help with win32 calls.
Could of course have done that myself, but deepseek did a good job with this basic task. With more complex tasks it fails (sometimes pointing at mistakes helps), it fails especially when a solution can not be readily found online already. Still, that maybe can give a hint in the direction of a solution. Anyway, indeed it displays fine afterwards. So why the custom-draw? Needed in XP times? Then it can be replaced by now with native calls? Event Windows 11 itself changed its context menu style during the various updates.
Image

Code: Select all

UsePNGImageDecoder()

; Load icons
If CreateImage(0, 16, 16, 32)
  StartDrawing(ImageOutput(0))
  Box(0, 0, 15, 15, RGB(255, 255, 128)) ; Yellow box
  DrawRotatedText(-5, 3, "+", 45, RGB(255, 0, 128)) ; Rotated text
  StopDrawing()
EndIf

If LoadImage(1, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png") ; Load an icon
  ; Create a popup menu using Win32 API
  hMenu = CreatePopupMenu_()
  If hMenu
    ; Add menu items with icons
    AppendMenu_(hMenu, #MF_STRING, 1, "Open")
    SetMenuItemBitmaps_(hMenu, 1, #MF_BYCOMMAND, ImageID(1), 0) ; Set icon for "Open"

    AppendMenu_(hMenu, #MF_STRING, 2, "Save")
    AppendMenu_(hMenu, #MF_STRING, 3, "Save as")

    AppendMenu_(hMenu, #MF_STRING, 4, "Quit")
    SetMenuItemBitmaps_(hMenu, 4, #MF_BYCOMMAND, ImageID(0), 0) ; Set icon for "Quit"

    ; Add a separator
    AppendMenu_(hMenu, #MF_SEPARATOR, 0, "")

    ; Add a submenu
    hSubMenu = CreatePopupMenu_()
    If hSubMenu
      AppendMenu_(hSubMenu, #MF_STRING, 5, "PureBasic.exe")
      AppendMenu_(hSubMenu, #MF_STRING, 6, "Test.txt")
      AppendMenu_(hMenu, #MF_POPUP, hSubMenu, "Recent files")
    EndIf
  EndIf
EndIf

; Open a window
OpenWindow(0, 200, 200, 200, 120, "Win32 Popup-Menu Example")

Repeat
  Event = WaitWindowEvent() ; Check for window events

  Select Event
    Case #PB_Event_RightClick ; Right mouse button clicked
      ; Display the popup menu at the cursor position
      GetCursorPos_(@pt.POINT)
      TrackPopupMenu_(hMenu, 0, pt\x, pt\y, 0, WindowID(0), #Null)

    Case #PB_Event_Menu ; A menu item was clicked
      Select EventMenu() ; Get the clicked menu item
        Case 1 : Debug "Menu: Open"
        Case 2 : Debug "Menu: Save"
        Case 3 : Debug "Menu: Save as"
        Case 4 : End
        Case 5 : Debug "Menu: PureBasic.exe"
        Case 6 : Debug "Menu: Test.txt"
      EndSelect
  EndSelect
Until Event = #PB_Event_CloseWindow

; Clean up
If hMenu
  DestroyMenu_(hMenu)
EndIf
If hSubMenu
  DestroyMenu_(hSubMenu)
EndIf
Side note: DestroyMenu_(hSubMenu) is not needed, because per MSDN: "DestroyMenu is recursive, that is, it will destroy the menu and all its submenus."

PB original code

Code: Select all

 UsePNGImageDecoder()

  If CreateImage(0,16,16,32)
    StartDrawing(ImageOutput(0))
    Box(0,0,15,15,RGB(255,255,128))
    DrawRotatedText(-5,3, "+", 45, RGB(255,0,128))
    StopDrawing()
  EndIf

  OpenWindow(0, 200, 200, 200, 120, "Image Popup-Menu Example")

  If LoadImage(1,#PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png")  ; load an icon
    If CreatePopupImageMenu(0)      ; creation of the pop-up menu begins...
      MenuItem(1, "Open", ImageID(1))      ; Display the icon
      MenuItem(2, "Save")      ; just like in a normal menu...
      MenuItem(3, "Save as")
      MenuItem(4, "Quit", ImageID(0)) ; Display our own icon
      MenuBar()
      OpenSubMenu("Recent files")
        MenuItem(5, "PureBasic.exe")
        MenuItem(6, "Test.txt")
      CloseSubMenu()
    EndIf

    Repeat
      Event = WaitWindowEvent()     ; check for window events

      Select Event
        Case #PB_Event_RightClick  ; right mouse button was clicked =>
          DisplayPopupMenu(0, WindowID(0))  ; now display the popup-menu

        Case #PB_Event_Menu        ; an item of the popup-menu was clicked
          Select EventMenu()       ; get the clicked menu item...
            Case 1 : Debug "Menu: Open"
            Case 2 : Debug "Menu: Save"
            Case 3 : Debug "Menu: Save as"
            Case 4 : End
            Case 5 : Debug "Menu: PureBasic.exe"
            Case 6 : Debug "Menu: Text.txt"
          EndSelect

      EndSelect
    Until Event = #PB_Event_CloseWindow
  EndIf
Rinzwind
Enthusiast
Enthusiast
Posts: 702
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Windows 11 style mismatch CreatePopupImageMenu

Post by Rinzwind »

Please move to feature requests. Thanks.
Post Reply