A few days ago,I wrote one. Here,I will give you.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D ;
namespace myTrackBar
{
public partial class button : Button
{
private Color color1 =Color.White ;
private Color color2 = Color.FromArgb(81, 198, 255);
public button()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);//自动重绘
}
/// <summary>
/// 画圆形按钮
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;//使边框平滑
GraphicsPath path = new GraphicsPath();
path.AddEllipse (this.ClientRectangle);//画椭圆
PathGradientBrush pbr = new PathGradientBrush(path);
pbr.CenterColor = color1;
Color[] colors = { color2 };
pbr.SurroundColors = colors;
pe.Graphics.FillPath(pbr, path);//填充椭圆
DrawText(pe);//椭圆上写字
this.Region = new Region(path);
}
/// <summary>
/// 在按钮上写字
/// </summary>
/// <param name="e"></param>
private void DrawText(PaintEventArgs e)
{
float x = this.Width / 2-4;
float y = this.Height / 2-4;
e.Graphics.DrawString(this.Text , this.Font, new SolidBrush(Color.Red),x,y);//指定位置写字
}
/// <summary>
/// 鼠标指定后改变颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_MouseEnter(object sender, EventArgs e)
{
color2 = Color.Pink;
}
/// <summary>
/// 鼠标离开颜色复原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_MouseLeave(object sender, EventArgs e)
{
color2 = Color.FromArgb(81, 198, 255);
}
}
}