Fejl i menu med ikoner
Hej...denne kode gider ikke virke af en eller anden grund?
jeg har taget den fra microsoft, og kopieret den fra VB til C#
som jeg ser det, bliver OnMeasureItem ikke kaldt...
ms kode:
http://support.microsoft.com/?kbid=888168
public class IconMenuItem:System.Windows.Forms.MenuItem
{
private Icon icon;
private Font font;
public IconMenuItem(string menuText,EventHandler handler,Shortcut shortcut,Icon ico):base(menuText,handler,shortcut)
{
this.icon = icon;
this.font = SystemInformation.MenuFont;
this.OwnerDraw = true;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
StringFormat sFormat = new StringFormat();
sFormat.HotkeyPrefix = HotkeyPrefix.Show;
sFormat.SetTabStops(50,new Single[] {0});
if (this.icon.Height > this.font.Height)
e.ItemHeight = this.icon.Height+6;
else
e.ItemHeight = this.font.Height+6;
e.ItemWidth = (int)e.Graphics.MeasureString(AppendShortcut(),this.font,1000,sFormat).Width+this.icon.Width+5;
sFormat.Dispose();
sFormat = null;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
Brush br;
StringFormat sFormat;
base.OnDrawItem(e);
e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds);
if (this.icon != null)
e.Graphics.DrawIcon(this.icon, e.Bounds.Left + 3, e.Bounds.Top + 3);
sFormat = new StringFormat();
sFormat.HotkeyPrefix = HotkeyPrefix.Show;
sFormat.SetTabStops(50, new Single[] {0});
br = new SolidBrush(SystemColors.WindowText);
e.Graphics.DrawString(AppendShortcut(), this.font, br, e.Bounds.Left+this.icon.Width+10, e.Bounds.Top+ 2, sFormat);
br.Dispose();
br = null;
sFormat.Dispose();
sFormat = null;
}
private string AppendShortcut()
{
string s;
s = this.Text;
if (this.ShowShortcut && this.Shortcut != Shortcut.None)
{
Keys k = (Keys)Shortcut;
//GetType(k) should be GetType(Keys)
s += Convert.ToChar(9)+TypeDescriptor.GetConverter(k.GetType()).ConvertToString(k);
}
return s;
}
}
