3

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?

2 Answers2

5

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.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
2
  1. Add an empty ImageList component to your form.
  2. Set its Height property to the value you need.
  3. Associate Images property of your PopupMenu with this ImageList.
  4. Profit! :)
kot-da-vinci
  • 1,152
  • 16
  • 30