Recent

Author Topic: TMenu  (Read 2478 times)

seghele0

  • Full Member
  • ***
  • Posts: 173
TMenu
« on: February 08, 2021, 05:09:05 pm »
Hello
Windows10 + Lazarus.
How can I adjust the font.size and font.name of yhe menu (+ items) via a button.
 ::)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TMenu
« Reply #1 on: February 08, 2021, 11:56:29 pm »
Hi!

Look at this discussion from 2016:

https://forum.lazarus.freepascal.org/index.php?topic=32056.0

Winni

PS .: The "Search" button in the header of this page is not only for decoration!

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: TMenu
« Reply #2 on: February 09, 2021, 09:48:35 am »
 ;)
My knowledge in programming is of a very low level.
Is it possible to give a clear example of the code for this button?
Thank you.
 

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: TMenu
« Reply #3 on: February 09, 2021, 10:12:24 am »
With a simple Button's OnClick this is not going to happen...

Google "delphi main menu font size" gives the following examples:

https://www.tek-tips.com/viewthread.cfm?qid=1360646
http://www.delphigroups.info/2/e5/484469.html
https://www.askingbox.com/question/delphi-lazarus-how-can-i-change-font-and-color-of-menu-or-popupmenu

Basically set OwnerDraw to True and implement OnDrawItem and OnMeasureItem for each TMenuItem.
You can do that (attach the event to the TMenuItem) in the ObjectInspector, or in code.

Doing it in code also gives you the possibility to turn that off in code (Set OwnerDraw to False and the events to nil).

Bart

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TMenu
« Reply #4 on: February 09, 2021, 10:21:59 am »
I wonder why there is a Screen.MenuFont. Unfortunately it does not seem to be respected for menu painting. What is it good for?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TMenu
« Reply #5 on: February 09, 2021, 12:08:03 pm »
I wonder why there is a Screen.MenuFont. Unfortunately it does not seem to be respected for menu painting. What is it good for?

Like the other xxxFont properties, it's supposed to do just that: change the corresponding font. Whether it really does or not, though, depends quite a lot on the widgetset, desktop theme, etc. so it's not a very reliable way to do it.

It might posibly work for custom drawn controls, but I don't know for sure. :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TMenu
« Reply #6 on: February 09, 2021, 05:29:36 pm »
Windows10 + Lazarus.
How can I adjust the font.size and font.name of yhe menu (+ items) via a button.
On Delphi it is as simple as this (for the submenus, not the mainmenu at the top)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Screen.MenuFont.Size := 30;
  4.   MainMenu1.OwnerDraw := True;
  5. end;
On Lazarus this doesn't work.

On Lazarus you'll need to use the MenuDrawItem and MenuMeasureItem.
But even then... making the font bigger has some side effects (just try it) and will need a lot more work to get working correctly.
For using a different font it works fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainMenu1DrawItem(Sender: TObject; ACanvas: TCanvas;
  2.   ARect: TRect; AState: TOwnerDrawState);
  3. begin
  4.   ACanvas.Font.Name := 'Times New Roman';
  5.   ACanvas.Font.Size := 12;
  6.   ACanvas.Brush.Style := bsClear;
  7.   ACanvas.TextOut(ARect.left, ARect.Top, TMenuItem(Sender).Caption);
  8. end;
  9.  
  10. procedure TForm1.MainMenu1MeasureItem(Sender: TObject; ACanvas: TCanvas;
  11.   var AWidth, AHeight: Integer);
  12. var
  13.   NewSize: TSize;
  14. begin
  15.   ACanvas.Font.Name := 'Times New Roman';
  16.   ACanvas.Font.Size := 12;
  17.   AWidth := ACanvas.TextWidth(TMenuItem(Sender).Caption);
  18.   AHeight := ACanvas.TextHeight('Tg');
  19. end;

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TMenu
« Reply #7 on: February 09, 2021, 07:54:10 pm »
Windows10 + Lazarus.
How can I adjust the font.size and font.name of yhe menu (+ items) via a button.
On Delphi it is as simple as this (for the submenus, not the mainmenu at the top)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Screen.MenuFont.Size := 30;
  4.   MainMenu1.OwnerDraw := True;
  5. end;
On Lazarus this doesn't work.
Ah, I only had checked the top menu. But what an ugly menu is this with normal font at the top and different font in the dropdowns?

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: TMenu
« Reply #8 on: February 10, 2021, 11:49:10 am »
 :)
rvk, Thanks.
It works !!!! (see attachment).

I had to remove:
var   NewSize: TSize;
Reason: Error Identifier not found "TSize"
I suppose that it was not needed!

Also an extra hint:
Parameter "AState" not used.
Can this be solved ?


rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TMenu
« Reply #9 on: February 10, 2021, 12:14:01 pm »
NewSize was indeed not needed anymore (it was a leftover because I combined two examples).

The hint for AState is something from Lazarus.
AState isn't used and FPC complains about parameters that aren't used.
But it can't hurt (only way to get rid of it is locally disable the hint)


Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainMenu1DrawItem(Sender: TObject; ACanvas: TCanvas;
  2.   ARect: TRect; {%H-}AState: TOwnerDrawState);

You can also disable the specific "parameter not used" hint for your whole program. See here for the setting https://wiki.freepascal.org/Turn_warnings_and_hints_on_or_off#Global_IDE_option

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: TMenu
« Reply #10 on: February 10, 2021, 12:43:15 pm »
Thanks.
 ;)

 

TinyPortal © 2005-2018