|
-
February 3rd, 2013, 04:09 PM
#10
Re: how to create irregular shape button?
 Originally Posted by murathankocan
This is an old thread, but that's not a good way of doing it. What Hannes posted was GDI, and although all you really need is the OnPaint() event method, you may want others to give it a bit more 'animation'.
Here's a button I created a while ago for an example to show someone else:
Code:
//-------------------------------
// Design Author: AceInfinity
// Name: BlueButton
// Date: December 3, 2012
//-------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CustomControls
{
class BlueButton : Button
{
MOUSESTATE MState;
Rectangle ButtonRect;
enum MOUSESTATE
{
Down, Up, Hover, Leave
}
#region Constructor
public BlueButton()
{
this.DoubleBuffered = true;
this.Size = new Size(80, 25);
//Set initial size for Graphics to use
ButtonRect = Rectangle.FromLTRB(0, 0,
this.ClientRectangle.Right - 1, this.ClientRectangle.Bottom - 1);
MState = MOUSESTATE.Leave;
}
#endregion
#region Overrides
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
//Setup
Graphics G = pevent.Graphics;
G.SmoothingMode = SmoothingMode.AntiAlias;
G.Clear(Color.FromArgb(255, 230, 230, 230));
//Choose brushes and pens
SolidBrush SB = new SolidBrush(Color.FromArgb(100, 0, 100, 200));
Pen P = new Pen(SB, 1f);
//Draw the border
G.DrawRectangle(P, Rectangle.FromLTRB(0, 0, ClientRectangle.Right - 1, ClientRectangle.Bottom - 1));
Color TopGradient;
Color BottomGradient;
TopGradient = BottomGradient = Color.Black;
switch (MState)
{
case MOUSESTATE.Down:
TopGradient = Color.FromArgb(100, 0, 180, 255);
BottomGradient = Color.FromArgb(100, 0, 180, 255);
break;
case MOUSESTATE.Hover:
TopGradient = Color.FromArgb(10, 0, 210, 255);
BottomGradient = Color.FromArgb(100, 0, 210, 255);
break;
case MOUSESTATE.Leave:
TopGradient = Color.FromArgb(10, 0, 180, 255);
BottomGradient = Color.FromArgb(100, 0, 180, 255);
break;
}
//Fill the rectangle with a linear gradient
using (LinearGradientBrush LGB = new LinearGradientBrush(ButtonRect,
TopGradient, BottomGradient, 90f))
{
G.FillRectangle(LGB, ButtonRect);
}
//Draw the text to the button
GDrawString(G, this.Text);
P.Dispose();
SB.Dispose();
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
MState = MOUSESTATE.Down;
this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
MState = MOUSESTATE.Hover;
this.Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
MState = MOUSESTATE.Hover;
this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
MState = MOUSESTATE.Leave;
this.Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
//Set new size for Graphics to use
ButtonRect = Rectangle.FromLTRB(0, 0,
this.ClientRectangle.Right - 1, this.ClientRectangle.Bottom - 1);
//Force the control to be redrawn
this.Invalidate();
}
#endregion
#region Helper Methods
protected void GDrawString(Graphics G, string text)
{
if (text.Length > 0)
{
Size size = G.MeasureString(text, Font, Width).ToSize();
Point pos = new Point(this.Width / 2 - size.Width / 2, this.Height / 2 - size.Height / 2);
using (SolidBrush SB = new SolidBrush(Color.FromArgb(200, 0, 150, 220)))
{
G.DrawString(text, this.Font, SB, pos.X, pos.Y);
}
}
}
#endregion
}
}

Although it's a regular rectangle, you can do what you want... Here was a full theme I created:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|