Search code examples
delphifontsdelphi-5popupmenu

Changing Popup Row Height after Font Size


I need my Visually impaired User to be able select a font size and mostly I have it handled OK, but Popup Menu is not working well as the Row height is not changed with Font Size.

Using this...

puMenuMain.OwnerDraw:=True;
Screen.MenuFont.Size:=18;  // Actually selected from list by User or Helper

Works well for the Font Size, but the Row height is not changed. In other Components such as TDBGrid, a Font.Size change also changes the Row Height.

How can I get the Popup Menus to adjust the Row Height for the selected Font.Size?


Solution

  • The documentation for OwnerDraw property for TPopupMenu states:

    When OwnerDraw is true, menu items receive an OnMeasureItem and an OnDrawItem event when they need to be rendered on screen.

    So assign a handler for OnMeasureItem of the items of the popup menu either at design time, or at run time:

    puMenuMain.OwnerDraw:=True;
    Screen.MenuFont.Size:=18; 
    for i := 0 to puMain.Items.Count - 1 do
      puMain.Items[i].OnMeasureItem := PopupMeasureItem;
    

    where PopupMeasureItem can be as simple as

    procedure TMyForm.PopupMeasureItem(Sender: TObject; ACanvas: TCanvas;
      var Width,   Height: Integer); 
    begin
      Height := ACanvas.TextHeight('.') + 2;
    end;
    

    or you can determine the necessary height as the user selects from the list to save calling TextHeight each time an item is to be drawn.