C# WInFomr 窗体圆角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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 @   LuoCore  阅读(172)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示