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