炫彩效果——窗体淡入淡出_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 炫彩效果——窗体淡入淡出

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

 2015/4/1 15:33:51  johnvwan  程序员俱乐部  我要评论(0)
  • 摘要:在实际项目中,经常会有窗体弹出,为了不显得那么突兀,可以给加些动画过度效果,淡入淡出就是一种比较常见的动画。1usingSystem;2usingSystem.Windows.Forms;34namespaceTestFormEffect5{6publicpartialclassForm1:Form7{8privatereadonlyTimer_timer;9privatereadonlydouble_inspeed;10privatereadonlydouble_outspeed
  • 标签:

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

 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 }

 

  

上一篇: C#基础总复习02 下一篇: wcf学习之路
  • 相关文章
发表评论
用户名: 匿名