轻松实现WinForm的渐入渐出效果

23 篇文章 0 订阅

 计时器timer是个很好用的控件,以下是在WinForm中实现一个web里面常用的渐入渐出效果。

 

  1 namespace  setOpacity
  2
  3 {
  4
  5    public partial class Form1 : Form
  6
  7    {
  8
  9        public Form1()
 10
 11        {
 12
 13            InitializeComponent();
 14
 15            //状态初始化
 16
 17            this.Opacity = 0;
 18
 19           show = true;
 20
 21           // timer1.Interval = 100;
 22
 23            //设置timer的运行间隔
 24
 25            timer1.Enabled = true;
 26
 27        }

 28
 29 
 30
 31        //定义两个状态
 32
 33        public bool show = true;
 34
 35        public bool close = false;
 36
 37 
 38
 39        
 40
 41        /// <summary>
 42
 43        /// 计时器中需要定时间间隔完成的内容
 44
 45        /// </summary>
 46
 47        /// <param name="sender"></param>
 48
 49        /// <param name="e"></param>

 50
 51        private void timer1_Tick(object sender, EventArgs e)
 52
 53        {
 54
 55            if (show)
 56
 57            {
 58
 59                if (this.Opacity < 1)
 60
 61                {
 62
 63                    this.Opacity = Opacity + 0.1;
 64
 65                }

 66
 67                else
 68
 69                {
 70
 71                    show = false;
 72
 73                    this.timer1.Enabled = false;
 74
 75                }

 76
 77            }

 78
 79 
 80
 81            if (close)
 82
 83            {
 84
 85                if (this.Opacity > 0.1)
 86
 87                {
 88
 89                    this.Opacity = this.Opacity - 0.1;
 90
 91                }

 92
 93                else
 94
 95                {
 96
 97                    close = false;
 98
 99                    this.timer1.Enabled = false;
100
101                    this.Close();
102
103                }

104
105            }

106
107        }

108
109 
110
111 
112
113        /// <summary>
114
115        /// 重写Form的关闭事件
116
117        /// </summary>
118
119        /// <param name="e"></param>

120
121        protected override void OnClosing(CancelEventArgs e)
122
123        
124
125            base.OnClosing(e);    
126
127            if (this.Opacity > 0.1)
128
129            {
130
131                e.Cancel = true;
132
133            }

134
135            timer1.Enabled = true;
136
137            close = true;
138
139        }

140
141    }

142
143}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值