Click here to Skip to main content
6,291,522 members and growing! (16,712 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Beginner

SpringButton

By Sir Zeppa'Man

A nice and simple c# button
C#, Windows, .NET, Visual Studio, Dev
Posted:21 Nov 2005
Updated:7 Jul 2006
Views:33,435
Bookmarked:22 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 4.93 Rating: 3.79 out of 5
3 votes, 15.0%
1

2
1 vote, 5.0%
3
6 votes, 30.0%
4
10 votes, 50.0%
5

Sample Image - springbutton.jpg

Introduction

In this sample is shown how to build a simple button with a nice graphic interface. using the.My control would be an example about using the drawings classes and the essentials key-words .I decide 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 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 others proprieties that i need are yet behind 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 te control as �selected� when the mouse is over .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 idirectly on the control ,firstthe central rectangle ,and than the two triangle in the opposite corners. I use this 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 delagate

In the end i would to explain ho to use delegate.So i declared this class that i use as EventArgs. In fact wen the use click on the control i decide if the click has been on a triangleand if yes,i do a delegate with the TriangleEventArgs that say wath 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 see my other work please visit my home page: http://zeppaman.altervista.org

Tanks!!

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

About the Author

Sir Zeppa'Man


Member

Location: Italy Italy

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralIButtonControl Pinmembergabis21:06 15 Jul '06  
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2006
Editor:
Copyright 2005 by Sir Zeppa'Man
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project