how to get width of a popup menu?

Is it possible to get the width of a popup menu before it is posted? I
have a button that calls PopupMenu to display a drop-down menu, and I
want the right edge of the menu to be aligned with the right edge of
the button when the menu drops down.

Hi,

Is it possible to get the width of a popup menu before it is posted? I
have a button that calls PopupMenu to display a drop-down menu, and I
want the right edge of the menu to be aligned with the right edge of
the button when the menu drops down.

Unfortunately no, but you can guesstimate it pretty close with a few
calculations.

1) Find the longest label in the menu and use GetTextExtent in a
memory dc or the window that is popping it up to get the on screen
width of the longest label.

2) If you have any checkbox/radio/bitmaps in the menu add 20. To
account for the item and its extra padding between it and the label.

3) If running on Windows or Mac add 32 to account for the extra
padding space menus have on these two platforms.

I have wanted to do the same thing you are trying and this has gotten
it close enough for me in most cases. There may be some special cases
(on GTK) depending on the theme where it won't be as on as one would
like.

Cody

···

On Tue, Oct 5, 2010 at 3:55 PM, Bryan Oakley <bryan.oakley@gmail.com> wrote:

Thanks, Cody. I guess that will have to be good enough. .

--bryan

···

On Tue, Oct 5, 2010 at 4:03 PM, Cody Precord <codyprecord@gmail.com> wrote:

Hi,

On Tue, Oct 5, 2010 at 3:55 PM, Bryan Oakley <bryan.oakley@gmail.com> wrote:

Is it possible to get the width of a popup menu before it is posted? I
have a button that calls PopupMenu to display a drop-down menu, and I
want the right edge of the menu to be aligned with the right edge of
the button when the menu drops down.

Unfortunately no, but you can guesstimate it pretty close with a few
calculations.

1) Find the longest label in the menu and use GetTextExtent in a
memory dc or the window that is popping it up to get the on screen
width of the longest label.

2) If you have any checkbox/radio/bitmaps in the menu add 20. To
account for the item and its extra padding between it and the label.

3) If running on Windows or Mac add 32 to account for the extra
padding space menus have on these two platforms.

I have wanted to do the same thing you are trying and this has gotten
it close enough for me in most cases. There may be some special cases
(on GTK) depending on the theme where it won't be as on as one would
like.

You could "cheat" a little bit by :

1) Call the popup;
2) Query its dimensions;
3) Immediately .Hide() it;
4) Change its dimensions and redraw it;
5) .Show() it.

There may, or may not, be a visible flicker of the popup window in its
first brief existence.

Ray Pasco

···

On Oct 5, 5:03 pm, Cody Precord <codyprec...@gmail.com> wrote:

Hi,

On Tue, Oct 5, 2010 at 3:55 PM, Bryan Oakley <bryan.oak...@gmail.com> wrote:
> Is it possible to get the width of a popup menu before it is posted? I
> have a button that calls PopupMenu to display a drop-down menu, and I
> want the right edge of the menu to be aligned with the right edge of
> the button when the menu drops down.

Hi,

···

On Wed, Oct 6, 2010 at 12:40 PM, Ray Pasco <pascor22234@gmail.com> wrote:

On Oct 5, 5:03 pm, Cody Precord <codyprec...@gmail.com> wrote:

Hi,

On Tue, Oct 5, 2010 at 3:55 PM, Bryan Oakley <bryan.oak...@gmail.com> wrote:
> Is it possible to get the width of a popup menu before it is posted? I
> have a button that calls PopupMenu to display a drop-down menu, and I
> want the right edge of the menu to be aligned with the right edge of
> the button when the menu drops down.

You could "cheat" a little bit by :

1) Call the popup;
2) Query its dimensions;
3) Immediately .Hide() it;
4) Change its dimensions and redraw it;
5) .Show() it.

No, you cant do that. Menus are not a true Window object, there is no
api to retrieve or modify its dimensions. Menu's also don't have a
Hide method and they are drawn by underlying toolkit not user code.

Cody