Click here to Skip to main content
Click here to Skip to main content

Button derived color selection control in C#

, 22 Dec 2003
Rate this:
Please Sign up or sign in to vote.
Button derived color selection control similar to the one used in MS Office applications.

Sample Image - ColorButton.gif

Introduction

ColorButton is a color selection control derived from System.Windows.Forms.Button similar to the one used in MS Office applications. It uses the same colors like MS Office, including support for "Automatic" and "More Colors".

How To Use

Add a normal Button to your Windows Form and set the Text property to an empty string. Then manually change all occurrences of System.Windows.Forms.Button to ColorButton.ColorButton for this button in the source file.

Properties

ColorButton adds the following new properties:

Color (get/set)

Gets or sets the selected color. System.Drawing.Color.Transparent is used for the "Automatic" color.

Automatic (get/set)

Gets or sets the string for the "Automatic" button. Setting this to an empty string disables the "Automatic" color feature.

MoreColors (get/set)

Gets or sets the string for the "More Colors" button. Setting this to an empty string disables the "More Colors" color feature.

Events

ColorButton adds the following new event:

Changed

This event occurs whenever the user changes the selected color. Use the Color property to determine what the selected color is.

History

  • 1.1
    • Added keyboard support to color panel (thanks to Ignazio Di Napoli)
    • Button handles disabled state correctly
    • Button is drawn pushed, if color panel is visible
  • 1.0
    • Initial release

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

Share

About the Author

Thomas Ascher
Web Developer
Austria Austria
No Biography provided

Comments and Discussions

 
QuestionHow to use this in VB PinmemberJ3ssy Pmntra29-Sep-14 7:13 
GeneralFixed Control for Clipregion PinmemberCodeMonkey3315-Sep-06 7:23 
GeneralReworked as a .NET 2.0 ToolStripButton PinmemberGreg Ennis7-Mar-06 4:37 
I needed a color picker that worked on a .NET toolbar ToolStrip. This was close but needed some changes. Here is my revised class:

public class ToolStripColorButton : ToolStripButton
{
private System.ComponentModel.Container components = null ;

private Color buttonColor = Color.Transparent ;
private string autoButton = "Automatic" ;
private string moreButton = "More Colors..." ;

public event EventHandler Changed ;

public Color Color
{
get { return buttonColor ; }
set
{
buttonColor = value ;
Invalidate();
}
}

public string Automatic
{
get { return autoButton ; }
set { autoButton = value ; }
}

public string MoreColors
{
get { return moreButton ; }
set { moreButton = value ; }
}

public bool DroppedDown
{
get { return this.Checked; }
}

protected virtual void OnChanged( EventArgs e )
{
if( Changed != null )
Changed( this, e ) ;
}

public ToolStripColorButton()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose() ;
}

base.Dispose( disposing ) ;
}

#region Vom Komponenten-Designer generierter Code
private void InitializeComponent()
{
components = new System.ComponentModel.Container() ;
}
#endregion

protected override void OnPaint( PaintEventArgs e )
{
base.OnPaint( e ) ;

int offset = 0;

Rectangle rc = new Rectangle(e.ClipRectangle.Left + 5 + offset,
e.ClipRectangle.Top + 5 + offset,
e.ClipRectangle.Width - 20,
e.ClipRectangle.Height - 11);
Pen darkPen = new Pen(SystemColors.ControlDark);

if (Enabled)
{
e.Graphics.FillRectangle(new SolidBrush(buttonColor), rc);
e.Graphics.DrawRectangle(darkPen, rc);
}

Pen textPen = new Pen(Enabled ? SystemColors.ControlText : SystemColors.GrayText);
Point pt = new Point(rc.Right, (e.ClipRectangle.Height + offset) / 2);

e.Graphics.DrawLine(textPen, pt.X + 5, pt.Y - 1, pt.X + 9, pt.Y - 1);
e.Graphics.DrawLine(textPen, pt.X + 6, pt.Y, pt.X + 8, pt.Y);
e.Graphics.DrawLine(textPen, pt.X + 7, pt.Y, pt.X + 7, pt.Y + 1);
}

protected override void OnClick( EventArgs e )
{
this.Checked = true;
Invalidate();

Point pt = Parent.PointToScreen( new Point( this.Bounds.X, this.Bounds.Y + this.Bounds.Height ) ) ;

ColorPanel panel = new ColorPanel( pt, this ) ;
panel.Show() ;
}

protected class ColorPanel : System.Windows.Forms.Form
{
private ToolStripColorButton colorButton;
private int colorIndex = -1 ;
// ADDED: Ignazio Di Napoli - neclepsio@hotmail.com
private int keyboardIndex = -50 ;
// END ADDED

private Color[] colorList = new Color[40]
{
Color.FromArgb( 0x00, 0x00, 0x00 ), Color.FromArgb( 0x99, 0x33, 0x00 ),
Color.FromArgb( 0x33, 0x33, 0x00 ), Color.FromArgb( 0x00, 0x33, 0x00 ),
Color.FromArgb( 0x00, 0x33, 0x66 ), Color.FromArgb( 0x00, 0x00, 0x80 ),
Color.FromArgb( 0x33, 0x33, 0x99 ), Color.FromArgb( 0x33, 0x33, 0x33 ),

Color.FromArgb( 0x80, 0x00, 0x00 ), Color.FromArgb( 0xFF, 0x66, 0x00 ),
Color.FromArgb( 0x80, 0x80, 0x00 ), Color.FromArgb( 0x00, 0x80, 0x00 ),
Color.FromArgb( 0x00, 0x80, 0x80 ), Color.FromArgb( 0x00, 0x00, 0xFF ),
Color.FromArgb( 0x66, 0x66, 0x99 ), Color.FromArgb( 0x80, 0x80, 0x80 ),

Color.FromArgb( 0xFF, 0x00, 0x00 ), Color.FromArgb( 0xFF, 0x99, 0x00 ),
Color.FromArgb( 0x99, 0xCC, 0x00 ), Color.FromArgb( 0x33, 0x99, 0x66 ),
Color.FromArgb( 0x33, 0xCC, 0xCC ), Color.FromArgb( 0x33, 0x66, 0xFF ),
Color.FromArgb( 0x80, 0x00, 0x80 ), Color.FromArgb( 0x99, 0x99, 0x99 ),

Color.FromArgb( 0xFF, 0x00, 0xFF ), Color.FromArgb( 0xFF, 0xCC, 0x00 ),
Color.FromArgb( 0xFF, 0xFF, 0x00 ), Color.FromArgb( 0x00, 0xFF, 0x00 ),
Color.FromArgb( 0x00, 0xFF, 0xFF ), Color.FromArgb( 0x00, 0xCC, 0xFF ),
Color.FromArgb( 0x99, 0x33, 0x66 ), Color.FromArgb( 0xC0, 0xC0, 0xC0 ),

Color.FromArgb( 0xFF, 0x99, 0xCC ), Color.FromArgb( 0xFF, 0xCC, 0x99 ),
Color.FromArgb( 0xFF, 0xFF, 0x99 ), Color.FromArgb( 0xCC, 0xFF, 0xCC ),
Color.FromArgb( 0xCC, 0xFF, 0xFF ), Color.FromArgb( 0x99, 0xCC, 0xFF ),
Color.FromArgb( 0xCC, 0x99, 0xFF ), Color.FromArgb( 0xFF, 0xFF, 0xFF )
} ;

public ColorPanel(Point pt, ToolStripColorButton button)
{
colorButton = button ;

FormBorderStyle = FormBorderStyle.FixedDialog ;
MinimizeBox = false ;
MaximizeBox = false ;
ControlBox = false ;
ShowInTaskbar = false ;
TopMost = true ;

SetStyle( ControlStyles.DoubleBuffer, true ) ;
SetStyle( ControlStyles.UserPaint, true ) ;
SetStyle( ControlStyles.AllPaintingInWmPaint, true ) ;

Width = 156 ;
Height = 100 ;

if( colorButton.autoButton != "" )
Height += 23 ;
if( colorButton.moreButton != "" )
Height += 23 ;

CenterToScreen() ;
Location = pt ;

Capture = true ;
}

protected override void OnClosed( EventArgs e )
{
base.OnClosed( e ) ;

colorButton.Checked = false;
colorButton.Invalidate();
}

protected override void OnPaint( PaintEventArgs e )
{
base.OnPaint( e ) ;

Pen darkPen = new Pen( SystemColors.ControlDark ) ;
Pen lightPen = new Pen( SystemColors.ControlLightLight ) ;
SolidBrush lightBrush = new SolidBrush( SystemColors.ControlLightLight ) ;
bool selected = false ;
int x = 6, y = 5 ;

if( colorButton.autoButton != "" )
{
selected = colorButton.Color == Color.Transparent ;
DrawButton( e, x, y, colorButton.autoButton, 100, selected ) ;
y += 23 ;
}

for( int i = 0 ; i < 40 ; i++ )
{
if( colorButton.Color.ToArgb() == colorList[i].ToArgb() )
selected = true ;

if( colorIndex == i )
{
e.Graphics.DrawRectangle( lightPen, x - 3, y - 3, 17, 17 ) ;
e.Graphics.DrawLine( darkPen, x - 2, y + 14, x + 14, y + 14 ) ;
e.Graphics.DrawLine( darkPen, x + 14, y - 2, x + 14, y + 14 ) ;
}
else if( colorButton.Color.ToArgb() == colorList[i].ToArgb() )
{
// ADDED: Ignazio Di Napoli - neclepsio@hotmail.com
if( keyboardIndex == -50 )
keyboardIndex = i ;
// END ADDED

e.Graphics.FillRectangle( lightBrush, x - 3, y - 3, 18, 18 ) ;
e.Graphics.DrawLine( darkPen, x - 3, y - 3, x + 13, y - 3 ) ;
e.Graphics.DrawLine( darkPen, x - 3, y - 3, x - 3, y + 13 ) ;
}

e.Graphics.FillRectangle( new SolidBrush( colorList[i] ), x, y, 11, 11 ) ;
e.Graphics.DrawRectangle( darkPen, x, y, 11, 11 ) ;

if( ( i + 1 ) % 8 == 0 )
{
x = 6 ;
y += 18 ;
}
else
x += 18 ;
}

if( colorButton.moreButton != "" )
DrawButton( e, x, y, colorButton.moreButton, 101, ! selected ) ;
}

// ADDED: Ignazio Di Napoli - neclepsio@hotmail.com
protected override void OnKeyDown( KeyEventArgs e )
{
if( e.KeyCode == Keys.Escape )
Close() ;
else if( e.KeyCode == Keys.Left )
MoveIndex( -1 ) ;
else if( e.KeyCode == Keys.Up )
MoveIndex( -8 ) ;
else if( e.KeyCode == Keys.Down )
MoveIndex( +8 ) ;
else if( e.KeyCode == Keys.Right )
MoveIndex( +1 ) ;
else if( e.KeyCode == Keys.Enter ||
e.KeyCode == Keys.Space )
OnClick( EventArgs.Empty ) ;
else
base.OnKeyDown( e ) ;
}

private void MoveIndex( int delta )
{
int lbound = ( colorButton.autoButton != "" ? -8 : 0 ) ;
int ubound = 39 + ( colorButton.moreButton != "" ? 8 : 0 ) ;
int d = ubound - lbound + 1 ;

if( delta == -1 && keyboardIndex < 0 )
keyboardIndex = ubound ;
else if( delta == 1 && keyboardIndex > 39 )
keyboardIndex = lbound ;
else if( delta == 1 && keyboardIndex < 0 )
keyboardIndex = 0 ;
else if( delta == -1 && keyboardIndex > 39 )
keyboardIndex = 39 ;
else
keyboardIndex += delta ;

if( keyboardIndex < lbound )
keyboardIndex += d ;
if( keyboardIndex > ubound )
keyboardIndex -= d ;

if( keyboardIndex < 0 )
colorIndex = 100 ;
else if( keyboardIndex > 39 )
colorIndex = 101 ;
else
colorIndex = keyboardIndex ;

Refresh() ;
}
// END ADDED

protected override void OnMouseDown( MouseEventArgs e )
{
if( RectangleToScreen( ClientRectangle ).Contains( Cursor.Position ) )
base.OnMouseDown( e ) ;
else
Close() ;
}

protected override void OnMouseMove( MouseEventArgs e )
{
base.OnMouseMove( e ) ;

if( RectangleToScreen( ClientRectangle ).Contains( Cursor.Position ) )
{
Point pt = PointToClient( Cursor.Position ) ;
int x = 6, y = 5 ;

if( colorButton.autoButton != "" )
{
if( SetColorIndex( new Rectangle( x - 3, y - 3, 143, 22 ), pt, 100 ) )
return ;

y += 23 ;
}

for( int i = 0 ; i < 40 ; i++ )
{
if( SetColorIndex( new Rectangle( x - 3, y - 3, 17, 17 ), pt, i ) )
return ;

if( ( i + 1 ) % 8 == 0 )
{
x = 6 ;
y += 18 ;
}
else
x += 18 ;
}

if( colorButton.moreButton != "" )
{
if( SetColorIndex( new Rectangle( x - 3, y - 3, 143, 22 ), pt, 101 ) )
return ;
}
}

if( colorIndex != -1 )
{
colorIndex = -1 ;
Invalidate() ;
}
}

protected override void OnClick( EventArgs e )
{
if( colorIndex < 0 )
return ;

if( colorIndex < 40 )
colorButton.Color = colorList[colorIndex] ;
else if( colorIndex == 100 )
colorButton.Color = Color.Transparent ;
else
{
ColorDialog dlg = new ColorDialog() ;
dlg.Color = colorButton.Color ;
dlg.FullOpen = true ;

if( dlg.ShowDialog( this ) != DialogResult.OK )
{
Close() ;
return ;
}

colorButton.Color = dlg.Color ;
}

Close() ;
colorButton.OnChanged( EventArgs.Empty ) ;
}

protected void DrawButton( PaintEventArgs e, int x, int y, string text,
int index, bool selected )
{
Pen darkPen = new Pen( SystemColors.ControlDark ) ;
Pen lightPen = new Pen( SystemColors.ControlLightLight ) ;
SolidBrush lightBrush = new SolidBrush( SystemColors.ControlLightLight ) ;

if( colorIndex == index )
{
e.Graphics.DrawRectangle( lightPen, x - 3, y - 3, 143, 22 ) ;
e.Graphics.DrawLine( darkPen, x - 2, y + 19, x + 140, y + 19 ) ;
e.Graphics.DrawLine( darkPen, x + 140, y - 2, x + 140, y + 19 ) ;
}
else if( selected )
{
e.Graphics.FillRectangle( lightBrush, x - 3, y - 3, 144, 23 ) ;
e.Graphics.DrawLine( darkPen, x - 3, y - 3, x + 139, y - 3 ) ;
e.Graphics.DrawLine( darkPen, x - 3, y - 3, x - 3, y + 18 ) ;
}

Rectangle rc = new Rectangle( x, y, 137, 16 ) ;
SolidBrush textBrush = new SolidBrush( SystemColors.ControlText ) ;

StringFormat textFormat = new StringFormat() ;
textFormat.Alignment = StringAlignment.Center ;
textFormat.LineAlignment = StringAlignment.Center ;

e.Graphics.DrawRectangle( darkPen, rc ) ;
e.Graphics.DrawString( text, colorButton.Font, textBrush, rc, textFormat ) ;
}

protected bool SetColorIndex( Rectangle rc, Point pt, int index )
{
if( rc.Contains( pt ) )
{
if( colorIndex != index )
{
colorIndex = index ;
Invalidate() ;
}

return true ;
}

return false ;
}
}
}

GeneralRe: Reworked as a .NET 2.0 ToolStripButton Pinmembershlomifr7-Aug-09 4:05 
GeneralVisual Effects Pinmembersleepycoder@hotmail.com29-Aug-04 9:46 
GeneralNice Job PinmemberTom Spink22-Dec-03 8:08 
GeneralRe: Nice Job PinmemberThomas Ascher23-Dec-03 0:22 
GeneralRe: Nice Job PinmemberTom Spink23-Dec-03 1:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 23 Dec 2003
Article Copyright 2003 by Thomas Ascher
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid