Toasts – Blame information for rev

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