重写Button控件实现渐变背景 - (第十一讲)
视频讲解如下:
网盘下载,提取码:pt2b
https://pan.baidu.com/s/1C2ri-X89szdMtxfgc-feGg
这里给大家讲解一下如何重写Button控件实现渐变背景
重写Button控件后的效果如下:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
namespace WindowsFormsApp
{
/// <summary>
/// 椭圆形按钮
/// </summary>
public partial class MyButton : Button
{
enum MODE { leave, enter, down, up }
MODE pModel = MODE.leave;
/// <summary>
/// 倒圆角值
/// </summary>
public int Radius { get; set; }
/// <summary>
/// 默认颜色
/// </summary>
public Color DefaultShowColor { get; set; }
/// <summary>
/// 点击后的颜色
/// </summary>
public Color DefaultDownColor { get; set; }
public MyButton()
{
//这些得带上,不然会有黑边
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = 0;
FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent;
// 设置默认值
Radius = 10;
DefaultShowColor = Color.FromArgb(48, 176, 253);
DefaultDownColor = Color.FromArgb(255, 165, 53);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
List<Color> cols = new List<Color>() { Color.White, DefaultShowColor, DefaultDownColor };
Color begin = cols[0];
Color end = cols[1];
if (pModel == MODE.down) { begin = cols[2]; end = cols[0]; } // 点击后的颜色
if (pModel == MODE.up) { begin = cols[0]; end = cols[2]; } // 弹起后的颜色
if (pModel == MODE.enter) { begin = cols[0]; end = cols[2]; } // 鼠标移动时的颜色
Draw(e.ClipRectangle, e.Graphics, false, begin, end); // 设置颜色
RectangleF rect = DrawText(e.ClipRectangle, e.Graphics, this.ForeColor); // 重新绘制文字
// 重新绘制图片
if (this.Image != null)
{
int X = (int)(rect.Location.X - this.Image.Width);
int Y = (int)(rect.Location.Y);
Point p = new Point(X, Y);
Size s = new Size(this.Image.Width, this.Image.Height);
e.Graphics.DrawImage(this.Image, new Rectangle(p, s), 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel);
}
}
// 按下
protected override void OnMouseDown(MouseEventArgs mevent)
{
pModel = MODE.down;
base.OnMouseDown(mevent);
}
// 弹起
protected override void OnMouseUp(MouseEventArgs mevent)
{
pModel = MODE.up;
base.OnMouseUp(mevent);
}
// 移动进入
protected override void OnMouseEnter(EventArgs e)
{
pModel = MODE.enter;
base.OnMouseEnter(e);
}
// 移动离开
protected override void OnMouseLeave(EventArgs e)
{
pModel = MODE.leave;
base.OnMouseLeave(e);
}
// 绘图
void Draw(Rectangle rectangle, Graphics g, bool cusp, Color begin_color, Color? end_colorex = null)
{
Color end_color = end_colorex == null ? begin_color : (Color)end_colorex;
int span = 2;
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//渐变填充
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
//画尖角
if (cusp)
{
span = 10;
PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
PointF[] ptsArray = { p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
// 填充圆角18
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, Radius));
}
// 绘制文字
RectangleF DrawText(Rectangle rectangle, Graphics g, Color color)
{
Rectangle NewRec = new Rectangle(rectangle.X, rectangle.Y + 1, rectangle.Width, rectangle.Height);
SolidBrush sbr = new SolidBrush(color);
RectangleF rect = new RectangleF();
switch (TextAlign)
{
case ContentAlignment.MiddleCenter:
rect = getTextRec(NewRec, g);
break;
default:
rect = getTextRec(NewRec, g);
break;
}
g.DrawString(Text, Font, sbr, rect);
return rect;
}
RectangleF getTextRec(Rectangle rectangle, Graphics g)
{
var rect = new RectangleF();
var size = g.MeasureString(Text, Font);
if (size.Width > rectangle.Width || size.Height > rectangle.Height)
{
rect = rectangle;
}
else
{
rect.Size = size;
int X = rectangle.X;
if (this.Image != null) X += this.Image.Width;
rect.Location = new PointF(X + (rectangle.Width - size.Width) / 2, rectangle.Y + (rectangle.Height - size.Height) / 2);
}
return rect;
}
// 四边圆角
GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
gp.CloseAllFigures();
return gp;
}
}
}