马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现窗体淡入淡出

  [复制链接]
查看7345 | 回复7 | 2012-12-9 14:16:32 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace TransForm
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.             timer1.Enabled = false;
  16.             inspeed = 10;
  17.             outspeed = 10;
  18.         }
  19.         private System.Windows.Forms.Timer timer1;
  20.         public System.Windows.Forms.Button button1;
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.components = new System.ComponentModel.Container();
  29.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  30.             this.button1 = new System.Windows.Forms.Button();
  31.             this.SuspendLayout();
  32.             //
  33.             // timer1
  34.             //
  35.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  36.             //
  37.             // button1
  38.             //
  39.             this.button1.Location = new System.Drawing.Point(342, 205);
  40.             this.button1.Name = "button1";
  41.             this.button1.Size = new System.Drawing.Size(62, 23);
  42.             this.button1.TabIndex = 0;
  43.             this.button1.Text = "关闭";
  44.             this.button1.UseVisualStyleBackColor = true;
  45.             this.button1.Click += new System.EventHandler(this.button1_Click);
  46.             //
  47.             // Form1
  48.             //
  49.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  50.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  51.             this.ClientSize = new System.Drawing.Size(416, 240);
  52.             this.ControlBox = false;
  53.             this.Controls.Add(this.button1);
  54.             this.Name = "Form1";
  55.             this.Text = "Transform";
  56.             this.Load += new System.EventHandler(this.Form1_Load);
  57.             this.ResumeLayout(false);
  58.         }
  59.         #endregion
  60.         private int state;//淡入\淡出状态
  61.         private double inspeed;
  62.         private double outspeed;
  63.         public double InSpeed //淡入速度属性(0---100)
  64.         {
  65.             get
  66.             {
  67.                 return inspeed;
  68.             }
  69.             set
  70.             {
  71.                 inspeed = value;
  72.             }
  73.         }
  74.         public double OutSpeed //淡出速度属性(0---100)
  75.         {
  76.             get
  77.             {
  78.                 return outspeed;
  79.             }
  80.             set
  81.             {
  82.                 outspeed = value;
  83.             }
  84.         }
  85.         private void Form1_Load(object sender, EventArgs e)
  86.         {
  87.             state = 0;
  88.             timer1.Enabled = true;
  89.             this.Opacity = 0;
  90.         }
  91.         private void timer1_Tick(object sender, EventArgs e)
  92.         {
  93.             if (state >= 0)
  94.             {
  95.                 this.Opacity += inspeed/100 ;
  96.                 if (this.Opacity == 1)
  97.                 {
  98.                     timer1.Enabled = false;
  99.                 }
  100.             }
  101.             else if (state < 0)
  102.             {
  103.                 this.Opacity -= outspeed/100 ;
  104.                 if (this.Opacity == 0)
  105.                 {
  106.                     this.Close();
  107.                     timer1.Enabled = false;
  108.                 }
  109.             }
  110.         }
  111.         public void FormClose()//窗体关闭时,调用此函数实现淡出效果
  112.         {
  113.             state = -1;
  114.             timer1.Enabled = true;
  115.         }
  116.         private void button1_Click(object sender, EventArgs e)
  117.         {
  118.             FormClose();
  119.         }
  120.     }
  121. }
复制代码
复制粘贴到您的类中即可使用
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2012-12-9 14:16:50 | 显示全部楼层
收藏起来以后用
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
金贤重 | 2012-12-9 14:51:23 | 显示全部楼层
支持!不错的~挺~~~
上效果图:
金贤重 | 2012-12-9 14:51:59 | 显示全部楼层
呀图片我没有上,大家可以尝试弄下,效果还是不错的~
指尖的青春 | 2012-12-24 19:12:01 | 显示全部楼层
不错!值得学习,楼主继续












不恋爱,不约会,
不旷工,不旅行,不撒野;
亲爱的青春,被狗吃了吗?
chao2332601 | 2013-6-16 02:06:48 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:56:33 | 显示全部楼层
谢谢分享!!!
mars317 | 2014-6-7 23:25:07 | 显示全部楼层
谢谢分享!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则