using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace winformTest
{
public partial class XButton : Button
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
enum model
{
hover,
enter,
press,
enable
}
public Color HoverBackColor { get; set; }
public Color EnterBackColor { get; set; }
public Color PressBackColor { get; set; }
public Color HoverForeColor { get; set; }
public Color EnterForeColor { get; set; }
public Color PressForeColor { get; set; }
public int Radius { get; set; }
model paintmodel = model.hover;
public XButton()
{
InitializeComponent();
//这些得带上,不然会有黑边
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = ;
FlatAppearance.BorderColor = Color.FromArgb(, , , );
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent;
SetDefaultColor();
}
public void SetDefaultColor()
{//给个初始值
HoverBackColor = Color.LightBlue;
EnterBackColor = Color.Blue;
PressBackColor = Color.DarkBlue;
HoverForeColor = Color.White;
EnterForeColor = Color.White;
PressForeColor = Color.White;
Radius = ;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);//这个不能去,而且得放在前面,不然会有黑框之类的莫名其妙的东西
var colorback = HoverBackColor;
var colorfore = HoverForeColor;
switch (paintmodel)
{
case model.hover:
colorback = HoverBackColor;
colorfore = HoverForeColor;
break;
case model.enter:
colorback = EnterBackColor;
colorfore = EnterForeColor;
break;
case model.press:
colorback = PressBackColor;
colorfore = PressForeColor;
break;
case model.enable:
colorback = Color.LightGray;
break;
default:
colorback = HoverBackColor;
colorfore = HoverForeColor;
break;
}
Draw(e.ClipRectangle, e.Graphics, false, colorback);
DrawText(e.ClipRectangle, e.Graphics, colorfore);
}
protected override void OnMouseEnter(EventArgs e)
{
paintmodel = model.enter;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
paintmodel = model.hover;
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
paintmodel = model.press;
base.OnMouseDown(mevent);
}
protected override void OnEnabledChanged(EventArgs e)
{
paintmodel = Enabled ? model.hover : model.enable;
Invalidate();//false 转换为true的时候不会刷新 这里强制刷新下
base.OnEnabledChanged(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 = ;
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//渐变填充
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
//画尖角
if (cusp)
{
span = ;
PointF p1 = new PointF(rectangle.Width - , rectangle.Y + );
PointF p2 = new PointF(rectangle.Width - , rectangle.Y + );
PointF p3 = new PointF(rectangle.Width, rectangle.Y + );
PointF[] ptsArray = { p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
//填充
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - , Radius));
}
void DrawText(Rectangle rectangle, Graphics g, Color color)
{
SolidBrush sbr = new SolidBrush(color);
var rect = new RectangleF();
switch (TextAlign)
{
case ContentAlignment.MiddleCenter:
rect = getTextRec(rectangle, g);
break;
default:
rect = getTextRec(rectangle, g);
break;
}
g.DrawString(Text, Font, sbr, 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;
rect.Location = new PointF(rectangle.X + (rectangle.Width - size.Width) / , rectangle.Y + (rectangle.Height - size.Height) / );
}
return rect;
}
GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, , );
gp.AddArc(width - radius, y, radius, radius, , );
gp.AddArc(width - radius, height - radius, radius, radius, , );
gp.AddArc(x, height - radius, radius, radius, , );
gp.CloseAllFigures();
return gp;
}
}
}