Label/Title inside Popup Menu

Windows specific forum
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Label/Title inside Popup Menu

Post by tatanas »

Hi,

Do you know a way to create some Title inside a Popup Menu ?
The best would be a non selectable title that could be personnalized (font, size, color).

I would like this kind of Menu :


Cars (title)
______ (menubar)
Ferrari (menuitem)
Mercedes
Ford
______
Color (title)
______
Red (menuitem)
Blue
Grey

Thanks for your time.
Windows 10 Pro x64
PureBasic 6.20 x64
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Label/Title inside Popup Menu

Post by Caronte3D »

I don't know, but you can make your own popup with a simple window and put whatever you want inside.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Label/Title inside Popup Menu

Post by Axolotl »

Hi tatanas,

one way to go is using the #MFS_DEFAULT style on the menuitem.

Code: Select all

EnableExplicit
#Window = 0
#Menu = 0

Procedure OpenMainWindow() 
  Protected menustyle.MenuItemInfo 

  If OpenWindow(#Window, 8, 8, 560, 240, "Test ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  	If CreatePopupMenu(#Menu) 
  		MenuItem(1, "Cars")   ; Title 
  		MenuBar()
  		MenuItem(2, "Ferrari")
  		MenuItem(3, "Mercedes")
  		MenuItem(4, "Ford")
  		MenuBar()
  		MenuItem(5, "Color") ; Title 
  		MenuBar()
  		MenuItem(6, "Red") 
  		MenuItem(7, "Blue") 
  		MenuItem(8, "Grey") 
  		MenuBar()
  		MenuItem(9, "Close") 

; 
; Cars (title)
; ______ (menubar)
; Ferrari (menuitem)
; Mercedes
; Ford
; ______
; Color (title)
; ______
; Red (menuitem)
; Blue
; Grey
; 

      ; set Title items Bold 
      menustyle\cbSize = SizeOf(menustyle) 
      menustyle\fMask = #MIIM_STATE ;|#MIIM_STRING 
      menustyle\fState = #MFS_DEFAULT 
      SetMenuItemInfo_(MenuID(#Menu), 1, #False, menustyle)   ; make it bold 
      SetMenuItemInfo_(MenuID(#Menu), 5, #False, menustyle)   ; make it bold 

      ; and disable Title items 
      DisableMenuItem(#Menu, 1, #True)  ; no events 
      DisableMenuItem(#Menu, 5, #True)  ; no events 
    EndIf 	
    ProcedureReturn #True 
	EndIf
  ProcedureReturn #False 
EndProcedure  

If OpenMainWindow() 
	Repeat
		Select WaitWindowEvent()
		  Case #PB_Event_LeftClick 
		    DisplayPopupMenu(#Menu, WindowID(#Window)) 

			Case #PB_Event_Menu
  	    Debug " MenuItem #" + EventMenu() 

				If EventMenu() = 9 ; Close 
				  PostEvent(#PB_Event_CloseWindow) 
				EndIf 

			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf 


Or you can do it with one of RASHADs codes like this.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Label/Title inside Popup Menu

Post by Axolotl »

Hi, me again,

another way to do this is to ownerdraw the menu items like here.
It needs to be adapted to your needs.

Happy coding and stay healthy.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Label/Title inside Popup Menu

Post by ChrisR »

Axolotl wrote: Thu Feb 01, 2024 4:43 pm one way to go is using the #MFS_DEFAULT style on the menuitem.
Is there a way to have an Item (Title), in Bold, Disabled as well with CreatePopupImageMenu ?
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Label/Title inside Popup Menu

Post by tatanas »

Thank you Axolotl.

I was going to ask the same question as ChrisR.
I guess if it's not possible, ownerdraw will be the way.
Windows 10 Pro x64
PureBasic 6.20 x64
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Label/Title inside Popup Menu

Post by Axolotl »

Sorry for my delayed answer.

In addition to having a unique identifier, each menu item in a menu bar or menu has a unique position value.
try this:

Code: Select all


Procedure main() 
  Protected MII.MENUITEMINFO 

  If OpenWindow(0, 10, 10, 200, 100, "Menu Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateMenu(0, WindowID(0))    ; hier beginnt das Erstellen des Menüs... 
      MenuTitle("File") 
      MenuItem(1, "Open"   +Chr(9)+"Ctrl+O") 
      MenuItem(2, "Save"   +Chr(9)+"Ctrl+S") 
      MenuItem(3, "Save as"+Chr(9)+"Ctrl+A") 
      MenuItem(4, "Close"  +Chr(9)+"Ctrl+C") 
      MenuTitle("Edit") 
      MenuItem(10, "Test 10") 
      MenuItem(11, "Test 11") 
      MenuItem(12, "Test 12") 
      MenuTitle("Project") 
      MenuItem(20, "Test 20") 
      MenuItem(21, "Test 21") 
      MenuItem(22, "Test 22") 
    EndIf 

  ; set Title items Bold 
    MII\cbSize = SizeOf(MII) 
    MII\fMask = #MIIM_STATE 
    MII\fState = #MFS_DEFAULT 
  
    SetMenuItemInfo_(MenuID(0), 0, #True, MII)   ; make Title it bold 
    ; SetMenuItemInfo_(MenuID(0), 1, #True, MII)   ; make Title it bold 
    SetMenuItemInfo_(MenuID(0), 2, #True, MII)   ; make Title it bold 
;//                                #True --> use position instead of ID 

    MII\fState = #MFS_DEFAULT | #MFS_DISABLED  ; 
    SetMenuItemInfo_(MenuID(0), 3, #False, MII)   ; make Item it bold and disabled 

    MII\fState = #MFS_DEFAULT   
    SetMenuItemInfo_(MenuID(0), 4, #False, MII)   ; make Item it bold 
;//                                #False --> use ID instead of position 
    	
    DrawMenuBar_(WindowID(0))   ; we have to draw it now 

  ; main loop  
    Repeat
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow  
          Break 
        Case #PB_Event_Menu
          Debug "Menu #" + EventMenu() 
          If EventMenu() = 4 
            PostEvent(#PB_Event_CloseWindow) ; or break will do the trick as well..... 
          EndIf 
      EndSelect 
    ForEver 
  EndIf 

  Debug "bye."
EndProcedure 

main() 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Label/Title inside Popup Menu

Post by ChrisR »

Thanks for the nice Menu example
What I needed was the same as your 1st code, Set Title items Bold, Disabled, but for CreatePopupImageMenu() rather than CreatePopupMenu()

A workaround is to use CreatePopupMenu(#Menu) rather than the CreatePopupImageMenu(), then add the image with SetMenuItemBitmaps for each menu items

Code: Select all

EnableExplicit
#Window = 0
#Menu = 0
#MenuImage = 0
#MenuImageTitle = 1

LoadImage(#MenuImageTitle, #PB_Compiler_Home + "Examples\Sources\Data\Drive.bmp")
LoadImage(#MenuImage, #PB_Compiler_Home + "Examples\Sources\Data\File.bmp")

Macro _MenuItem(MenuItemID, Text, IDImage)
  MenuItem(MenuItemID, Text)
  SetMenuItemBitmaps_(MenuID(#Menu), MenuItemID, #MF_BYCOMMAND, IDImage, 0)
EndMacro

Procedure OpenMainWindow() 
  Protected menustyle.MenuItemInfo 
  
  If OpenWindow(#Window, 8, 8, 560, 240, "Test ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    ; Set Title items Bold, Disabled does not work with CreatePopupImageMenu(#Menu)
    ; Workaround, use CreatePopupMenu(#Menu) and for each items use SetMenuItemBitmaps Api to associates an image
    If CreatePopupMenu(#Menu)
      _MenuItem(1, "Cars", ImageID(#MenuImageTitle))
      MenuBar()
      _MenuItem(2, "Ferrari", ImageID(#MenuImage))
      _MenuItem(3, "Mercedes", ImageID(#MenuImage))
      _MenuItem(4, "Ford", ImageID(#MenuImage))
      MenuBar()
      _MenuItem(5, "Color", ImageID(#MenuImageTitle)) 
      MenuBar()
      _MenuItem(6, "Red", ImageID(#MenuImage)) 
      _MenuItem(7, "Blue", ImageID(#MenuImage))
      _MenuItem(8, "Grey", ImageID(#MenuImage)) 
      MenuBar()
      _MenuItem(9, "Close", ImageID(#MenuImage)) 
      
      ; set Title items Bold and disable it
      menustyle\cbSize = SizeOf(menustyle) 
      menustyle\fMask = #MIIM_STATE ;|#MIIM_STRING 
      menustyle\fState = #MFS_DEFAULT | #MFS_DISABLED  
      SetMenuItemInfo_(MenuID(#Menu), 1, #False, menustyle)   ; make it bold, disable 
      SetMenuItemInfo_(MenuID(#Menu), 5, #False, menustyle)   ; make it bold, disable 
    EndIf 	
    ProcedureReturn #True 
  EndIf
  ProcedureReturn #False 
EndProcedure  

If OpenMainWindow() 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_LeftClick 
        DisplayPopupMenu(#Menu, WindowID(#Window)) 
        
      Case #PB_Event_Menu
        Debug " MenuItem #" + EventMenu() 
        
        If EventMenu() = 9 ; Close 
          PostEvent(#PB_Event_CloseWindow) 
        EndIf 
        
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Label/Title inside Popup Menu

Post by tatanas »

Your workaround is pretty nice.

It could be a nice project for Thorsten1867 and his "Ex" modules :)
Windows 10 Pro x64
PureBasic 6.20 x64
Post Reply