Colorful form-fade in and fade out

Source: Internet
Author: User

Colorful form-fade in and fade out

In actual projects, we often need pop-up forms to interact with users. To make the pop-up forms not so abrupt, we can add some conversion effects. fade in and fade out is one of them.

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    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.