Click here to Skip to main content
Licence CPOL
First Posted 21 Nov 2005
Views 44,095
Downloads 513
Bookmarked 28 times

SpringButton

By Sir Zeppa'Man | 7 Jul 2006
A nice and simple button in C#.
3 votes, 15.0%
1

2
1 vote, 5.0%
3
6 votes, 30.0%
4
10 votes, 50.0%
5
4.52/5 - 20 votes
3 removed
μ 3.79, σa 2.47 [?]

Sample Image - springbutton.jpg

Introduction

This sample shows how to build a simple button with a nice graphic interface. This control is an example of using the drawings classes and the essential keywords. I decided to draw an unusual button like this picture below:

Sample Image - abutton.jpg

First step

First of all, I declare my new class:

public class SpringButton : Control
{}

How to expose the proprieties

After that, I expose some essential proprieties like the size in pixel of the triangles and the second color of the button. All the other proprieties that I need are available in the System.Windows.Form.Control class.

//this variable say if the
//mouse is over the contol
private bool Sel = false;
private Color BackColor2= Color.Gray;

public Color BackColorEnd
{
    get{return BackColor2; }
    set{BackColor2=value; 
        this.Invalidate();  }
}

int _triangle =25;
//I add a proprety
//that's the lenght of
//a triangle rectangle (45°)

public int Triangle
{
    get{return _triangle;}
    set{ 
        _triangle=value;
        //if lenght change I update 
        //the control
        this.Invalidate(true);
    }
}

Mouse "lighting"

Another important step is to set the control as 'selected' when the mouse is over it. So if the mouse is over the control, the back color and the border color are inverted (see the code in the OnPaint override).

//set the button as "selected" on mouse entering
//and as not selected on mouse leaving
protected override void OnMouseEnter(EventArgs e)
{
    Sel = true;
    this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
    Sel = false;
    this.Invalidate();
}

The core

The main step is this override of the OnPaint procedure. In this method, I draw directly on the control. First the central rectangle, and then the two triangles in the opposite corners. Here is the code:

protected void PaintBut(PaintEventArgs e)
{
    //I select the rights color 
    //To paint the button...
    Color FColor = this.BackColorEnd;
    Color BColor = this.BackColor;
    if (Sel == true)
    {
      FColor = this.BackColor;
      BColor = this.BackColorEnd;
    }
    //and draw(see All the code...)

The delegate

I will now explain how to use the delegate. I declared this class that I use as EventArgs. When the user clicks on the control, if the click has been on a triangle and I delegate with the TriangleEventArgs that says the triangle has been clicked.

protected override void OnMouseDown(MouseEventArgs e)
{ 
    base.OnClick(e);
    // if the user use this delegate...
    if (this.TriangleClick != null)
    {
    //check if the user click on the left triangle
    //or in the right with some geometrics  rules...
    //(is't possible to click all triangle at the same time )
    
    int x= e.X;
    int y= e.Y;
    
    if((x<_triangle)&&(y<=(_triangle-x))||
       (x>this.ClientRectangle.Width-_triangle)&&
       (y>=(this.ClientRectangle.Height-_triangle-x)) )
    {
        //try with right...
        TriangleClickEventArgs te= new TriangleClickEventArgs(false);
        //if not...
        if((x<_triangle)&&(y<=(_triangle-x)))
            te= new TriangleClickEventArgs(true);
           
            this.TriangleClick(this,te);
        }
    }
}

Credits

If you would like to see my other works, please visit my home page: http://zeppaman.altervista.org.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sir Zeppa'Man



Italy Italy

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIButtonControl Pinmembergabis21:06 15 Jul '06  
IButtonControl allows a control to act like a button on a form.
 
public interface IButtonControl
 
Classes that Implement IButtonControl
Class Description
Button Represents a Windows button control.
LinkLabel Represents a Windows label control that can display hyperlinks.
 
Remarks
An example of where this interface might be implemented is default and cancel button processing. Default buttons are notified when an unprocessed ENTER key is entered for a form, just like a dialog box would be closed. Similarly, cancel buttons are notified whenever an unprocessed ESC key is entered on a form, much like a dialog box would be dismissed.
Notes to Implementers: Implement this interface in classes that act as button controls. The members of this interface will provide basic button functionality, such as providing a DialogResult to the parent form or the ability to perform a Click event, or acting as the default button of a form.
 
Example
The following example inherits from the ButtonBase class and implements the IButtonControl interface. Implementation is added to the DialogResult property and the NotifyDefault and PerformClick methods.
 
using System;
using System.Windows.Forms;
using System.Drawing;
 
public class MyButton : ButtonBase, IButtonControl
{
private DialogResult myDialogResult;
 
public MyButton()
{
// Make the button White and a Popup style
// so it can be distinguished on the form.
this.FlatStyle = FlatStyle.Popup;
this.BackColor = Color.White;
}

// Add implementation to the IButtonControl.DialogResult property.
public DialogResult DialogResult
{
get
{
return this.myDialogResult;
}
 
set
{
if(Enum.IsDefined(typeof(DialogResult), value))
{
this.myDialogResult = value;
}
}
}
 
// Add implementation to the IButtonControl.NotifyDefault method.
public void NotifyDefault(bool value)
{
if(this.IsDefault != value)
{
this.IsDefault = value;
}
}
 
// Add implementation to the IButtonControl.PerformClick method.
public void PerformClick()
{
if(this.CanSelect)
{
this.OnClick(EventArgs.Empty);
}
}
}

GeneralAh! It's cool! PinmemberMahesh Sapre5:07 10 May '06  
GeneralRe: Ah! It's cool! PinmemberSir Zeppa'Man6:04 10 May '06  
GeneralNice PinmemberNinjaCross0:51 10 May '06  
GeneralRe: Nice PinmemberSir Zeppa'Man1:31 10 May '06  
Generaltyu Pinmemberpccai2:12 24 Nov '05  
QuestionSpring Button Pinmembermr chiken6:57 23 Nov '05  
GeneralRe: Spring Button [modified] PinmemberThe_Mega_ZZTer14:50 9 Jul '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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 7 Jul 2006
Article Copyright 2005 by Sir Zeppa'Man
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid