Winform 窗口圆角实现
项目需要,得把Winform的窗口做成圆角,同时把标题栏等一堆东西去掉,做成如下的样子:
首先,在新建Winform之后,在属性栏中, 把MaximizeBox和MinimizeBox 置为False,同时将ShowIcon置为False,FormBorderStyle置为None。之后,窗口就变成一块白板了。
然后,PS出一张如上的背景图来,把图做成圆角。将改图设为Winform的背景,BackgroundImageLayout设为Stretch。
网上有人说此后只需要将Winform的BackColor置为Color.Transparent就Ok了, 但是VS2010会报错,控件背景不允许使用透明色。
所以只能学习网上有人提到的办法,实现Winform的Paint时间,将窗口重绘成圆角的。
窗口圆角
1 private void Main_Paint(object sender, PaintEventArgs e)
2 {
3 List<Point> list = new List<Point>();
4 int width = this.Width;
5 int height = this.Height;
6
7 #region 四个圆角
8
9 //左上
10 list.Add(new Point(0, 47));
11 list.Add(new Point(1, 42));
12 list.Add(new Point(2, 38));
13 list.Add(new Point(3, 36));
14 list.Add(new Point(4, 33));
15 list.Add(new Point(5, 32));
16 list.Add(new Point(6, 29));
17 list.Add(new Point(7, 27));
18 list.Add(new Point(8, 26));
19 list.Add(new Point(9, 24));
20 list.Add(new Point(10, 22));
21 list.Add(new Point(11, 21));
22 list.Add(new Point(12, 20));
23 list.Add(new Point(13, 19));
24 list.Add(new Point(14, 17));
25 list.Add(new Point(15, 16));
26 list.Add(new Point(16, 15));
27 list.Add(new Point(17, 14));
28 list.Add(new Point(19, 13));
29 list.Add(new Point(20, 12));
30 list.Add(new Point(21, 11));
31 list.Add(new Point(22, 10));
32 list.Add(new Point(24, 9));
33 list.Add(new Point(26, 8));
34 list.Add(new Point(27, 7));
35 list.Add(new Point(29, 6));
36 list.Add(new Point(32, 5));
37 list.Add(new Point(33, 4));
38 list.Add(new Point(36, 3));
39 list.Add(new Point(38, 2));
40 list.Add(new Point(42, 1));
41 list.Add(new Point(47, 0));
42
43 //右上
44 list.Add(new Point(width - 47, 0));
45 list.Add(new Point(width - 42, 1));
46 list.Add(new Point(width - 38, 2));
47 list.Add(new Point(width - 36, 3));
48 list.Add(new Point(width - 33, 4));
49 list.Add(new Point(width - 32, 5));
50 list.Add(new Point(width - 29, 6));
51 list.Add(new Point(width - 27, 7));
52 list.Add(new Point(width - 26, 8));
53 list.Add(new Point(width - 24, 9));
54 list.Add(new Point(width - 22, 10));
55 list.Add(new Point(width - 21, 11));
56 list.Add(new Point(width - 20, 12));
57 list.Add(new Point(width - 19, 13));
58 list.Add(new Point(width - 17, 14));
59 list.Add(new Point(width - 16, 15));
60 list.Add(new Point(width - 15, 16));
61 list.Add(new Point(width - 14, 17));
62 list.Add(new Point(width - 13, 19));
63 list.Add(new Point(width - 12, 20));
64 list.Add(new Point(width - 11, 21));
65 list.Add(new Point(width - 10, 22));
66 list.Add(new Point(width - 9, 24));
67 list.Add(new Point(width - 8, 26));
68 list.Add(new Point(width - 7, 27));
69 list.Add(new Point(width - 6, 29));
70 list.Add(new Point(width - 5, 32));
71 list.Add(new Point(width - 4, 33));
72 list.Add(new Point(width - 3, 36));
73 list.Add(new Point(width - 2, 38));
74 list.Add(new Point(width - 1, 42));
75 list.Add(new Point(width - 0, 47));
76
77 //右下
78 list.Add(new Point(width - 0, height - 47));
79 list.Add(new Point(width - 1, height - 42));
80 list.Add(new Point(width - 2, height - 38));
81 list.Add(new Point(width - 3, height - 36));
82 list.Add(new Point(width - 4, height - 33));
83 list.Add(new Point(width - 5, height - 32));
84 list.Add(new Point(width - 6, height - 29));
85 list.Add(new Point(width - 7, height - 27));
86 list.Add(new Point(width - 8, height - 26));
87 list.Add(new Point(width - 9, height - 24));
88 list.Add(new Point(width - 10, height - 22));
89 list.Add(new Point(width - 11, height - 21));
90 list.Add(new Point(width - 12, height - 20));
91 list.Add(new Point(width - 13, height - 19));
92 list.Add(new Point(width - 14, height - 17));
93 list.Add(new Point(width - 15, height - 16));
94 list.Add(new Point(width - 16, height - 15));
95 list.Add(new Point(width - 17, height - 14));
96 list.Add(new Point(width - 19, height - 13));
97 list.Add(new Point(width - 20, height - 12));
98 list.Add(new Point(width - 21, height - 11));
99 list.Add(new Point(width - 22, height - 10));
100 list.Add(new Point(width - 24, height - 9));
101 list.Add(new Point(width - 26, height - 8));
102 list.Add(new Point(width - 27, height - 7));
103 list.Add(new Point(width - 29, height - 6));
104 list.Add(new Point(width - 32, height - 5));
105 list.Add(new Point(width - 33, height - 4));
106 list.Add(new Point(width - 36, height - 3));
107 list.Add(new Point(width - 38, height - 2));
108 list.Add(new Point(width - 42, height - 1));
109 list.Add(new Point(width - 47, height - 0));
110
111 //左下
112 list.Add(new Point(47, height - 0));
113 list.Add(new Point(42, height - 1));
114 list.Add(new Point(38, height - 2));
115 list.Add(new Point(36, height - 3));
116 list.Add(new Point(33, height - 4));
117 list.Add(new Point(32, height - 5));
118 list.Add(new Point(29, height - 6));
119 list.Add(new Point(27, height - 7));
120 list.Add(new Point(26, height - 8));
121 list.Add(new Point(24, height - 9));
122 list.Add(new Point(22, height - 10));
123 list.Add(new Point(21, height - 11));
124 list.Add(new Point(20, height - 12));
125 list.Add(new Point(19, height - 13));
126 list.Add(new Point(17, height - 14));
127 list.Add(new Point(16, height - 15));
128 list.Add(new Point(15, height - 16));
129 list.Add(new Point(14, height - 17));
130 list.Add(new Point(13, height - 19));
131 list.Add(new Point(12, height - 20));
132 list.Add(new Point(11, height - 21));
133 list.Add(new Point(10, height - 22));
134 list.Add(new Point(9, height - 24));
135 list.Add(new Point(8, height - 26));
136 list.Add(new Point(7, height - 27));
137 list.Add(new Point(6, height - 29));
138 list.Add(new Point(5, height - 32));
139 list.Add(new Point(4, height - 33));
140 list.Add(new Point(3, height - 36));
141 list.Add(new Point(2, height - 38));
142 list.Add(new Point(1, height - 42));
143 list.Add(new Point(0, height - 47));
144
145 #endregion
146
147 Point[] points = list.ToArray();
148
149 GraphicsPath shape = new GraphicsPath();
150 shape.AddPolygon(points);
151
152 this.Region = new System.Drawing.Region(shape);
153
154 }
2 {
3 List<Point> list = new List<Point>();
4 int width = this.Width;
5 int height = this.Height;
6
7 #region 四个圆角
8
9 //左上
10 list.Add(new Point(0, 47));
11 list.Add(new Point(1, 42));
12 list.Add(new Point(2, 38));
13 list.Add(new Point(3, 36));
14 list.Add(new Point(4, 33));
15 list.Add(new Point(5, 32));
16 list.Add(new Point(6, 29));
17 list.Add(new Point(7, 27));
18 list.Add(new Point(8, 26));
19 list.Add(new Point(9, 24));
20 list.Add(new Point(10, 22));
21 list.Add(new Point(11, 21));
22 list.Add(new Point(12, 20));
23 list.Add(new Point(13, 19));
24 list.Add(new Point(14, 17));
25 list.Add(new Point(15, 16));
26 list.Add(new Point(16, 15));
27 list.Add(new Point(17, 14));
28 list.Add(new Point(19, 13));
29 list.Add(new Point(20, 12));
30 list.Add(new Point(21, 11));
31 list.Add(new Point(22, 10));
32 list.Add(new Point(24, 9));
33 list.Add(new Point(26, 8));
34 list.Add(new Point(27, 7));
35 list.Add(new Point(29, 6));
36 list.Add(new Point(32, 5));
37 list.Add(new Point(33, 4));
38 list.Add(new Point(36, 3));
39 list.Add(new Point(38, 2));
40 list.Add(new Point(42, 1));
41 list.Add(new Point(47, 0));
42
43 //右上
44 list.Add(new Point(width - 47, 0));
45 list.Add(new Point(width - 42, 1));
46 list.Add(new Point(width - 38, 2));
47 list.Add(new Point(width - 36, 3));
48 list.Add(new Point(width - 33, 4));
49 list.Add(new Point(width - 32, 5));
50 list.Add(new Point(width - 29, 6));
51 list.Add(new Point(width - 27, 7));
52 list.Add(new Point(width - 26, 8));
53 list.Add(new Point(width - 24, 9));
54 list.Add(new Point(width - 22, 10));
55 list.Add(new Point(width - 21, 11));
56 list.Add(new Point(width - 20, 12));
57 list.Add(new Point(width - 19, 13));
58 list.Add(new Point(width - 17, 14));
59 list.Add(new Point(width - 16, 15));
60 list.Add(new Point(width - 15, 16));
61 list.Add(new Point(width - 14, 17));
62 list.Add(new Point(width - 13, 19));
63 list.Add(new Point(width - 12, 20));
64 list.Add(new Point(width - 11, 21));
65 list.Add(new Point(width - 10, 22));
66 list.Add(new Point(width - 9, 24));
67 list.Add(new Point(width - 8, 26));
68 list.Add(new Point(width - 7, 27));
69 list.Add(new Point(width - 6, 29));
70 list.Add(new Point(width - 5, 32));
71 list.Add(new Point(width - 4, 33));
72 list.Add(new Point(width - 3, 36));
73 list.Add(new Point(width - 2, 38));
74 list.Add(new Point(width - 1, 42));
75 list.Add(new Point(width - 0, 47));
76
77 //右下
78 list.Add(new Point(width - 0, height - 47));
79 list.Add(new Point(width - 1, height - 42));
80 list.Add(new Point(width - 2, height - 38));
81 list.Add(new Point(width - 3, height - 36));
82 list.Add(new Point(width - 4, height - 33));
83 list.Add(new Point(width - 5, height - 32));
84 list.Add(new Point(width - 6, height - 29));
85 list.Add(new Point(width - 7, height - 27));
86 list.Add(new Point(width - 8, height - 26));
87 list.Add(new Point(width - 9, height - 24));
88 list.Add(new Point(width - 10, height - 22));
89 list.Add(new Point(width - 11, height - 21));
90 list.Add(new Point(width - 12, height - 20));
91 list.Add(new Point(width - 13, height - 19));
92 list.Add(new Point(width - 14, height - 17));
93 list.Add(new Point(width - 15, height - 16));
94 list.Add(new Point(width - 16, height - 15));
95 list.Add(new Point(width - 17, height - 14));
96 list.Add(new Point(width - 19, height - 13));
97 list.Add(new Point(width - 20, height - 12));
98 list.Add(new Point(width - 21, height - 11));
99 list.Add(new Point(width - 22, height - 10));
100 list.Add(new Point(width - 24, height - 9));
101 list.Add(new Point(width - 26, height - 8));
102 list.Add(new Point(width - 27, height - 7));
103 list.Add(new Point(width - 29, height - 6));
104 list.Add(new Point(width - 32, height - 5));
105 list.Add(new Point(width - 33, height - 4));
106 list.Add(new Point(width - 36, height - 3));
107 list.Add(new Point(width - 38, height - 2));
108 list.Add(new Point(width - 42, height - 1));
109 list.Add(new Point(width - 47, height - 0));
110
111 //左下
112 list.Add(new Point(47, height - 0));
113 list.Add(new Point(42, height - 1));
114 list.Add(new Point(38, height - 2));
115 list.Add(new Point(36, height - 3));
116 list.Add(new Point(33, height - 4));
117 list.Add(new Point(32, height - 5));
118 list.Add(new Point(29, height - 6));
119 list.Add(new Point(27, height - 7));
120 list.Add(new Point(26, height - 8));
121 list.Add(new Point(24, height - 9));
122 list.Add(new Point(22, height - 10));
123 list.Add(new Point(21, height - 11));
124 list.Add(new Point(20, height - 12));
125 list.Add(new Point(19, height - 13));
126 list.Add(new Point(17, height - 14));
127 list.Add(new Point(16, height - 15));
128 list.Add(new Point(15, height - 16));
129 list.Add(new Point(14, height - 17));
130 list.Add(new Point(13, height - 19));
131 list.Add(new Point(12, height - 20));
132 list.Add(new Point(11, height - 21));
133 list.Add(new Point(10, height - 22));
134 list.Add(new Point(9, height - 24));
135 list.Add(new Point(8, height - 26));
136 list.Add(new Point(7, height - 27));
137 list.Add(new Point(6, height - 29));
138 list.Add(new Point(5, height - 32));
139 list.Add(new Point(4, height - 33));
140 list.Add(new Point(3, height - 36));
141 list.Add(new Point(2, height - 38));
142 list.Add(new Point(1, height - 42));
143 list.Add(new Point(0, height - 47));
144
145 #endregion
146
147 Point[] points = list.ToArray();
148
149 GraphicsPath shape = new GraphicsPath();
150 shape.AddPolygon(points);
151
152 this.Region = new System.Drawing.Region(shape);
153
154 }
这样就把圆角窗口做出来了。需要说的是,这样设置出来的圆角是又毛刺的,精细程度跟你手动设置的点的个数有关,是个体力活。
借鉴了别人的工作,致敬~