Recent

Author Topic: [Solved] How to change popup menu row background color?  (Read 652 times)

loaded

  • Hero Member
  • *****
  • Posts: 853
[Solved] How to change popup menu row background color?
« on: September 19, 2023, 11:15:08 am »
Hi All,
In a project I'm working on, I have a popup menu with a lot of content. I want to change the background colors so that I can easily find the ones I use most.
Is it possible to do this with simple methods?
« Last Edit: September 19, 2023, 01:19:52 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to change popup menu row background color?
« Reply #1 on: September 19, 2023, 12:41:52 pm »
This is how you can colorize your items:
1. Create your MenuItems with Designer and apply to each Item that you want to colorize a different Tag number, let it 0 to be unchanged
2. Create a OnDraw handler for the PopupMenu1
3. Copy and paste this code into that handler:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1DrawItem(Sender: TObject; ACanvas: TCanvas;
  2.   ARect: TRect; AState: TOwnerDrawState);
  3. var
  4.   MenuItem: TMenuItem;
  5. begin
  6.   if (Sender is TMenuItem) then
  7.     begin
  8.       MenuItem := (Sender as TMenuItem);
  9.       ACanvas.Brush.Style := bsClear;
  10.       case MenuItem.Tag of
  11.         1: ACanvas.Brush.Color := clRed;
  12.         2: ACanvas.Brush.Color := clSkyBlue;
  13.         3: ACanvas.Brush.Color := clMoneyGreen;
  14.         else
  15.           ACanvas.Brush.Color := clNone;
  16.       end;
  17.       ACanvas.FillRect(ARect);
  18.       ACanvas.TextRect(ARect, ARect.Left + 5, ARect.Top + 2, MenuItem.Caption);
  19.     end;
  20. end;
4. Remove the handler from PopupMenu1 again
5. Assign this handler now to all MenuItems
6. Run app.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

loaded

  • Hero Member
  • *****
  • Posts: 853
Re: How to change popup menu row background color?
« Reply #2 on: September 19, 2023, 01:19:40 pm »
Thank you very much for your answer KodeZwerg
This trick never occurred to me; I think it's because of old age.  ;D
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] How to change popup menu row background color?
« Reply #3 on: September 19, 2023, 02:14:05 pm »
Thank you very much for your answer KodeZwerg
This trick never occurred to me; I think it's because of old age.  ;D
You are welcome, lets expand this "trick" a little more:
Code: Pascal  [Select][+][-]
  1. var
  2.   MenuItem: TMenuItem;
  3. begin
  4.   if (Sender is TMenuItem) then
  5.     begin
  6.       MenuItem := (Sender as TMenuItem);
  7.       ACanvas.Brush.Style := bsClear;
  8.       case MenuItem.Tag of
  9.         1: if odSelected in AState then
  10.              ACanvas.Brush.Color := clHighlight
  11.            else
  12.              ACanvas.Brush.Color := clRed;
  13.         2: if odSelected in AState then
  14.              ACanvas.Brush.Color := clHighlight
  15.            else
  16.              ACanvas.Brush.Color := clSkyBlue;
  17.         3: if odSelected in AState then
  18.              ACanvas.Brush.Color := clHighlight
  19.            else
  20.              ACanvas.Brush.Color := clMoneyGreen;
  21.         else
  22.           if odSelected in AState then
  23.              ACanvas.Brush.Color := clHighlight
  24.            else
  25.              ACanvas.Brush.Color := clNone;
  26.       end;
  27.       if odSelected in AState then
  28.         ACanvas.Font.Style := [fsBold]
  29.       else
  30.         ACanvas.Font.Style := [];
  31.       ACanvas.FillRect(ARect);
  32.       ACanvas.TextRect(ARect, ARect.Left + 5, ARect.Top + 2, MenuItem.Caption);
  33.     end;
  34. end;
Now the items are colored differently while you hover with mouse or cursor-keys over an item (and the font is a little bigger)
Feel free to add more different things for different states, like odDisabled ... let your imagination no limits on that  :-*
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018