轻松实现WinForm的渐入渐出效果
计时器timer是个很好用的控件,以下是在WinForm中实现一个web里面常用的渐入渐出效果。
1
namespace setOpacity
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
public partial class Form1 : Form
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
{
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
public Form1()
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
InitializeComponent();
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
//状态初始化
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
this.Opacity = 0;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
show = true;
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
// timer1.Interval = 100;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
//设置timer的运行间隔
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
timer1.Enabled = true;
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
//定义两个状态
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
public bool show = true;
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
public bool close = false;
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
/// <summary>
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
/// 计时器中需要定时间间隔完成的内容
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
/// </summary>
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
/// <param name="sender"></param>
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
/// <param name="e"></param>
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
private void timer1_Tick(object sender, EventArgs e)
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
{
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
if (show)
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
{
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
if (this.Opacity < 1)
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
{
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
this.Opacity = Opacity + 0.1;
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
else
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
{
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
show = false;
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
this.timer1.Enabled = false;
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
}
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
if (close)
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
{
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
if (this.Opacity > 0.1)
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
{
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
this.Opacity = this.Opacity - 0.1;
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
}
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
else
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
{
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
close = false;
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
this.timer1.Enabled = false;
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
this.Close();
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
}
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
}
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
}
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
/// <summary>
114![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
/// 重写Form的关闭事件
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
/// </summary>
118![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
/// <param name="e"></param>
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
protected override void OnClosing(CancelEventArgs e)
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
{
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
base.OnClosing(e);
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
if (this.Opacity > 0.1)
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
{
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
e.Cancel = true;
132![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
}
134![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
timer1.Enabled = true;
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
close = true;
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
}
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
}
144![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
145![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
130
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
142
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
144
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
145
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行示例:http://newhost.byhh.net/f/DotNet/1181880733/setOpacity.exe
值得一提的是,timer是内部多线程。这杨我们可以方便的实现诸如可控制的动态数字之类的功能。有兴趣的朋友可以试一下。