PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Platters/classes/formanimator.cs

http://skimpt.googlecode.com/
C# | 348 lines | 122 code | 39 blank | 187 comment | 13 complexity | b0f8499b697319458d0fa32bba9cbfad MD5 | raw file
  1#region "License Agreement"
  2/* Skimpt, an open source screenshot utility.
  3      Copyright (C) <year>  <name of author>
  4
  5      This program is free software: you can redistribute it and/or modify
  6      it under the terms of the GNU General Public License as published by
  7      the Free Software Foundation, either version 3 of the License, or
  8      (at your option) any later version.
  9
 10      this program is distributed in the hope that it will be useful,
 11      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13      GNU General Public License for more details.
 14
 15      You should have received a copy of the GNU General Public License
 16      along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 17#endregion
 18using System;
 19using System.Collections.Generic;
 20using System.Linq;
 21using System.Text;
 22using System.ComponentModel; 
 23using System.Runtime.InteropServices;
 24using System.Windows.Forms;
 25
 26
 27/// ----------------------------------------------------------------------------- 
 28/// Project : Wunnell.Windows.Forms 
 29/// Class : FormAnimator 
 30/// 
 31/// ----------------------------------------------------------------------------- 
 32/// <summary> 
 33/// Animates a form when it is shown, hidden or closed. 
 34/// </summary> 
 35/// <remarks> 
 36/// MDI child forms do not support the Blend method and only support other 
 37/// methods while being displayed for the first time and when closing. 
 38/// </remarks> 
 39/// <history> 
 40/// [John] 31/08/2005 Created 
 41/// [Affan] October 25, 2008 Converted to C#.net
 42/// </history> 
 43/// ----------------------------------------------------------------------------- 
 44public sealed class FormAnimator 
 45{ 
 46    
 47    #region " Types " 
 48    
 49    /// ----------------------------------------------------------------------------- 
 50    /// <summary> 
 51    /// The methods of animation available. 
 52    /// </summary> 
 53    /// <remarks> 
 54    /// </remarks> 
 55    /// <history> 
 56    /// [John] 31/08/2005 Created 
 57    /// </history> 
 58    /// ----------------------------------------------------------------------------- 
 59    public enum AnimationMethod 
 60    { 
 61        [Description("Default animation method. Rolls out from edge when showing and into edge when hiding. Requires a direction.")] 
 62        Roll = 0x0, 
 63        [Description("Expands out from centre when showing and collapses into centre when hiding.")] 
 64        Centre = 0x10, 
 65        [Description("Slides out from edge when showing and slides into edge when hiding. Requires a direction.")] 
 66        Slide = 0x40000, 
 67        [Description("Fades from transaprent to opaque when showing and from opaque to transparent when hiding.")] 
 68        Blend = 0x80000 
 69    } 
 70    
 71    /// ----------------------------------------------------------------------------- 
 72    /// <summary> 
 73    /// The directions in which the Roll and Slide animations can be shown. 
 74    /// </summary> 
 75    /// <remarks> 
 76    /// Horizontal and vertical directions can be combined to create diagonal animations. 
 77    /// </remarks> 
 78    /// <history> 
 79    /// [John] 31/08/2005 Created 
 80    /// </history> 
 81    /// ----------------------------------------------------------------------------- 
 82    [Flags()] 
 83    public enum AnimationDirection 
 84    { 
 85        [Description("From left to right.")] 
 86        Right = 0x1, 
 87        [Description("From right to left.")] 
 88        Left = 0x2, 
 89        [Description("From top to bottom.")] 
 90        Down = 0x4, 
 91        [Description("From bottom to top.")] 
 92        Up = 0x8 
 93    } 
 94    
 95    #endregion 
 96    
 97    #region " Constants " 
 98    
 99        //Hide the form. 
100    private const int AW_HIDE = 0x10000; 
101        //Activate the form. 
102    private const int AW_ACTIVATE = 0x20000; 
103    
104    #endregion 
105    
106    #region " Variables " 
107    
108        //The form to be animated. 
109    private Form m_Form; 
110    
111        //The animation method used to show and hide the form. 
112    private AnimationMethod m_Method; 
113        //The direction in which to Roll or Slide the form. 
114    private AnimationDirection m_Direction; 
115        //The number of milliseconds over which the animation is played. 
116    private int m_Duration; 
117    
118    #endregion 
119    
120    #region " Properties " 
121    
122    /// ----------------------------------------------------------------------------- 
123    /// <summary> 
124    /// Gets or sets the animation method used to show and hide the form. 
125    /// </summary> 
126    /// <value> 
127    /// The animation method used to show and hide the form. 
128    /// </value> 
129    /// <remarks> 
130    /// Roll is used by default if no method is specified. 
131    /// </remarks> 
132    /// <history> 
133    /// [John] 31/08/2005 Created 
134    /// </history> 
135    /// ----------------------------------------------------------------------------- 
136    [Description("Gets or sets the animation method used to show and hide the form.")] 
137    public AnimationMethod Method { 
138        get { return this.m_Method; } 
139        set { this.m_Method = value; } 
140    } 
141    
142    /// ----------------------------------------------------------------------------- 
143    /// <summary> 
144    /// Gets or sets the direction in which the animation is performed. 
145    /// </summary> 
146    /// <value> 
147    /// The direction in which the animation is performed. 
148    /// </value> 
149    /// <remarks> 
150    /// The direction is only applicable to the Roll and Slide methods. 
151    /// </remarks> 
152    /// <history> 
153    /// [John] 31/08/2005 Created 
154    /// </history> 
155    /// ----------------------------------------------------------------------------- 
156    [Description("Gets or sets the direction in which the animation is performed.")] 
157    public AnimationDirection Direction { 
158        get { return this.m_Direction; } 
159        set { this.m_Direction = value; } 
160    } 
161    
162    /// ----------------------------------------------------------------------------- 
163    /// <summary> 
164    /// Gets or sets the number of milliseconds over which the animation is played. 
165    /// </summary> 
166    /// <value> 
167    /// The number of milliseconds over which the animation is played. 
168    /// </value> 
169    /// <remarks> 
170    /// </remarks> 
171    /// <history> 
172    /// [John] 5/09/2005 Created 
173    /// </history> 
174    /// ----------------------------------------------------------------------------- 
175    [Description("Gets or sets the number of milliseconds over which the animation is played.")] 
176    public int Duration { 
177        get { return this.m_Duration; } 
178        set { this.m_Duration = value; } 
179    } 
180    
181    /// ----------------------------------------------------------------------------- 
182    /// <summary> 
183    /// Gets the form to be animated. 
184    /// </summary> 
185    /// <value> 
186    /// The form to be animated. 
187    /// </value> 
188    /// <remarks> 
189    /// </remarks> 
190    /// <history> 
191    /// [John] 5/09/2005 Created 
192    /// </history> 
193    /// ----------------------------------------------------------------------------- 
194    [Description("Gets the form to be animated.")] 
195    public Form Form { 
196        get { return this.m_Form; } 
197    } 
198    [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
199    private static extern bool AnimateWindow(IntPtr hWnd, int dwTime, int dwFlags); 
200    
201    #endregion 
202    
203
204    
205    #region " Constructors " 
206    
207    /// ----------------------------------------------------------------------------- 
208    /// <summary> 
209    /// Creates a new FormAnimator object for the specified form. 
210    /// </summary> 
211    /// <param name="form"> 
212    /// The form to be animated. 
213    /// </param> 
214    /// <remarks> 
215    /// No animation will be used unless the Method and/or Direction properties are 
216    /// set independently. The duration is set to quarter of a second by default. 
217    /// </remarks> 
218    /// <history> 
219    /// [John] 5/09/2005 Created 
220    /// </history> 
221    /// ----------------------------------------------------------------------------- 
222    public FormAnimator(Form form) 
223    { 
224        this.m_Form = form; 
225        
226       
227        this.m_Form.Load += new EventHandler(m_Form_Load);
228        this.m_Form.VisibleChanged += new EventHandler(m_Form_VisibleChanged);
229        this.m_Form.Closing += new CancelEventHandler(m_Form_Closing);
230        //The default animation length is quarter of a second. 
231        this.m_Duration = 250; 
232    } 
233    
234    /// ----------------------------------------------------------------------------- 
235    /// <summary> 
236    /// Creates a new FormAnimator object for the specified form using the specified 
237    /// method over the specified duration. 
238    /// </summary> 
239    /// <param name="form"> 
240    /// The form to be animated. 
241    /// </param> 
242    /// <param name="method"> 
243    /// The animation method used to show and hide the form. 
244    /// </param> 
245    /// <param name="duration"> 
246    /// The number of milliseconds over which the animation is played. 
247    /// </param> 
248    /// <remarks> 
249    /// No animation will be used for the Roll or Slide methods unless the Direction 
250    /// property is set independently. 
251    /// </remarks> 
252    /// <history> 
253    /// [John] 5/09/2005 Created 
254    /// </history> 
255    /// ----------------------------------------------------------------------------- 
256    public FormAnimator(Form form, AnimationMethod method, int duration) : this(form) 
257    {         
258        this.m_Method = method; 
259        this.m_Duration = duration; 
260    } 
261    
262    // 
263    /// ----------------------------------------------------------------------------- 
264    /// <summary> 
265    /// Creates a new FormAnimator object for the specified form using the specified 
266    /// method in the specified direction over the specified duration. 
267    /// </summary> 
268    /// <param name="form"> 
269    /// The form to be animated. 
270    /// </param> 
271    /// <param name="method"> 
272    /// The animation method used to show and hide the form. 
273    /// </param> 
274    /// <param name="direction"> 
275    /// The direction in which to animate the form. 
276    /// </param> 
277    /// <param name="duration"> 
278    /// The number of milliseconds over which the animation is played. 
279    /// </param> 
280    /// <remarks> 
281    /// The direction argument will have no effect if the Centre or Blend method is 
282    /// specified. 
283    /// </remarks> 
284    /// <history> 
285    /// [John] 5/09/2005 Created 
286    /// </history> 
287    /// ----------------------------------------------------------------------------- 
288    public FormAnimator(Form form, AnimationMethod method, AnimationDirection direction, int duration) : this(form, method, duration) 
289    { 
290        
291        this.m_Direction = direction; 
292    } 
293    ~FormAnimator ()
294    {
295        this.m_Form = null;
296    }
297    #endregion 
298    
299    #region " Event Handlers " 
300    
301    //Animates the form automatically when it is loaded. 
302    private void m_Form_Load(object sender, System.EventArgs e)  
303    { 
304        //MDI child forms do not support transparency so do not try to use the Blend method. 
305        if (this.m_Form.MdiParent == null || this.m_Method != AnimationMethod.Blend) { 
306            //Activate the form. 
307            FormAnimator.AnimateWindow(this.m_Form.Handle, this.m_Duration, (int)FormAnimator.AW_ACTIVATE | (int)this.m_Method | (int)this.m_Direction); 
308        } 
309    } 
310    
311    //Animates the form automatically when it is shown or hidden. 
312    private void m_Form_VisibleChanged(object sender, System.EventArgs e)  // ERROR: Handles clauses are not supported in C# 
313    { 
314        //Do not attempt to animate MDI child forms while showing or hiding as they do not behave as expected. 
315        if (this.m_Form.MdiParent == null) { 
316            int flags = (int)this.m_Method | (int)this.m_Direction; 
317            
318            if (this.m_Form.Visible) {
319               
320                //Activate the form. 
321                flags = flags | FormAnimator.AW_ACTIVATE; 
322            } 
323            else {
324               
325                //Hide the form. 
326                flags = flags | FormAnimator.AW_HIDE; 
327            } 
328            
329            FormAnimator.AnimateWindow(this.m_Form.Handle, this.m_Duration, flags); 
330        } 
331    } 
332    
333    //Animates the form automatically when it closes. 
334    private void m_Form_Closing(object sender, System.ComponentModel.CancelEventArgs e) // ERROR: Handles clauses are not supported in C#  
335    {
336        
337        if (!e.Cancel) { 
338            //MDI child forms do not support transparency so do not try to use the Blend method. 
339            if (this.m_Form.MdiParent == null || this.m_Method != AnimationMethod.Blend) { 
340                //Hide the form. 
341                FormAnimator.AnimateWindow(this.m_Form.Handle, this.m_Duration, (int)FormAnimator.AW_HIDE | (int)this.m_Method | (int)AnimationDirection.Down); 
342            } 
343
344        } 
345    }     
346    #endregion 
347    
348}