C#封装实现圆角panel的自定义控件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/thanks_hck/article/details/81475378

public partial class ucRoundPanel : UserControl
    {
        public ucRoundPanel()
        {
            InitializeComponent();
        }
        public int _Radius;
        public int _setRoundRadius
        {
            get { return _Radius; }
            set { if (value < 0) { _Radius = 0; } else { _Radius = value; } base.Refresh(); }
        }


        // 圆角代码
        private int count = 0;
        public void Round(System.Drawing.Region region)
        {
            // ---------------------------------------------------------------------------------------------// 已经是.net 提供给我们的最容易的改窗体的属性了(以前要自 己调 API)
            System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();
            if (count == 0)
            {
                int x = 0; int y = 0;

                int thisWidth = this.Width;
                int thisHeight = this.Height;
                int angle = _Radius;
                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);
                }
                // ---------------------------------------------------------------------------------------------
                else
                {
                    // 顶端
                    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);
                }
                count += 1;
            }
        }
        public ucRoundPanel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Round(this.Region);
        } // 圆角

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            base.Refresh();
        }
    }

猜你喜欢

转载自blog.csdn.net/thanks_hck/article/details/81475378