132 lines
4.6 KiB
C#
132 lines
4.6 KiB
C#
|
using System;
|
||
|
using System.Drawing;
|
||
|
using System.Drawing.Drawing2D;
|
||
|
using System.Windows.Forms;
|
||
|
|
||
|
namespace RoboticControlApp
|
||
|
{
|
||
|
public class ModernButton : Button
|
||
|
{
|
||
|
private bool isPressed = false;
|
||
|
private string description;
|
||
|
private Color normalColor = Color.FromArgb(245, 245, 245);
|
||
|
private Color hoverColor = Color.FromArgb(230, 230, 230);
|
||
|
private Color pressedColor = Color.FromArgb(0, 120, 215);
|
||
|
private Color borderColor = Color.FromArgb(200, 200, 200);
|
||
|
|
||
|
public ModernButton(string key, string desc)
|
||
|
{
|
||
|
description = desc;
|
||
|
Text = key;
|
||
|
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);
|
||
|
FlatStyle = FlatStyle.Flat;
|
||
|
FlatAppearance.BorderSize = 0;
|
||
|
Font = new Font("Microsoft YaHei", 10F, FontStyle.Bold);
|
||
|
BackColor = normalColor;
|
||
|
ForeColor = Color.Black;
|
||
|
Cursor = Cursors.Hand;
|
||
|
}
|
||
|
|
||
|
public void SetPressed(bool pressed)
|
||
|
{
|
||
|
isPressed = pressed;
|
||
|
Invalidate();
|
||
|
}
|
||
|
|
||
|
protected override void OnPaint(PaintEventArgs e)
|
||
|
{
|
||
|
Graphics g = e.Graphics;
|
||
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||
|
|
||
|
// 确定颜色
|
||
|
Color bgColor = isPressed ? pressedColor : (ClientRectangle.Contains(PointToClient(MousePosition)) ? hoverColor : normalColor);
|
||
|
Color textColor = isPressed ? Color.White : Color.Black;
|
||
|
|
||
|
// 绘制背景
|
||
|
using (SolidBrush brush = new SolidBrush(bgColor))
|
||
|
{
|
||
|
g.FillRoundedRectangle(brush, ClientRectangle, 8);
|
||
|
}
|
||
|
|
||
|
// 绘制边框
|
||
|
using (Pen pen = new Pen(isPressed ? pressedColor : borderColor, 1))
|
||
|
{
|
||
|
g.DrawRoundedRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1), 8);
|
||
|
}
|
||
|
|
||
|
// 绘制按键文本
|
||
|
using (SolidBrush textBrush = new SolidBrush(textColor))
|
||
|
{
|
||
|
StringFormat sf = new StringFormat();
|
||
|
sf.Alignment = StringAlignment.Center;
|
||
|
sf.LineAlignment = StringAlignment.Center;
|
||
|
|
||
|
Font keyFont = new Font("Microsoft YaHei", 12F, FontStyle.Bold);
|
||
|
g.DrawString(Text, keyFont, textBrush, new RectangleF(0, 0, Width, Height * 0.6f), sf);
|
||
|
|
||
|
if (!string.IsNullOrEmpty(description))
|
||
|
{
|
||
|
Font descFont = new Font("Microsoft YaHei", 8F);
|
||
|
g.DrawString(description, descFont, textBrush, new RectangleF(0, Height * 0.6f, Width, Height * 0.4f), sf);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 绘制按下效果
|
||
|
if (isPressed)
|
||
|
{
|
||
|
using (SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(50, 0, 0, 0)))
|
||
|
{
|
||
|
g.FillRoundedRectangle(shadowBrush, new Rectangle(2, 2, Width - 4, Height - 4), 6);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnMouseEnter(EventArgs e)
|
||
|
{
|
||
|
base.OnMouseEnter(e);
|
||
|
if (!isPressed) Invalidate();
|
||
|
}
|
||
|
|
||
|
protected override void OnMouseLeave(EventArgs e)
|
||
|
{
|
||
|
base.OnMouseLeave(e);
|
||
|
if (!isPressed) Invalidate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 扩展方法用于绘制圆角矩形
|
||
|
public static class GraphicsExtensions
|
||
|
{
|
||
|
public static void FillRoundedRectangle(this Graphics graphics, Brush brush, Rectangle rect, int radius)
|
||
|
{
|
||
|
using (GraphicsPath path = CreateRoundedRectanglePath(rect, radius))
|
||
|
{
|
||
|
graphics.FillPath(brush, path);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle rect, int radius)
|
||
|
{
|
||
|
using (GraphicsPath path = CreateRoundedRectanglePath(rect, radius))
|
||
|
{
|
||
|
graphics.DrawPath(pen, path);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int radius)
|
||
|
{
|
||
|
GraphicsPath path = new GraphicsPath();
|
||
|
int diameter = radius * 2;
|
||
|
|
||
|
path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
|
||
|
path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
|
||
|
path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
|
||
|
path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
|
||
|
path.CloseFigure();
|
||
|
|
||
|
return path;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|