WinForm 窗体圆角实现

找了很多资料最后找到了, 表示感谢  为了扩散, 决定复制一份并加上自己尝试的一些方法……

圆角窗体参考地址:https://blog.csdn.net/lllljz/article/details/7561811

主要是region这个属性, 可以设置窗体的区域

        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }

        /// <summary>
        /// 窗体size发生改变时重新设置Region属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Resize(object sender, EventArgs e)
        {
            SetWindowRegion();
        }

效果图:

 

 

下面说一下我做的尝试:

问UI搞了一张图圆角、 png格式

使用代码将窗体透明

 public Form1()
        {
            InitializeComponent();

            this.BackColor = ColorTranslator.FromHtml("#F7F1F1");
            this.TransparencyKey =     ColorTranslator.FromHtml("#F7F1F1");
            this.label1.BackColor = Color.White;
            this.Opacity = 1;
        }

这里的transparencykey属性可以把所有改颜色变成透明 并且捕获不到鼠标事件, 然后将png的图片设置为背景图, 或者用e.graphes.drawimage()方法将图片放上去。

这个时候会有一个问题, 就是如果png图片的周围有一圈透明色, 则会将backcolor的颜色显示出来效果就是这样:

 

至今没找到解决办法。UI说png的图必须要有一圈透明的圆角模板, so……我选择上面的那种方法……

转载于:https://www.cnblogs.com/tony-brook/p/10308843.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值