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?
Related Topics
Fonts for Windows, Linux and Mac on the Web
Article | 0 Comments
Color Values: Convert RGB, CMYK, CMY, HSV, TColor and XYZ Values
Tutorial | 0 Comments
Pipette: Fetch Colors from all over the Screen
Tutorial | 0 Comments
Delphi/Lazarus: How to set up a Link to a Website in an Application
Tutorial | 0 Comments
CSS: Colorize Table Rows Alternately only with CSS
Tutorial | 0 Comments
Delphi/Lazarus: Only allow Numbers in TEdit
Tip | 0 Comments
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.
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.
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:
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:
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
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
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