| Printable Version
Spring Button
By Zeppaman
Download source files - 10 Kb
Download demo project - 13 Kb

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:
First step
First of all I declare my new class :
public class SpringButton : Control
{}
how to expose proprietiesAfter 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.
private bool Sel = false;
private Color BackColor2= Color.Gray;
public Color BackColorEnd
{
get{return BackColor2; }
set{BackColor2=value;
this.Invalidate(); }
}
int _triangle =25;
public int Triangle
{
get{return _triangle;}
set{
_triangle=value;
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)
protected override void OnMouseEnter(EventArgs e)
{
Sel = true;
this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
Sel = false;
this.Invalidate();
}
The coreThe 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)
{
Color FColor = this.BackColorEnd;
Color BColor = this.BackColor;
if (Sel == true)
{
FColor = this.BackColor;
BColor = this.BackColorEnd;
}
The delagateIn 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 (this.TriangleClick != null)
{
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)) )
{
TriangleClickEventArgs te= new TriangleClickEventArgs(false);
if((x<_triangle)&&(y<=(_triangle-x)))
te= new TriangleClickEventArgs(true);
this.TriangleClick(this,te);
}
}
}
|