نمونه زیر رو ببینید.
برای border یک دایره معمولی باید رسم کنید البته در هر رویداد میتونید رنگ و اندازه رو تغییر بدید.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
public class RoundedButton : Button
{
enum ButtonState
{
Normal,
MouseEnter,
GotFocus,
Pushed
}
private ButtonState _state;
private ushort _borderWidth;
private Color _backColorMouseEnterState;
private Color _borderColorNormalState;
private Color _borderColorGotFocusState;
private Color _backColorDisableState;
private Color _backColorPushedState;
public sealed override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
public Color BackColorMouseEnterState
{
get { return _backColorMouseEnterState; }
set { _backColorMouseEnterState = value; }
}
public Color BorderColorNormalState
{
get { return _borderColorNormalState; }
set
{
if (_borderColorNormalState != value)
{
_borderColorNormalState = value;
Invalidate();
}
}
}
public Color BorderColorGotFocusState
{
get { return _borderColorGotFocusState; }
set { _borderColorGotFocusState = value; }
}
public Color BackColorDisableState
{
get { return _backColorDisableState; }
set { _backColorDisableState = value; }
}
public Color BackColorPushedState
{
get { return _backColorPushedState; }
set { _backColorPushedState = value; }
}
public ushort BorderWidth
{
get { return _borderWidth; }
set
{
if (_borderWidth != value)
{
_borderWidth = value;
Invalidate();
}
}
}
public RoundedButton()
{
BackColor = Color.FromArgb(125, 192, 255);
BackColorMouseEnterState = Color.FromArgb(180, 219, 255);
BackColorPushedState = Color.FromArgb(64, 162, 255);
BackColorDisableState = Color.FromArgb(214, 214, 214);
BorderWidth = 5;
BorderColorNormalState = Color.FromArgb(47, 154, 255);
BorderColorGotFocusState = Color.FromArgb(0, 121, 235);
}
protected override void OnGotFocus(EventArgs e)
{
_state = ButtonState.GotFocus;
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
_state = ButtonState.Normal;
base.OnLostFocus(e);
}
protected override void OnMouseEnter(EventArgs e)
{
_state = ButtonState.MouseEnter;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
_state = ButtonState.Normal;
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_state = ButtonState.Pushed;
}
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_state = ButtonState.MouseEnter;
}
base.OnMouseUp(mevent);
}
protected override void OnKeyDown(KeyEventArgs kevent)
{
if (kevent.KeyCode == Keys.Space)
{
_state = ButtonState.Pushed;
}
base.OnKeyDown(kevent);
}
protected override void OnKeyUp(KeyEventArgs kevent)
{
if (kevent.KeyCode == Keys.Space)
{
_state = ButtonState.GotFocus;
}
base.OnKeyUp(kevent);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.High;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(0, 0, Width, Height);
DrawBackground(e.Graphics, path);
DrawBorder(e.Graphics, path);
DrawText(e.Graphics);
}
}
private void DrawText(Graphics g)
{
using (StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
g.DrawString(Text, Font, new SolidBrush(ForeColor), this.ClientRectangle , stringFormat);
}
}
private void DrawBorder(Graphics g, GraphicsPath path)
{
using (Pen pen = _state == ButtonState.GotFocus ? new Pen(BorderColorGotFocusState, BorderWidth) : new Pen(BorderColorNormalState, BorderWidth))
{
g.DrawPath(pen, path);
}
}
private void DrawBackground(Graphics g, GraphicsPath path)
{
Region = new Region(path);
Color backColorToDraw =
_state == ButtonState.MouseEnter
? BackColorMouseEnterState
: (Enabled
? BackColor
: BackColorDisableState);
g.FillRegion(new SolidBrush(backColorToDraw), Region);
}
}