PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Lab Hours/Notification/FormAnimator.vb

#
Visual Basic | 313 lines | 118 code | 36 blank | 159 comment | 0 complexity | 6a68a5dfe0562301b8f02020ca849500 MD5 | raw file
  1. 'found at http://www.vbforums.com/showthread.php?t=351757
  2. Imports System.ComponentModel
  3. Imports System.Runtime.InteropServices
  4. ''' <summary>
  5. ''' Animates a form when it is shown, hidden or closed.
  6. ''' </summary>
  7. ''' <remarks>
  8. ''' MDI child forms do not support the Blend method and only support other methods while being displayed for the first time and when closing.
  9. ''' </remarks>
  10. Public NotInheritable Class FormAnimator
  11. #Region " Types "
  12. ''' <summary>
  13. ''' The methods of animation available.
  14. ''' </summary>
  15. Public Enum AnimationMethod
  16. ''' <summary>
  17. ''' Rolls out from edge when showing and into edge when hiding.
  18. ''' </summary>
  19. ''' <remarks>
  20. ''' This is the default animation method and requires a direction.
  21. ''' </remarks>
  22. Roll = &H0
  23. ''' <summary>
  24. ''' Expands out from centre when showing and collapses into centre when hiding.
  25. ''' </summary>
  26. Centre = &H10
  27. ''' <summary>
  28. ''' Slides out from edge when showing and slides into edge when hiding.
  29. ''' </summary>
  30. ''' <remarks>
  31. ''' Requires a direction.
  32. ''' </remarks>
  33. Slide = &H40000
  34. ''' <summary>
  35. ''' Fades from transaprent to opaque when showing and from opaque to transparent when hiding.
  36. ''' </summary>
  37. Blend = &H80000
  38. End Enum
  39. ''' <summary>
  40. ''' The directions in which the Roll and Slide animations can be shown.
  41. ''' </summary>
  42. ''' <remarks>
  43. ''' Horizontal and vertical directions can be combined to create diagonal animations.
  44. ''' </remarks>
  45. <Flags()> Public Enum AnimationDirection
  46. ''' <summary>
  47. ''' From left to right.
  48. ''' </summary>
  49. Right = &H1
  50. ''' <summary>
  51. ''' From right to left.
  52. ''' </summary>
  53. Left = &H2
  54. ''' <summary>
  55. ''' From top to bottom.
  56. ''' </summary>
  57. Down = &H4
  58. ''' <summary>
  59. ''' From bottom to top.
  60. ''' </summary>
  61. Up = &H8
  62. End Enum
  63. #End Region 'Types
  64. #Region " Constants "
  65. ''' <summary>
  66. ''' Hide the form.
  67. ''' </summary>
  68. Private Const AW_HIDE As Integer = &H10000
  69. ''' <summary>
  70. ''' Activate the form.
  71. ''' </summary>
  72. Private Const AW_ACTIVATE As Integer = &H20000
  73. ''' <summary>
  74. ''' The number of milliseconds over which the animation occurs if no value is specified.
  75. ''' </summary>
  76. Private Const DEFAULT_DURATION As Integer = 250
  77. #End Region 'Constants
  78. #Region " Variables "
  79. ''' <summary>
  80. ''' The form to be animated.
  81. ''' </summary>
  82. Private WithEvents _form As Form
  83. ''' <summary>
  84. ''' The animation method used to show and hide the form.
  85. ''' </summary>
  86. Private _method As AnimationMethod
  87. ''' <summary>
  88. ''' The direction in which to Roll or Slide the form.
  89. ''' </summary>
  90. Private _direction As AnimationDirection
  91. ''' <summary>
  92. ''' The number of milliseconds over which the animation is played.
  93. ''' </summary>
  94. Private _duration As Integer
  95. #End Region 'Variables
  96. #Region " Properties "
  97. ''' <summary>
  98. ''' Gets or sets the animation method used to show and hide the form.
  99. ''' </summary>
  100. ''' <value>
  101. ''' The animation method used to show and hide the form.
  102. ''' </value>
  103. ''' <remarks>
  104. ''' <b>Roll</b> is used by default if no method is specified.
  105. ''' </remarks>
  106. Public Property Method() As AnimationMethod
  107. Get
  108. Return Me._method
  109. End Get
  110. Set(ByVal Value As AnimationMethod)
  111. Me._method = Value
  112. End Set
  113. End Property
  114. ''' <summary>
  115. ''' Gets or sets the direction in which the animation is performed.
  116. ''' </summary>
  117. ''' <value>
  118. ''' The direction in which the animation is performed.
  119. ''' </value>
  120. ''' <remarks>
  121. ''' The direction is only applicable to the <b>Roll</b> and <b>Slide</b> methods.
  122. ''' </remarks>
  123. Public Property Direction() As AnimationDirection
  124. Get
  125. Return Me._direction
  126. End Get
  127. Set(ByVal Value As AnimationDirection)
  128. Me._direction = Value
  129. End Set
  130. End Property
  131. ''' <summary>
  132. ''' Gets or sets the number of milliseconds over which the animation is played.
  133. ''' </summary>
  134. ''' <value>
  135. ''' The number of milliseconds over which the animation is played.
  136. ''' </value>
  137. Public Property Duration() As Integer
  138. Get
  139. Return Me._duration
  140. End Get
  141. Set(ByVal Value As Integer)
  142. Me._duration = Value
  143. End Set
  144. End Property
  145. ''' <summary>
  146. ''' Gets the form to be animated.
  147. ''' </summary>
  148. ''' <value>
  149. ''' The form to be animated.
  150. ''' </value>
  151. Public ReadOnly Property Form() As Form
  152. Get
  153. Return Me._form
  154. End Get
  155. End Property
  156. #End Region 'Properties
  157. #Region " APIs "
  158. ''' <summary>
  159. ''' Windows API function to animate a window.
  160. ''' </summary>
  161. <DllImport("user32")> _
  162. Private Shared Function AnimateWindow(ByVal hWnd As IntPtr, _
  163. ByVal dwTime As Integer, _
  164. ByVal dwFlags As Integer) As Boolean
  165. End Function
  166. #End Region 'APIs
  167. #Region " Constructors "
  168. ''' <summary>
  169. ''' Creates a new <b></b>FormAnimator object for the specified form.
  170. ''' </summary>
  171. ''' <param name="form">
  172. ''' The form to be animated.
  173. ''' </param>
  174. ''' <remarks>
  175. ''' 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.
  176. ''' </remarks>
  177. Public Sub New(ByVal form As Form)
  178. Me._form = form
  179. Me._duration = DEFAULT_DURATION
  180. End Sub
  181. ''' <summary>
  182. ''' Creates a new <b>FormAnimator</b> object for the specified form using the specified method over the specified duration.
  183. ''' </summary>
  184. ''' <param name="form">
  185. ''' The form to be animated.
  186. ''' </param>
  187. ''' <param name="method">
  188. ''' The animation method used to show and hide the form.
  189. ''' </param>
  190. ''' <param name="duration">
  191. ''' The number of milliseconds over which the animation is played.
  192. ''' </param>
  193. ''' <remarks>
  194. ''' No animation will be used for the <b>Roll</b> or <b>Slide</b> methods unless the <b>Direction</b> property is set independently.
  195. ''' </remarks>
  196. Public Sub New(ByVal form As Form, _
  197. ByVal method As AnimationMethod, _
  198. ByVal duration As Integer)
  199. Me.New(form)
  200. Me._method = method
  201. Me._duration = duration
  202. End Sub
  203. ''' <summary>
  204. ''' Creates a new <b>FormAnimator</b> object for the specified form using the specified method in the specified direction over the specified 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="direction">
  213. ''' The direction in which to animate the form.
  214. ''' </param>
  215. ''' <param name="duration">
  216. ''' The number of milliseconds over which the animation is played.
  217. ''' </param>
  218. ''' <remarks>
  219. ''' The <i>direction</i> argument will have no effect if the <b>Centre</b> or <b>Blend</b> method is
  220. ''' specified.
  221. ''' </remarks>
  222. Public Sub New(ByVal form As Form, _
  223. ByVal method As AnimationMethod, _
  224. ByVal direction As AnimationDirection, _
  225. ByVal duration As Integer)
  226. Me.New(form, method, duration)
  227. Me._direction = direction
  228. End Sub
  229. #End Region 'Constructors
  230. #Region " Event Handlers "
  231. ''' <summary>
  232. ''' Animates the form automatically when it is loaded.
  233. ''' </summary>
  234. Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles _form.Load
  235. 'MDI child forms do not support transparency so do not try to use the Blend method.
  236. If Me._form.MdiParent Is Nothing OrElse Me._method <> AnimationMethod.Blend Then
  237. 'Activate the form.
  238. AnimateWindow(Me._form.Handle, _
  239. Me._duration, _
  240. AW_ACTIVATE Or Me._method Or Me._direction)
  241. End If
  242. End Sub
  243. ''' <summary>
  244. ''' Animates the form automatically when it is shown or hidden.
  245. ''' </summary>
  246. Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _form.VisibleChanged
  247. 'Do not attempt to animate MDI child forms while showing or hiding as they do not behave as expected.
  248. If Me._form.MdiParent Is Nothing Then
  249. Dim flags As Integer = Me._method Or Me._direction
  250. If Me._form.Visible Then
  251. 'Activate the form.
  252. flags = flags Or AW_ACTIVATE
  253. Else
  254. 'Hide the form.
  255. flags = flags Or AW_HIDE
  256. End If
  257. AnimateWindow(Me._form.Handle, _
  258. Me._duration, _
  259. flags)
  260. End If
  261. End Sub
  262. ''' <summary>
  263. ''' Animates the form automatically when it closes.
  264. ''' </summary>
  265. Private Sub Form_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _form.Closing
  266. If Not e.Cancel Then
  267. 'MDI child forms do not support transparency so do not try to use the Blend method.
  268. If Me._form.MdiParent Is Nothing OrElse Me._method <> AnimationMethod.Blend Then
  269. 'Hide the form.
  270. AnimateWindow(Me._form.Handle, _
  271. Me._duration, _
  272. AW_HIDE Or Me._method Or Me._direction)
  273. End If
  274. End If
  275. End Sub
  276. #End Region 'Event Handlers
  277. End Class