• Winform圆角用户控件的软件实现


    1、文件结构
    在这里插入图片描述
    2、控件视图
    在这里插入图片描述

    3、程序代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace UserCtrlLib
    {
        public partial class LatchPower : UserControl
        {
            public LatchPower()
            {
                InitializeComponent();
                RoundRadius = 10;
            }
    
            #region Properties
            private int roundRadius;
            [Description("圆角半径(0为不要圆角)")]
            [Category("自定义外观")]
            public int RoundRadius
            {
                get { return roundRadius; }
                set 
                { 
                    if(value < 0)
                    {
                        roundRadius = 0;
                    }
                    else
                    {
                        roundRadius = value;
                    }
                    base.Refresh(); 
                }
            }
    
            #endregion
    
            #region Override Methods
            /// 
            /// 在控件尺寸变化时刷新
            /// 
            /// 
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                base.Refresh();
            }
            /// 
            /// 将控件边框修改成圆角
            /// 
            /// 
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Round(this.Region, RoundRadius);
            }
            #endregion
    
            #region UI Methods
            /// 
            /// 绘制控件的圆角
            /// 
            /// 
            /// 
            public void Round(System.Drawing.Region region, int radius)
            {
                System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();
                int x = 0;
                int y = 0;
                int thisWidth = this.Width;
                int thisHeight = this.Height;
                int angle = radius;
                // 半径非0为圆角矩形
                if (angle > 0)
                {
                    System.Drawing.Graphics g = CreateGraphics();
                    oPath.AddArc(x, y, angle, angle, 180, 90);                                 // 左上角
                    oPath.AddArc(thisWidth - angle, y, angle, angle, 270, 90);                 // 右上角
                    oPath.AddArc(thisWidth - angle, thisHeight - angle, angle, angle, 0, 90);  // 右下角
                    oPath.AddArc(x, thisHeight - angle, angle, angle, 90, 90);                 // 左下角
                    oPath.CloseAllFigures();
                    Region = new System.Drawing.Region(oPath);
                }
                //半径为0时为矩形
                else
                {
                    angle = 0;
                    oPath.AddLine(x + angle, y, thisWidth - angle, y);                         // 顶端
                    oPath.AddLine(thisWidth, y + angle, thisWidth, thisHeight - angle);        // 右边
                    oPath.AddLine(thisWidth - angle, thisHeight, x + angle, thisHeight);       // 底边
                    oPath.AddLine(x, y + angle, x, thisHeight - angle);                        // 左边
                    oPath.CloseAllFigures();
                    Region = new System.Drawing.Region(oPath);
                }
            }
            #endregion
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    4、调用并运行
    在这里插入图片描述

  • 相关阅读:
    【力扣每日一题05】数组篇--加一
    STM32之串口中断接收丢失数据
    竞赛选题 基于视觉的身份证识别系统
    java多线程
    Appium框架
    leetcode - 03 树专题 450~230~572~208~100~剑指Offer 32~剑指Offer 33
    基于Java的机场航班起降与协调管理系统的设计与实现(源码资料等)
    Frida IOS 堆栈输出与IDA 对应
    R可视乎|灯芯柱状图代码解读
    微信小程序-生成canvas图片并保存到手机相册
  • 原文地址:https://blog.csdn.net/qq_27474555/article/details/133786885