// Calculates the position of each MenuItem,
// returns the bounds of all MenuItems.
private Size MeasureItemBounds(Graphics g)
{
Size outside = Size.Empty;
itemBounds = new Rectangle[MenuItems.Count];
int y = MenuPaddingOrigin.Y;
for (int i = 0; i < MenuItems.Count; i++)
{
MenuItem item = MenuItems[i];
int width, height;
if (item.Text == "-")
{
height = -1;
width = 0;
}
else
{
if (item.OwnerDraw)
{
MeasureItemEventArgs measureItem = new MeasureItemEventArgs(g, i);
item.OnMeasureItem(measureItem);
width = measureItem.ItemWidth;
height = measureItem.ItemHeight;
}
else
{
// Do the default handling
SizeF size = g.MeasureString(item.Text, SystemInformation.MenuFont);
width = Size.Truncate(size).Width;
height = Size.Truncate(size).Height;
}
}
width += ItemPaddingSize.Width;
Rectangle bounds = new Rectangle(MenuPaddingOrigin.X, y, 0, height + ItemPaddingSize.Height);
itemBounds[i] = bounds;
y += bounds.Height;
if (outside.Width < width)
{
outside.Width = width;
}
}
if (outside.Width < MinimumItemWidth)
{
outside.Width = MinimumItemWidth;
}
for (int i = 0; i < MenuItems.Count; i++)
{
itemBounds[i].Width = outside.Width;
}
outside.Height = y + MenuPaddingSize.Height;
outside.Width += MenuPaddingSize.Width;
return(outside);
}