C#封装带弧度的角的Panel的用户控件(可应用于圆角的按钮使用)

效果图:

public partial class CtrlRoundPanel : UserControl
    {
        public CtrlRoundPanel()
        {
            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 CtrlRoundPanel(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();
        }
    }

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值