C# WInFomr 窗体圆角

#region 设置窗体圆角
        /// <summary>
        /// 设置窗体圆角
        /// </summary>
        /// <param name="f"></param>
        public static void FormRounded(this Form f)
        {
            f.Resize += (object sender, EventArgs e) =>
            {
                if (f.WindowState == FormWindowState.Normal)
                {
                    f.SetWindowRegion();
                }
                else
                {
                    f.Region = null;
                }
            };
        }
        private static void SetWindowRegion(this Form f)
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, f.Width, f.Height);
            FormPath = GetRoundedRectPath(rect, 10);
            f.Region = new Region(FormPath);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect">窗体大小</param>
        /// <param name="radius">圆角大小</param>
        /// <returns></returns>
        private static 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;
        }
        #endregion 设置窗体圆角

 

posted @ 2022-02-18 16:02  LuoCore  阅读(167)  评论(0编辑  收藏  举报