Toasts – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
41 office 1 // =====COPYRIGHT=====
2 // Code originally retrieved from http://www.vbforums.com/showthread.php?t=547778 - no license information supplied
3 // =====COPYRIGHT=====
4  
5 using System;
6 using System.ComponentModel;
7 using System.Windows.Forms;
8  
9 namespace Toasts
10 {
11 /// <summary>
12 /// Animates a form when it is shown, hidden or closed
13 /// </summary>
14 /// <remarks>
15 /// MDI child forms do not support the Fade method and only support other methods while being displayed for the first
16 /// time and when closing
17 /// </remarks>
18 public sealed class FormAnimator
19 {
20 #region Static Fields and Constants
21  
22 /// <summary>
23 /// Hide the form
24 /// </summary>
25 private const int AwHide = 0x10000;
26  
27 /// <summary>
28 /// Activate the form
29 /// </summary>
30 private const int AwActivate = 0x20000;
31  
32 /// <summary>
33 /// The number of milliseconds over which the animation occurs if no value is specified
34 /// </summary>
35 private const int DefaultDuration = 250;
36  
37 #endregion
38  
39 #region Public Enums, Properties and Fields
40  
41 /// <summary>
42 /// The directions in which the Roll and Slide animations can be shown
43 /// </summary>
44 /// <remarks>
45 /// Horizontal and vertical directions can be combined to create diagonal animations
46 /// </remarks>
47 [Flags]
48 public enum AnimationDirection
49 {
50 None = 0x0,
51  
52 /// <summary>
53 /// From left to right
54 /// </summary>
55 Right = 0x1,
56  
57 /// <summary>
58 /// From right to left
59 /// </summary>
60 Left = 0x2,
61  
62 /// <summary>
63 /// From top to bottom
64 /// </summary>
65 Down = 0x4,
66  
67 /// <summary>
68 /// From bottom to top
69 /// </summary>
70 Up = 0x8
71 }
72  
73 /// <summary>
74 /// The methods of animation available.
75 /// </summary>
76 public enum AnimationMethod
77 {
78 /// <summary>
79 /// Rolls out from edge when showing and into edge when hiding
80 /// </summary>
81 /// <remarks>
82 /// This is the default animation method and requires a direction
83 /// </remarks>
84 Roll = 0x0,
85  
86 /// <summary>
87 /// Expands out from center when showing and collapses into center when hiding
88 /// </summary>
89 Center = 0x10,
90  
91 /// <summary>
92 /// Slides out from edge when showing and slides into edge when hiding
93 /// </summary>
94 /// <remarks>
95 /// Requires a direction
96 /// </remarks>
97 Slide = 0x40000,
98  
99 /// <summary>
100 /// Fades from transparent to opaque when showing and from opaque to transparent when hiding
101 /// </summary>
102 Fade = 0x80000
103 }
104  
105 /// <summary>
106 /// Gets or sets the animation method used to show and hide the form
107 /// </summary>
108 /// <value>
109 /// The animation method used to show and hide the form
110 /// </value>
111 /// <remarks>
112 /// <b>Roll</b> is used by default if no method is specified
113 /// </remarks>
114 public AnimationMethod Method
115 {
116 get => _method;
117 set => _method = value;
118 }
119  
120 /// <summary>
121 /// Gets or Sets the direction in which the animation is performed
122 /// </summary>
123 /// <value>
124 /// The direction in which the animation is performed
125 /// </value>
126 /// <remarks>
127 /// The direction is only applicable to the <b>Roll</b> and <b>Slide</b> methods
128 /// </remarks>
129 public AnimationDirection Direction
130 {
131 get => _direction;
132 set => _direction = value;
133 }
134  
135 /// <summary>
136 /// Gets or Sets the number of milliseconds over which the animation is played
137 /// </summary>
138 /// <value>
139 /// The number of milliseconds over which the animation is played
140 /// </value>
141 public int Duration
142 {
143 get => _duration;
144 set => _duration = value;
145 }
146  
147 /// <summary>
148 /// Gets the form to be animated
149 /// </summary>
150 /// <value>
151 /// The form to be animated
152 /// </value>
153 public Form Form => _form;
154  
155 #endregion
156  
157 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
158  
159 /// <summary>
160 /// The form to be animated
161 /// </summary>
162 private readonly Form _form;
163  
164 /// <summary>
165 /// The direction in which to Roll or Slide the form
166 /// </summary>
167 private AnimationDirection _direction;
168  
169 /// <summary>
170 /// The number of milliseconds over which the animation is played
171 /// </summary>
172 private int _duration;
173  
174 /// <summary>
175 /// The animation method used to show and hide the form
176 /// </summary>
177 private AnimationMethod _method;
178  
179 #endregion
180  
181 #region Constructors, Destructors and Finalizers
182  
183 /// <summary>
184 /// Creates a new <b>FormAnimator</b> object for the specified form
185 /// </summary>
186 /// <param name="form">
187 /// The form to be animated
188 /// </param>
189 /// <remarks>
190 /// No animation will be used unless the <b>Method</b> and/or <b>Direction</b> properties are set independently. The
191 /// <b>Duration</b> is set to quarter of a second by default.
192 /// </remarks>
193 public FormAnimator(Form form)
194 {
195 _form = form;
196  
197 _form.Load += Form_Load;
198 _form.VisibleChanged += Form_VisibleChanged;
199 _form.Closing += Form_Closing;
200  
201 _duration = DefaultDuration;
202 }
203  
204 /// <summary>
205 /// Creates a new <b>FormAnimator</b> object for the specified form using the specified method over the specified
206 /// duration
207 /// </summary>
208 /// <param name="form">
209 /// The form to be animated
210 /// </param>
211 /// <param name="method">
212 /// The animation method used to show and hide the form
213 /// </param>
214 /// <param name="duration">
215 /// The number of milliseconds over which the animation is played
216 /// </param>
217 /// <remarks>
218 /// No animation will be used for the <b>Roll</b> or <b>Slide</b> methods unless the <b>Direction</b> property is set
219 /// independently
220 /// </remarks>
221 public FormAnimator(Form form, AnimationMethod method, int duration) : this(form)
222 {
223 _method = method;
224 _duration = duration;
225 }
226  
227 /// <summary>
228 /// Creates a new <b>FormAnimator</b> object for the specified form using the specified method in the specified
229 /// direction over the specified duration
230 /// </summary>
231 /// <param name="form">
232 /// The form to be animated
233 /// </param>
234 /// <param name="method">
235 /// The animation method used to show and hide the form
236 /// </param>
237 /// <param name="direction">
238 /// The direction in which to animate the form
239 /// </param>
240 /// <param name="duration">
241 /// The number of milliseconds over which the animation is played
242 /// </param>
243 /// <remarks>
244 /// The <i>direction</i> argument will have no effect if the <b>Center</b> or <b>Fade</b> method is
245 /// specified
246 /// </remarks>
247 public FormAnimator(Form form, AnimationMethod method, AnimationDirection direction, int duration) : this(form,
248 method, duration)
249 {
250 _direction = direction;
251 }
252  
253 #endregion
254  
255 #region Event Handlers
256  
257 /// <summary>
258 /// Animates the form automatically when it is loaded
259 /// </summary>
260 private void Form_Load(object sender, EventArgs e)
261 {
262 // MDI child forms do not support transparency so do not try to use the Fade method
263 if (_form.MdiParent == null || _method != AnimationMethod.Fade)
264 NativeMethods.AnimateWindow(_form.Handle, _duration, AwActivate | (int)_method | (int)_direction);
265 }
266  
267 /// <summary>
268 /// Animates the form automatically when it is shown or hidden
269 /// </summary>
270 private void Form_VisibleChanged(object sender, EventArgs e)
271 {
272 // Do not attempt to animate MDI child forms while showing or hiding as they do not behave as expected
273 if (_form.MdiParent == null)
274 {
275 var flags = (int)_method | (int)_direction;
276  
277 if (_form.Visible)
278 flags = flags | AwActivate;
279 else
280 flags = flags | AwHide;
281  
282 NativeMethods.AnimateWindow(_form.Handle, _duration, flags);
283 }
284 }
285  
286 /// <summary>
287 /// Animates the form automatically when it closes
288 /// </summary>
289 private void Form_Closing(object sender, CancelEventArgs e)
290 {
291 if (!e.Cancel)
292 // MDI child forms do not support transparency so do not try to use the Fade method.
293 if (_form.MdiParent == null || _method != AnimationMethod.Fade)
294 NativeMethods.AnimateWindow(_form.Handle, _duration, AwHide | (int)_method | (int)_direction);
295 }
296  
297 #endregion
298 }
1 office 299 }