11 Vote

Delphi/Lazarus: How can I change font and color of Menu or PopupMenu?

Question by Abensberger | 2017-08-23 at 22:07

I want my PopupMenu to have a bigger font instead of the default font. But there is no font property with the PopupMenu. Any ideas how to use my own font/font-size?

ReplyPositiveNegativeDateVotes
2Best Answer2 Votes

In order to change the font, color or background of a TPopupMenu or TMainMenu, you have to draw the corresponding items on your own.

First, you have to change the property "OwnerDraw" of the PopupMenu or MainMenu to "true". You can also do that via the Object Inspector.

PopupMenu1.OwnerDraw := true;

Then, you have to care about the drawing that is carried out in the OnDrawItem procedure of each TMenuItem. For that, select one item after each other and double click the event OnDrawItem (you can also assign the same procedure to all items if you want the same drawing). This procedure gives you access to the item's canvas (ACanvas) on which you can draw and write everything you want:

procedure TForm1.MenuItem11DrawItem(Sender: TObject; 
  ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
var
  s: string;
begin
  // change font
  ACanvas.Font.Name := 'Consolas';
  ACanvas.Font.Size := 14;
  ACanvas.Font.Style := [fsBold];
  ACanvas.Font.Color := clYellow;
  // change background
  ACanvas.Brush.Color := clBlack;
  ACanvas.Rectangle(ARect);
  // write caption/text
  s := (Sender as TMenuItem).Caption;
  ACanvas.TextOut(ARect.Left + 2, ARect.Top + 2 , s);
end;

In this example, we are changing the font to Consolas in bold style, size 14 and yellow color with a black background. As caption, we are using the string stored in the property "Caption" and writing it with TextOut onto the canvas. However, we can also hard code something at this point.

Additionally, probably you also need the procedure OnMeasureItem of each MenuItem. This function is used to tell the system how large the MenuItem should be. Here is an example:

procedure TForm1.MenuItem11MeasureItem(Sender: TObject; 
  ACanvas: TCanvas; var Width, Height: Integer);
var
  s: string;
begin
  // calculate width and height of the menuitem
  s := (Sender as TMenuItem).Caption;
  Width := ACanvas.TextWidth(s);
  Height := ACanvas.TextHeight(s);
end;

As you can see, there are the variables Width and Height passed that can be changed to every value you want. Here, we are measuring the caption with ACanvas.TextWidth and ACanvas.TextHeight (the canvas is made available again) in order to provide as much space as you need for the text or caption. Again, of course you can also hard code some values here or make any other calculation you need.
2017-08-24 at 04:23

ReplyPositive Negative
11 Vote

Thanx for your answer concerning delphi.

Unfortuately I am using Lazarus and there is no OwnerDraw property of TPopUpMenu.
2017-08-25 at 20:23

ReplyPositive Negative
00 Votes

It should be a question of time until Lazarus is supporting this way, too. I have heard that this functionality is already available in Lazarus Trunc. Perhaps you can use the Trunc version for realizing your project.

Otherwise, you can of course also help to improve Lazarus and help making the function available. Or you go with another way. Instead of using the PopupMenu, you can for example build your own PopUp. For this, just take a normal TForm and show it after a right button click without borders at the place of the cursor. So, you should be able to place everything you want on this "PopupMenu".
2017-08-25 at 22:14

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.