代码改变世界

窗体慢慢从左到右地移动

2014-04-30 11:10  inguiq  阅读(154)  评论(0)    收藏  举报

摘自http://wenwen.sogou.com/z/q198038258.htm

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication
{
    public partial class Form1 : Form
    {
        Timer _tmr_start = new Timer();
        Timer _tmr_end = new Timer();
        public Form1()
        {
            InitializeComponent();
            this.Opacity = 0;
            TimeInitForm();
        }
        /// <summary>
        /// Shown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Shown(object sender, EventArgs e)
        {
            _tmr_start.Start();
        }
        /// <summary>
        /// FormClosing事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.ApplicationExitCall)
            {
                e.Cancel = true;
                _tmr_end.Start();
            }
        }
        private void TimeInitForm()
        {
            _tmr_start.Interval = 20;
            _tmr_end.Interval = 20;
            _tmr_start.Tick += new EventHandler(_tmr_start_Tick);
            _tmr_end.Tick += new EventHandler(_tmr_end_Tick);
        }
        void _tmr_end_Tick(object sender, EventArgs e)
        {
            if (this.Opacity == 0)
            {
                _tmr_end.Stop();
                Application.Exit();
                this.Close();
            }
            else
            {
                this.Location = new Point(this.Location.X + 5, this.Location.Y);
                this.Opacity = this.Opacity - 0.05;
            }
        }
        void _tmr_start_Tick(object sender, EventArgs e)
        {
            if (this.Opacity == 1)
            {
                _tmr_start.Stop();
            }
            else
            {
                this.Location = new Point(this.Location.X + 1, this.Location.Y);
                this.Opacity += 0.05;
            }
        }
    }
}