using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mybutton
{
public class ButtonEX : Button
{
[Browsable(true), DefaultValue(90), Description("按钮主体颜色渐变方向,X轴顺时针开始")]
[Category("Appearance")]
public int GradientAngle { get; set; }
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
//圆角和渐变的方法调用
Draw(pevent.ClipRectangle, pevent.Graphics, false, Color.Blue);
}
public int Radius { get; set; }
public ButtonEX()
{
//这些得带上,不然会有黑边
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = 0;
FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent;
}
/// <summary>
/// 画圆角和渐变的方法
/// </summary>
/// <param name="rectangle"></param>
/// <param name="g"></param>
/// <param name="cusp"></param>
/// <param name="begin_color"></param>
/// <param name="end_colorex"></param>
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;
//渐变填充
Brush b = new LinearGradientBrush(
//起始位置
new Point(this.Width, this.Height),
//结束位置
new Point(this.Width, -1),
//起始颜色
Color.FromArgb(255, 192, 0), // Opaque red
//结束颜色
Color.FromArgb(255, 243, 218)); // Opaque blue);
//画尖角
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(b, ptsArray);
}
//填充
g.FillPath(b, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, Radius));
}
/// <summary>
/// 画圆角的方法
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="radius"></param>
/// <returns></returns>
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;
}
}
}