-
Custom MenuItem
Created simple class derived from MenuItem
public class SauronMenuItem : System.Windows.Forms.MenuItem, IMenuItem
{
....
public SauronMenuItem(string Name, string Text, Image image, EventHandler onClick, Shortcut shortcut)
: base(Text, onClick, shortcut)
{
OwnerDraw = false;
_Name = Name;
_Image = image;
}
protected override void OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
//temporrally fixedvalues
e.ItemHeight = 32;
e.ItemWidth = 75;
}
protected override void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
base.OnDrawItem(e);
DrawText(e);
}
private void DrawText(System.Windows.Forms.DrawItemEventArgs e)
{
RectangleF TextRectF = new RectangleF(e.Bounds.X + 32, e.Bounds.Top, e.Bounds.Width - 48, e.Bounds.Height);
...
e.Graphics.DrawString(Text, MenuFont, TextBrush, TextRectF, sf);
}
}
When I try show ContextMenu then I get error "Out Of Memory". It happen after then method MenuItem.OnMeasureItem of last MenuItem called in ContextMenu.
Same error happen if I add handlers to MeasureItem and DrawItem without override apropriate methods.
Where is a bug? I hat i forgot did?
-
Re: Custom MenuItem
Quote:
OwnerDraw = false;
:confused:
Why U override drawing functions and at the same time set "OwnerDraw = false"?
Best regards,
Krzemo.
-
Re: Custom MenuItem
Sorry. I had mean OwnerDraw=true. I solved problem, I rebooted computer. The Visual Studio reboot did not have any effect.
Also I have problems with drawing all Context menus in all applications. All Context menus did not want draw items until cursor positioned on it.
Very strange behavior of .Net or Windows... I think that it happen when e.ItemHeight and e.ItemWidth was equals zerro.