ibcadmin 发表于 2012-12-9 14:16:32

C#实现窗体淡入淡出

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TransForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = false;
            inspeed = 10;
            outspeed = 10;
        }
        private System.Windows.Forms.Timer timer1;
        public System.Windows.Forms.Button button1;
        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // timer1
            //
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(342, 205);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(62, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "关闭";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(416, 240);
            this.ControlBox = false;
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Transform";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }
        #endregion
        private int state;//淡入\淡出状态
        private double inspeed;
        private double outspeed;
        public double InSpeed //淡入速度属性(0---100)
        {
            get
            {
                return inspeed;
            }
            set
            {
                inspeed = value;
            }
        }
        public double OutSpeed //淡出速度属性(0---100)
        {
            get
            {
                return outspeed;
            }
            set
            {
                outspeed = value;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            state = 0;
            timer1.Enabled = true;
            this.Opacity = 0;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (state >= 0)
            {
                this.Opacity += inspeed/100 ;
                if (this.Opacity == 1)
                {
                    timer1.Enabled = false;
                }
            }
            else if (state < 0)
            {
                this.Opacity -= outspeed/100 ;
                if (this.Opacity == 0)
                {
                    this.Close();
                    timer1.Enabled = false;
                }
            }
        }
        public void FormClose()//窗体关闭时,调用此函数实现淡出效果
        {
            state = -1;
            timer1.Enabled = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FormClose();
        }
    }
}复制粘贴到您的类中即可使用

ibcadmin 发表于 2012-12-9 14:16:50

收藏起来以后用

金贤重 发表于 2012-12-9 14:51:23

支持!不错的~挺~~~
上效果图:

金贤重 发表于 2012-12-9 14:51:59

呀图片我没有上,大家可以尝试弄下,效果还是不错的~

指尖的青春 发表于 2012-12-24 19:12:01

不错!值得学习,楼主继续











static/image/common/sigline.gif
不恋爱,不约会,
不旷工,不旅行,不撒野;
亲爱的青春,被狗吃了吗?

chao2332601 发表于 2013-6-16 02:06:48

谢谢分享!!!

chao2332601 发表于 2013-6-16 04:56:33

谢谢分享!!!

mars317 发表于 2014-6-7 23:25:07

谢谢分享!
页: [1]
查看完整版本: C#实现窗体淡入淡出