Jump to content
Incus J

Calculate the width of a Popup Menu?

Recommended Posts

I'd like to calculate the width of a Popup Menu before it is displayed, so that I can position and align it accurately when I call its .Popup(x,y) method.

function CalcPopupMenuWidth(popup : TPopupMenu) : single;
var
  i : integer;
  w : single;
  item : TMenuItem;
begin
  result := 0;
  for i := 0 to popup.ItemsCount-1 do begin
    item := popup.Items[i];
    w := popup.Items[i].Width;
    if w > result then result := w;
  end;
end;

The above approach iterates over the individual menu items, to determine the widest item.  It almost works, in that it calculates the width correctly, but only after the Popup Menu has been displayed once.  If the Popup Menu has never been displayed (yet) then this code always returns a value of 50.  I'm guessing maybe that's a default width for a TMenuItem, which gets updated with the actual width shortly before it is displayed.

 

How can I calculate the dimensions of a Popup Menu before displaying it?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×