-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormAnimator.cs
347 lines (313 loc) · 11.1 KB
/
FormAnimator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace SC2Scrapbook
{
/// <summary>
/// Animates a form when it is shown, hidden or closed.
/// </summary>
/// <remarks>
/// MDI child forms do not support the Blend method and only support other methods while being displayed for the first time and when closing.
/// </remarks>
public sealed class FormAnimator
{
#region Types
/// <summary>
/// The methods of animation available.
/// </summary>
public enum AnimationMethod
{
/// <summary>
/// Rolls out from edge when showing and into edge when hiding.
/// </summary>
/// <remarks>
/// This is the default animation method and requires a direction.
/// </remarks>
Roll = 0x0,
/// <summary>
/// Expands out from centre when showing and collapses into centre when hiding.
/// </summary>
Centre = 0x10,
/// <summary>
/// Slides out from edge when showing and slides into edge when hiding.
/// </summary>
/// <remarks>
/// Requires a direction.
/// </remarks>
Slide = 0x40000,
/// <summary>
/// Fades from transaprent to opaque when showing and from opaque to transparent when hiding.
/// </summary>
Blend = 0x80000
}
/// <summary>
/// The directions in which the Roll and Slide animations can be shown.
/// </summary>
/// <remarks>
/// Horizontal and vertical directions can be combined to create diagonal animations.
/// </remarks>
[Flags()]
public enum AnimationDirection
{
/// <summary>
/// From left to right.
/// </summary>
Right = 0x1,
/// <summary>
/// From right to left.
/// </summary>
Left = 0x2,
/// <summary>
/// From top to bottom.
/// </summary>
Down = 0x4,
/// <summary>
/// From bottom to top.
/// </summary>
Up = 0x8
}
#endregion // Types
#region Constants
/// <summary>
/// Hide the form.
/// </summary>
private const int AW_HIDE = 0x10000;
/// <summary>
/// Activate the form.
/// </summary>
private const int AW_ACTIVATE = 0x20000;
/// <summary>
/// The number of milliseconds over which the animation occurs if no value is specified.
/// </summary>
private const int DEFAULT_DURATION = 250;
#endregion // Constants
#region Variables
/// <summary>
/// The form to be animated.
/// </summary>
private Form _form;
/// <summary>
/// The animation method used to show and hide the form.
/// </summary>
private AnimationMethod _method;
/// <summary>
/// The direction in which to Roll or Slide the form.
/// </summary>
private AnimationDirection _direction;
/// <summary>
/// The number of milliseconds over which the animation is played.
/// </summary>
private int _duration;
#endregion // Variables
#region Properties
/// <summary>
/// Gets or sets the animation method used to show and hide the form.
/// </summary>
/// <value>
/// The animation method used to show and hide the form.
/// </value>
/// <remarks>
/// <b>Roll</b> is used by default if no method is specified.
/// </remarks>
public AnimationMethod Method
{
get
{
return this._method;
}
set
{
this._method = value;
}
}
/// <summary>
/// Gets or Sets the direction in which the animation is performed.
/// </summary>
/// <value>
/// The direction in which the animation is performed.
/// </value>
/// <remarks>
/// The direction is only applicable to the <b>Roll</b> and <b>Slide</b> methods.
/// </remarks>
public AnimationDirection Direction
{
get
{
return this._direction;
}
set
{
this._direction = value;
}
}
/// <summary>
/// Gets or Sets the number of milliseconds over which the animation is played.
/// </summary>
/// <value>
/// The number of milliseconds over which the animation is played.
/// </value>
public int Duration
{
get
{
return this._duration;
}
set
{
this._duration = value;
}
}
/// <summary>
/// Gets the form to be animated.
/// </summary>
/// <value>
/// The form to be animated.
/// </value>
public Form Form
{
get
{
return this._form;
}
}
#endregion // Properties
#region APIs
/// <summary>
/// Windows API function to animate a window.
/// </summary>
[DllImport("user32")]
private extern static bool AnimateWindow(IntPtr hWnd,
int dwTime,
int dwFlags);
#endregion // APIs
#region Constructors
/// <summary>
/// Creates a new <b>FormAnimator</b> object for the specified form.
/// </summary>
/// <param name="form">
/// The form to be animated.
/// </param>
/// <remarks>
/// No animation will be used unless the <b>Method</b> and/or <b>Direction</b> properties are set independently. The <b>Duration</b> is set to quarter of a second by default.
/// </remarks>
public FormAnimator(Form form)
{
this._form = form;
this._form.Load += new EventHandler(Form_Load);
this._form.VisibleChanged += new EventHandler(Form_VisibleChanged);
this._form.Closing += new CancelEventHandler(Form_Closing);
this._duration = DEFAULT_DURATION;
}
/// <summary>
/// Creates a new <b>FormAnimator</b> object for the specified form using the specified method over the specified duration.
/// </summary>
/// <param name="form">
/// The form to be animated.
/// </param>
/// <param name="method">
/// The animation method used to show and hide the form.
/// </param>
/// <param name="duration">
/// The number of milliseconds over which the animation is played.
/// </param>
/// <remarks>
/// No animation will be used for the <b>Roll</b> or <b>Slide</b> methods unless the <b>Direction</b> property is set independently.
/// </remarks>
public FormAnimator(Form form,
AnimationMethod method,
int duration)
: this(form)
{
this._method = method;
this._duration = duration;
}
/// <summary>
/// Creates a new <b>FormAnimator</b> object for the specified form using the specified method in the specified direction over the specified duration.
/// </summary>
/// <param name="form">
/// The form to be animated.
/// </param>
/// <param name="method">
/// The animation method used to show and hide the form.
/// </param>
/// <param name="direction">
/// The direction in which to animate the form.
/// </param>
/// <param name="duration">
/// The number of milliseconds over which the animation is played.
/// </param>
/// <remarks>
/// The <i>direction</i> argument will have no effect if the <b>Centre</b> or <b>Blend</b> method is
/// specified.
/// </remarks>
public FormAnimator(Form form,
AnimationMethod method,
AnimationDirection direction,
int duration)
: this(form, method, duration)
{
this._direction = direction;
}
#endregion // Constructors
#region Event Handlers
/// <summary>
/// Animates the form automatically when it is loaded.
/// </summary>
private void Form_Load(object sender, EventArgs e)
{
// MDI child forms do not support transparency so do not try to use the Blend method.
if (this._form.MdiParent == null || this._method != AnimationMethod.Blend)
{
// Activate the form.
AnimateWindow(this._form.Handle,
this._duration,
AW_ACTIVATE | (int)this._method | (int)this._direction);
}
}
/// <summary>
/// Animates the form automatically when it is shown or hidden.
/// </summary>
private void Form_VisibleChanged(object sender, EventArgs e)
{
// Do not attempt to animate MDI child forms while showing or hiding as they do not behave as expected.
if (this._form.MdiParent == null)
{
int flags = (int)this._method | (int)this._direction;
if (this._form.Visible)
{
// Activate the form.
flags = flags | AW_ACTIVATE;
}
else
{
// Hide the form.
flags = flags | AW_HIDE;
}
AnimateWindow(this._form.Handle,
this._duration,
flags);
}
}
/// <summary>
/// Animates the form automatically when it closes.
/// </summary>
private void Form_Closing(object sender, CancelEventArgs e)
{
if (!e.Cancel)
{
// MDI child forms do not support transparency so do not try to use the Blend method.
if (this._form.MdiParent == null || this._method != AnimationMethod.Blend)
{
// Hide the form.
AnimateWindow(this._form.Handle,
this._duration,
AW_HIDE | (int)this._method | (int)this._direction);
}
}
}
#endregion // Event Handlers
}
}