码迷,mamicode.com
首页 > Windows程序 > 详细

炫彩效果——窗体淡入淡出

时间:2015-04-01 12:51:25      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

  在实际项目中,经常会有窗体弹出,为了不显得那么突兀,可以给加些动画过度效果,淡入淡出就是一种比较常见的动画。

 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace TestFormEffect
 5 {
 6     public partial class Form1 : Form
 7     {
 8         private readonly Timer _timer;
 9         private readonly double _inspeed;
10         private readonly double _outspeed;
11 
12         private State _state;
13 
14         public const float Precision = 0.000001f;
15 
16         public Form1()
17         {
18             InitializeComponent();
19             _timer=new Timer();
20             _timer.Tick += timer1_Tick; 
21             _timer.Enabled = false;
22 
23             _inspeed = 20;
24             _outspeed = 20;
25         }
26 
27         private void Form1_Load(object sender, EventArgs e)
28         {
29             _state = State.In;
30             _timer.Enabled = true;
31             Opacity = 0;
32         }
33 
34         private void timer1_Tick(object sender, EventArgs e)
35         {
36             switch (_state)
37             {
38                 case State.In:
39                     Opacity += _inspeed/100;
40                     if (1 - Opacity <= Precision)
41                     {
42                         _timer.Enabled = false;
43                     }
44                     break;
45                 case State.Out:
46                     Opacity -= _outspeed/100;
47                     if (Opacity <= Precision)
48                     {
49                         this.Close();
50                         _timer.Enabled = false;
51                     }
52                     break;
53             }
54         }
55 
56         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
57         {
58             e.Cancel = true;
59             _state = State.Out;
60             _timer.Enabled = true;
61         }
62 
63     }
64 
65     enum State
66     {
67         In=1,
68         Out
69     }
70 }

 

  

炫彩效果——窗体淡入淡出

标签:

原文地址:http://www.cnblogs.com/johnvwan/p/4383406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!