Click here to Skip to main content
15,886,063 members
Articles / Programming Languages / C#
Article

MenuItem with Icon_Circle Button

Rate me:
Please Sign up or sign in to vote.
1.40/5 (5 votes)
3 Sep 20072 min read 25.7K   327   9   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


Written By
Software Developer
Vietnam Vietnam
I am from Vietnam and I love IT (^_^)

.....::::: A I G L E V N :::::.....

Comments and Discussions

 
Questionwrong place? Pin
NGS 5496724-Sep-07 5:45
NGS 5496724-Sep-07 5:45 
GeneralReformat the article Pin
Sudhir Mangla4-Sep-07 2:51
professionalSudhir Mangla4-Sep-07 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.