winform 页面设计 自定义圆角按键

1.概述

winform 自带的按键button 美观上面的话,完全不够看
那么?
想制作一个美观 的自定义圆角按键,如何实现呢?

2.效果展示

①仅显示文字
在这里插入图片描述
②仅显示图像
在这里插入图片描述
③显示图像与文字
在这里插入图片描述
⑤带有边界的圆角按键
在这里插入图片描述

3.自定义属性参数

在这里插入图片描述

4.实质运用

方便观看进一步的效果就自制一个自定义窗体
使用上圆角按键
请添加图片描述

5.核心代码

主要是对原来的按键进行了重绘设计

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (IsSolid)
            {
                pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;//绘画图形的平滑模式,抗锯齿
                RectangleF rectBorder = new RectangleF(0, 0, this.Width - 0.5f, this.Height - 0.5f);
                Pen penBorder = new Pen(this.BackColor, 3);
                GraphicsPath pathBorder = GetFigurePath(rectBorder);
                this.Region = new Region(pathBorder);//边界
                pevent.Graphics.DrawPath(penBorder, pathBorder);
                //在内层加一个真正的区域--显示的也是这个不会右region的锯齿问题
                RectangleF rectSurface = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
                GraphicsPath pathSurface = GetFigurePath(rectSurface);
                Color sfColor = SurfaceColor;
                pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);//遮盖层,遮挡住原有的图像与文字
                sfColor = hover ? GetArgbColor(press ? 128 : 255, HoverColor) : SurfaceColor;
                pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);//鼠标事件响应层

                //绘制背景图像
                if (BackgroundImage != null)
                {
                    DrawBackgroundImage(
                        pevent.Graphics,
                        this.BackgroundImage,
                        rectSurface,
                        this.BackgroundImageLayout,
                        BackImageOffsetX,
                        BackImageOffsetY
                        );
                    //不加的话,填充的时候,出现锯齿
                    penBorder.Width = 1;
                    pevent.Graphics.DrawPath(penBorder, pathBorder);
                }

                if (TextVisible)
                {
                    //绘制文字
                    RectangleF rectText = new RectangleF(0 + TextOffsetX, 0 + TextOffsetY
                        , this.Width - 0.5f + TextOffsetX, this.Height - 0.5f + TextOffsetY);
                    StringFormat format = new StringFormat();//控制文字方向
                    format.LineAlignment = strAlignV;//文字垂直方向
                    format.Alignment = strAlignH;//文字水平方向
                    pevent.Graphics.DrawString(
                        this.Text,
                        this.Font,
                        new SolidBrush(this.ForeColor),
                        rectText,
                        format);
                }
            }
            else
            {
                pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;//绘画图形的平滑模式,抗锯齿
                RectangleF rectBorder = new RectangleF(0, 0, this.Width - 0.5f, this.Height - 0.5f);
                Pen penBorder = new Pen(this.BackColor, 3);
                GraphicsPath pathBorder = GetFigurePath(rectBorder);
                this.Region = new Region(pathBorder);//边界
                pevent.Graphics.DrawPath(penBorder, pathBorder);
                //在内层加一个真正的区域--显示的也是这个不会右region的锯齿问题
                RectangleF rectSurface = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
                GraphicsPath pathSurface = GetFigurePath(rectSurface);
                Color sfColor = hover ? GetArgbColor(64, hoverColor)
                    : (IsSelected ? GetArgbColor(64, hoverColor) : GetArgbColor(0, this.BackColor));
                pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);
            }

            if (BorderVisible)
            {
                RectangleF rectBorder = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
                GraphicsPath pathBorder = GetFigurePath(rectBorder);
                Pen penBorder = new Pen(BorderColor, BorderWidth);
                pevent.Graphics.DrawPath(penBorder, pathBorder);
            }
        }

6.代码下载

https://download.csdn.net/download/adsd1233123/85924224

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱搞事的程小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值