MenuBar BackColor

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: MenuBar BackColor

Post by boddhi »

Denis wrote: Bienvenue au club :mrgreen:
L'esprit français aura fini par presque avoir raison de lui. :(
Je lance une idée : Tel Guillaume le Conquérant, notre vaillant ancêtre, lançons-nous à l'assaut des terres réseautiques anglophones ! :D

PS: For English-speakers, it's a joke! No need to send us armies :mrgreen:
PS to PS: KCC, you've published your reply while I was writing mine. I think I've already read the first part of your message somewhere...
As for the second part, I think it can be summed up in my first sentence. :( :mrgreen:
Last edited by boddhi on Tue Jul 16, 2024 8:18 pm, edited 1 time in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MenuBar BackColor

Post by Kwai chang caine »

Yes ! The hystory of French Forum is a long eventful history :mrgreen:
Ouaih...l'historique du Forum Français est une longue histoire mouvementée
ImageThe happiness is a road...
Not a destination
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: MenuBar BackColor

Post by boddhi »

Kwai chang caine wrote: Yes ! The hystory of French Forum is a long eventful history :mrgreen:
I know, I know. I've been following it since 2004 (although I only joined in 2010, in reaction to a very disdainful message from an older member, who was also a moderator at the time,and sometimes drew easily and vehemently).
 
Modos, sorry in advance for this "little" off-topic. :mrgreen:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Denis
Enthusiast
Enthusiast
Posts: 778
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: MenuBar BackColor

Post by Denis »

Here is a short article written by Raymond Chen about :
What states are possible in a DRAWITEMSTRUCT structure?
There is a summary table of different controls and their respective possible states.

Have a nice day!
A+
Denis
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MenuBar BackColor

Post by Kwai chang caine »

Thanks for the tips :wink:
ImageThe happiness is a road...
Not a destination
User avatar
Zapman
Enthusiast
Enthusiast
Posts: 205
Joined: Tue Jan 07, 2020 7:27 pm

Re: MenuBar BackColor

Post by Zapman »

Hi KCC,

It is probably to late :( , but here is a code doing what you asked.
Change line 64 to change the color of the menu bar background.

Code: Select all

Structure myMenuItem
      hFont.i
      text$
EndStructure

Structure MENUBARINFO Align #PB_Structure_AlignC
  cbSize.l
  rcBar.RECT
  hMenu.i
  hwndMenu.i
  fBarFocused.b
  fFocused.b
  fUnused.b
  Padding_1.b
EndStructure
    
Structure MENUINFO Align #PB_Structure_AlignC
  cbSize.l           ; Size of the structure (in bytes).
  fMask.l            ; Mask specifying which members are being set or retrieved.
  dwStyle.l          ; Style of the menu.
  cyMax.l            ; Maximum height of the menu, in pixels.
  hbrBack.i          ; Handle to the brush used to paint the menu background.
  dwContextHelpID.l  ; Context help ID for the menu.
  dwMenuData.i       ; Custom data associated with the menu.
EndStructure

;...Array of Menu item info
Global Dim menuItems.myMenuItem(2, 3)

Procedure myWindowCallback(hwnd, msg, wParam, lParam)
      Protected x, y, w, h
      Protected sz.SIZE
      
      Shared menuParent$
      result = #PB_ProcessPureBasicEvents
      
      
      If GetProp_(hWnd, "ISWndInitialized") = 0 And IsWindow(EventWindow()) And WindowID(EventWindow()) = hWnd
        ;
        ; While the command 'WindowEvent()' or 'WaitWindowEvent()' have not been
        ; executed, the value of 'EventWindow()' is not valid.
        ; Therefore, by testing 'IsWindow(EventWindow())', we have the opportunity to know
        ; the precise moment when the main loop of the program begins. This moment is
        ; the good one to make some check or to initialize something.
        ;
        If GetMenu_(hWnd)
          ; For the main menu of the window, PureBasic send the #WM_INITMENU message when
          ; the menu is created by CreateMenu(). At this moment, our callback procedure is
          ; generally not installed, so, it never receive the #WM_INITMENU for this menu.
          ; The following will resend this message at the moment the program enters into
          ; the main loop and begin to call 'WindowEvent()'.
          ;
          SetProp_(hWnd, "ISWndInitialized", 1)
          SendMessage_(hWnd, #WM_INITMENU, GetMenu_(hWnd), 0)
        EndIf
      EndIf
  
  
      Select msg
           Case #WM_INITMENU
             hMenu = wParam
             #MIM_BACKGROUND = $2
             Protected menuInfo.MENUINFO
             hBrush = CreateSolidBrush_($FF0000)
             menuInfo\cbSize = SizeOf(MENUINFO)
             menuInfo\hbrBack = hBrush
             menuInfo\fMask = #MIM_BACKGROUND
             SetMenuInfo_(hMenu, @menuInfo)
             DrawMenuBar_(hWnd)

           Case #WM_DRAWITEM
                  ;...Draw our menu items here
                  If wParam = 0
                        
                        *dis.DRAWITEMSTRUCT = lParam
                        If *dis\CtlType = #ODT_MENU
                              
                              *pMenuItem.myMenuItem = *dis\itemData
                              hOldFont = SelectObject_(*dis\hdc, *pMenuItem\hFont)
                              
                              
                              ;   SetBkColor_(*dis\hdc, #Black)
                              ;   SetTextColor_(*dis\hdc,#White)
                              ;    DrawTextEx_(*dis\hdc, *pMenuItem\text$, Len(*pMenuItem\text$),  *dis\rcItem, #DT_LEFT | #DT_NOCLIP, 0)
                              ;   SelectObject_(*dis\hdc, hOldFont)
                              
                              
                              If  *dis\itemID = 0  ; 1er élément du menu
                                    Brush = CreateSolidBrush_(#Red)
                              Else
                                    ; second
                                       Brush = CreateSolidBrush_($E0A030)
                              EndIf
                              
                           ;   Brush = CreateSolidBrush_(#Red)
                              SetTextColor_(*dis\hDC, #White)
                              FillRect_(*dis\hDC, *dis\rcItem, Brush)
                              SetBkMode_(*dis\hDC, #TRANSPARENT)
                              *dis\rcItem\Left + 2
                              DrawTextEx_(*dis\hDC, *pMenuItem\text$, -1, *dis\rcItem, #DT_EXPANDTABS | #DT_SINGLELINE | #DT_CENTER | #DT_VCENTER | #DT_NOCLIP, 0)
                              DeleteObject_(Brush)
                              
                        EndIf
                        
                  EndIf
                  
                  ProcedureReturn  #True
                  
                  ;- .  --->   #WM_MEASUREITEM
            Case #WM_MEASUREITEM
                  *mi.MEASUREITEMSTRUCT = lParam
                  If *mi\CtlType = #ODT_MENU
                        
                        If *mi\itemdata
                              hDC = GetDC_(hWnd)
                              If hDC
                                    SavedDC = SaveDC_(hDC)
                              EndIf
                              
                              ;*mi\itemID is the position of the menu item, 0 and 1 due to the use of
                              ; ModifyMenu_ and #MF_BYPOSITION
                              
                              ; using the font to calculate the item size
                              SelectObject_(hDC, menuItems(*mi\itemID, 0)\hFont)
                              
                              Protected Count = CountString(menuItems(*mi\itemID, 0)\text$, "&")
                              If Count = 0
                                    menuItems(*mi\itemID, 0)\text$ + "&"
                              EndIf
                              
                              ; get text size according hfont
                              GetTextExtentPoint32_(hDC, menuItems(*mi\itemID, 0)\text$, Len(menuItems(*mi\itemID, 0)\text$), @sz)
                              
                              *mi\itemWidth = sz\cx   ; largeur de l'élément
                              *mi\itemHeight = sz\cy  ; hauteur de l'élément
                              
                              If SavedDC
                                    RestoreDC_(hDC, SavedDC)
                              EndIf
                              
                              ReleaseDC_(hWnd, hDC)
                              ProcedureReturn 1
                              
                        EndIf
                  EndIf
      EndSelect
      
      ProcedureReturn result
      
EndProcedure

;-
;- .  --->   OpenWindow
OpenWindow(0, 10, 10, 300, 200, "OwnerDraw Menu", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
SetWindowCallback(@myWindowCallback())
hMenu = CreateMenu(0, WindowID(0))
MenuTitle("&File")
MenuItem(1, "&Open")
MenuItem(2, "&Save")
MenuItem(3, "E&xit")
MenuTitle("&Edit")
MenuItem(4, "&Cut")
MenuItem(5, "C&opy")
MenuItem(6, "&Paste")

;...Here we set ownerdraw for Menu Titles "File" and "Edit"
For i = 0 To 2
      menuItems(i, 0)\text$ = GetMenuTitleText(0, i)
      ModifyMenu_(hMenu, i, #MF_BYPOSITION | #MF_OWNERDRAW, i, menuItems(i ,0))
Next i

;...Show the menu changes
DrawMenuBar_(WindowID(0))

Repeat
      Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MenuBar BackColor

Post by Kwai chang caine »

Thanks a lot ZAPMAN for you help 8)
You have right, it's a little bit late, but it's always usefull to have another way of an desired effect :wink:
I keep your nice code for the next time :D
Have a good day
ImageThe happiness is a road...
Not a destination
User avatar
Zapman
Enthusiast
Enthusiast
Posts: 205
Joined: Tue Jan 07, 2020 7:27 pm

Re: MenuBar BackColor

Post by Zapman »

Kwai chang caine wrote: Mon Feb 03, 2025 7:28 pm Thanks a lot ZAPMAN for you help 8)
You have right, it's a little bit late, but it's always usefull to have another way of an desired effect :wink:
I keep your nice code for the next time :D
Have a good day
Sorry for the delay. I've just discovered today how to do.
Bonne soirée à toi aussi.
Post Reply