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

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

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

 2015/4/14 12:24:45  johnvwan  程序员俱乐部  我要评论(0)
  • 摘要:在实际项目中,我们经常需要弹出窗体来与用户交互,为了使弹出窗体不那么突兀,可以加些转换效果,淡入淡出就是其中一种。usingSystem;usingSystem.Windows.Forms;namespaceTestFormEffect{publicpartialclassForm1:Form{privatereadonlyTimer_timer;privatereadonlydouble_inspeed;privatereadonlydouble_outspeed
  • 标签:

  在实际项目中,我们经常需要弹出窗体来与用户交互,为了使弹出窗体不那么突兀,可以加些转换效果,淡入淡出就是其中一种。

using System;
using System.Windows.Forms;

namespace TestFormEffect
{
    public partial class Form1 : Form
    {
        private readonly Timer _timer;
        private readonly double _inspeed;
        private readonly double _outspeed;

        private State _state;

        public const float Precision = 0.000001f;

        public Form1()
        {
            InitializeComponent();
            _timer=new Timer();
            _timer.Tick += timer1_Tick; 
            _timer.Enabled = false;

            _inspeed = 20;
            _outspeed = 20;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _state = State.In;
            _timer.Enabled = true;
            Opacity = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (_state)
            {
                case State.In:
                    Opacity += _inspeed/100;
                    if (1 - Opacity <= Precision)
                    {
                        _timer.Enabled = false;
                    }
                    break;
                case State.Out:
                    Opacity -= _outspeed/100;
                    if (Opacity <= Precision)
                    {
                        this.Close();
                        _timer.Enabled = false;
                    }
                    break;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            _state = State.Out;
            _timer.Enabled = true;
        }

    }

    enum State
    {
        In=1,
        Out
    }
}

 

  • 相关文章
发表评论
用户名: 匿名