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