Introduction
Normal when creating Form with C# using Toolbox of VS.NET, you can't create Menu Item with Icon whether that is a really require. I researched and developed this application for this problem
Using MenuItem with Icon _ CircleButton, you can creating MenuItem with Icon which attrachtive, therefor, i created Custom Button using Drawing
Background
VS.NET 2003
Using the code
<code>
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
Brush br; // using for paint rectangle
Rectangle rcBk = e.Bounds;
// add Width of Icon Image
rcBk.X -= m_Icon.Width;
rcBk.Width += m_Icon.Width;
// When menu item is Enabled
if((e.State & DrawItemState.Selected) != 0 && this.Enabled == true)
{ // When selected (highlighted)
float f = 0;
br = new LinearGradientBrush(rcBk, m_Gradient_Color1, m_Gradient_Color2, f);
//Paint main rectangle
e.Graphics.FillRectangle(br, rcBk);
//Paint Border for main rectangle
Pen pen = new Pen(new SolidBrush(SystemColors.Highlight));
e.Graphics.DrawRectangle(pen, e.Bounds.Left, e.Bounds.Top, rcBk.Width - 17, rcBk.Height-1);
}
else // When not selected
{
//Paint main rectangle
br = new SolidBrush( SystemColors.Menu ) ;
e.Graphics.FillRectangle(br, rcBk);
//Paint background rectangle for Icon
br = new SolidBrush( SystemColors.Control ) ;
e.Graphics.FillRectangle(br, e.Bounds.Left, e.Bounds.Top, m_Icon.Width + 6, m_Icon.Height + 6);
}
//Paint Icon front of main rectangle
if(m_Icon != null)
{
e.Graphics.DrawIcon(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 3);
}
StringFormat sf = new StringFormat();
sf.HotkeyPrefix = HotkeyPrefix.Show;
// Set text color
if ( this.Enabled == false ) // When menu item is not enabled
{
br = new SolidBrush( SystemColors.GrayText ) ;
}
else
{
br = new SolidBrush( SystemColors.WindowText ) ;
// br = new SolidBrush(e.ForeColor);
}
//Paint text
e.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 25, e.Bounds.Top + 2, sf);
}
</code>
Points of Interest
No Problem
History
It is first version