目的:学习了解GDI绘制、在复刻Winform按钮,实现圆角边框/矩形边框/无边框,以及悬停、点击等事件
引申:可根据按钮的重绘,拓展到输入框的圆角功能
![](https://i-blog.csdnimg.cn/blog_migrate/8d1b91a31fab644d581901652ffa024a.png)
一、环境
VS2022
新建操作:1、新建->项目->Window窗口应用(.NET Framework)->创建(取个自己喜欢的命名)
2、对于新建的项目 右键 添加->类->添加(取个自己喜欢的命名)
![](https://i-blog.csdnimg.cn/blog_migrate/3377a574e99f1e23e2b453f77e981830.png)
二、代码
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace ControlTest
{
internal class buttonRY : System.Windows.Forms.Control
{
private GraphicsPath g;
private Rectangle cr; // 客户端矩形区域
private ControlState ControlState = ControlState.Normal; //控件状态
private Color LocalColorRY { get; set; } //控件当前的背景
public buttonRY()
{
//画面闪烁问题与双缓冲技术
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
}
#region 自定义面板属性
private Color _BackColor = Color.FromArgb(253, 253, 253); //#fdfdfd
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("背景颜色"), Category("自定义"), Browsable(true)]
public Color BackColorRY { get { return _BackColor; } set { _BackColor = value; this.Refresh(); } }
private Color _HoverColor = Color.FromArgb(224, 238, 249); // #e0eef9
[Description("悬停时的颜色"), Category("自定义"), Br