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……我选择上面的那种方法……

猜你喜欢

转载自www.cnblogs.com/tony-brook/p/10308843.html