Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MenuItem with Icon_Circle Button

0.00/5 (No votes)
3 Sep 2007 2  
Creating MenuItem with Icon _ Creating Circle Button
Screenshot - Demo_image.jpg

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here