C# 自制水晶按钮(很漂亮),该如何处理
C# 自制水晶按钮(很漂亮)
网上找的,直接上代码,供初学者学习借鉴:
网上找的,直接上代码,供初学者学习借鉴:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace GButton
{
public partial class GZXButton : Button
{
private enum MouseActionType
{
None,
Hover,
Click
}
private MouseActionType mouseActionType;
private ImageAttributes imgattr = new ImageAttributes();//实例化一个图像属性类
private Bitmap btnbmp;//定义一个位图
private Rectangle btnrc;//定义一个矩形
public GZXButton()
{
InitializeComponent();
mouseActionType = MouseActionType.None;
this.SetStyle(ControlStyles.AllPaintingInWmPaint|//禁止擦除背景
ControlStyles.DoubleBuffer|//双缓冲
ControlStyles.UserPaint,true);
//下面这些可以不设置,也可以自己定义
this.Font = new Font("Aial Black", 12, FontStyle.Bold);
this.BackColor = Color.DarkTurquoise;
this.Size = new Size(112, 48);
}
/// <summary>
/// 按钮形状
/// </summary>
/// <param name="rc">按钮的坐标和大小</param>
/// <param name="r">按钮圆弧的半径</param>
/// <returns>返回按钮形状</returns>
private GraphicsPath GetGraphicsPath(Rectangle rc, int r)
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
GraphicsPath gpath = new GraphicsPath();
gpath.AddArc(x, y, r, r, 180, 90);//左上角圆弧
gpath.AddArc(x + w - r, y, r, r, 270, 90);//右上角圆弧
gpath.AddArc(x + w - r, y + h - r,r, r, 0, 90);//右下角圆弧
gpath.AddArc(x, y + h - r, r, r, 90, 90);//左下角圆弧
gpath.CloseFigure();//闭合
return gpath;
}
protected override void OnPaint(PaintEventArgs pe)
{
// base.OnPaint(pe);
Graphics g = pe.Graphics;//创建画布
g.Clear(SystemColors.ButtonFace);//重置背景颜色,可以自定义
Color clr = this.BackColor;
int btnOff = 0;//按钮边距
int shadowOff = 0;//阴影边距
switch (mouseActionType)
{
case MouseActionType.None:
break;
case MouseActionType.Hover: