// ---------------------------------------------------------
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
// measure shortcut text
if (Shortcut != Shortcut.None)
{
string text = "";
int key = (int)Shortcut;
int ch = key & 0xFF;
if (((int)Keys.Control & key) > 0)
text += "Ctrl+";
if (((int)Keys.Shift & key) > 0)
text += "Shift+";
if (((int)Keys.Alt & key) > 0)
text += "Alt+";
if (ch >= (int)Shortcut.F1 && ch <= (int)Shortcut.F12)
text += "F" + (ch - (int)Shortcut.F1 + 1);
else
{
if (Shortcut == Shortcut.Del)
{
text += "Del";
}
else
{
text += (char)ch;
}
}
shortcuttext = text;
}
if (Text == "-")
{
e.ItemHeight = 8;
e.ItemWidth = 4;
return;
}
bool topLevel = Parent == Parent.GetMainMenu();
string tempShortcutText = shortcuttext;
if (topLevel)
{
tempShortcutText = "";
}
//int textwidth = (int)(e.Graphics.MeasureString(Text + tempShortcutText, SystemInformation.MenuFont).Width);
int textwidth = (int)(e.Graphics.MeasureString(Text + tempShortcutText, new Font("Tahoma", 9)).Width);
int extraHeight = 4;
e.ItemHeight = SystemInformation.MenuHeight + extraHeight;
if (topLevel)
e.ItemWidth = textwidth - 5;
else
e.ItemWidth = Math.Max(160, textwidth + 50);
// save menu item heihgt for later use
itemHeight = e.ItemHeight;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (doColorUpdate)
{
DoUpdateMenuColors();
}
base.OnDrawItem(e);
Graphics g = e.Graphics;
Rectangle bounds = e.Bounds;
bool selected = (e.State & DrawItemState.Selected) > 0;
bool toplevel = (Parent == Parent.GetMainMenu());
bool hasicon = Icon != null;
bool enabled = Enabled;
DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
if (hasicon)
DrawIcon(g, Icon, bounds, selected, Enabled, Checked);
else
if (Checked)
DrawCheckmark(g, bounds, selected);
if (Text == "-")
{
DrawSeparator(g, bounds);
}
else
{
DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);
}
}
public void DrawCheckmark(Graphics g, Rectangle bounds, bool selected)
{
int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE) / 2;
int checkLeft = bounds.Left + (STRIPE_WIDTH - BITMAP_SIZE) / 2;
ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Checkmark);
g.DrawRectangle(new Pen(borderColor), checkLeft - 1, checkTop - 1, BITMAP_SIZE + 1, BITMAP_SIZE + 1);
}