窗体启动特效
【思路】:
在Form的OnLoad中 先使Form不可见,然后从内向外,一层一层绘制Rectangle ,最后让Form可见
【难点】
首先输出效果的Rectangle要有个地方显示,Main Form可不行,因为当绘制Rectangle的时候 ,Form是不可见的,这里使用了Desktop桌面
【代码如下】
1. 加入命名空间
1 using System.Drawing.Imaging;
2
3 using System.Runtime.InteropServices;
4
2
3 using System.Runtime.InteropServices;
4
2. 声明Win32 Api GetDC()
1 [ DllImport("user32") ]
2
3 public static extern System.IntPtr GetDC(System.IntPtr dc);
4
2
3 public static extern System.IntPtr GetDC(System.IntPtr dc);
4
3. 声明变量
1 System.Drawing.Graphics g; //画图板
2
3 Pen p=new Pen(Color.Black,1); //画笔
4
5 int startx,starty,wx,wy,step; //startx,starty,wx,wy确定一个矩形
6
7 int cx,cy; //cx,cy为form的client的width 和height
8
2
3 Pen p=new Pen(Color.Black,1); //画笔
4
5 int startx,starty,wx,wy,step; //startx,starty,wx,wy确定一个矩形
6
7 int cx,cy; //cx,cy为form的client的width 和height
8
4.在Form的OnLoad上加上如下代码
1 this.Visible=false;
2
3 step=1;
4
5 g=Graphics.FromHdc(GetDC(System.IntPtr.Zero));
6
7 cx=this.ClientSize.Width;
8
9 cy=this.ClientSize.Height;
10
11 this.Visible=false;
12
13 step=1;
14
15 while(step<=cx/2)
16
17 {
18
19 startx=cx/2-step;
20
21 starty=cy*startx/cx;
22
23 wx=2*step;
24
25 wy=wx*cy/cx;
26
27 startx+=this.Left;
28
29 starty+=this.Top+this.Height-this.ClientSize.Height;
30
31 g.DrawRectangle(p,startx,starty,wx,wy);
32
33 System.Threading.Thread.Sleep(100);
34
35 step+=10;
36
37 }
38
39 this.Visible=true;
40
2
3 step=1;
4
5 g=Graphics.FromHdc(GetDC(System.IntPtr.Zero));
6
7 cx=this.ClientSize.Width;
8
9 cy=this.ClientSize.Height;
10
11 this.Visible=false;
12
13 step=1;
14
15 while(step<=cx/2)
16
17 {
18
19 startx=cx/2-step;
20
21 starty=cy*startx/cx;
22
23 wx=2*step;
24
25 wy=wx*cy/cx;
26
27 startx+=this.Left;
28
29 starty+=this.Top+this.Height-this.ClientSize.Height;
30
31 g.DrawRectangle(p,startx,starty,wx,wy);
32
33 System.Threading.Thread.Sleep(100);
34
35 step+=10;
36
37 }
38
39 this.Visible=true;
40