Rubrik: Oberfläche · Fenster | VB-Versionen: VB2005, VB2008, VB2010 | 20.03.12 |
![]() Eine Klasse und ihre Anwendung für das Animieren von Fenstern beim öffnen und schließen. | ||
Autor: ![]() | Bewertung: ![]() ![]() ![]() ![]() ![]() | Views: 9.359 |
ohne Homepage | System: WinXP, Vista, Win7, Win8, Win10 | ![]() |
Auf MSDN findet man die Beschreibung der Animationsmöglichkeiten von Fenstern. Die folgende Klasse stellt diese Möglichkeiten ins VB-Umfeld so zu sagen.
Imports System.Windows.Forms Imports System.Drawing, System.Runtime.InteropServices Imports System.Security, System.Security.Permissions Namespace AnimatedWindow Friend NotInheritable Class AnimationMethods Private Const AW_ACTIVATE = &H20000 Private Const AW_BLEND = &H80000 Private Const AW_CENTER = &H10 Private Const AW_SLIDE = &H40000 Private Const AW_HIDE = &H10000 Private Const AW_HOR_POSITIVE = &H1 Private Const AW_HOR_NEGATIVE = &H2 Private Const AW_VER_POSITIVE = &H4 Private Const AW_VER_NEGATIVE = &H8 Private Const WM_PAINT = &HF <Flags()> _ Public Enum AnimationFlags As Integer ' Activates the window. Activate = AW_ACTIVATE ' Uses a fade effect. This flag can be used ' only with a top-level window. Blend = AW_BLEND ' Makes the window appear to collapse inward if Hide ' is used or expand outward if the Hide is not used. Center = AW_CENTER ' Hides the window. By default, the window is shown. Hide = AW_HIDE ' Animates the window from left to right. This flag ' can be used with roll or slide animation. HorizontalPositive = AW_HOR_POSITIVE ' Animates the window from right to left. This flag ' can be used with roll or slide animation. HorizontalNegative = AW_HOR_NEGATIVE ' Uses a slide animation. By default, roll animation is used. Slide = AW_SLIDE ' Uses a roll animation. Roll = &H0 ' Animates the window from top to bottom. This flag ' can be used with roll or slide animation. VerticalPositive = AW_VER_POSITIVE ' Animates the window from bottom to top. This flag ' can be used with roll or slide animation. VerticalNegative = AW_VER_NEGATIVE End Enum <SuppressUnmanagedCodeSecurity()> _ <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function AnimateWindow(ByVal windowHandle As HandleRef, _ ByVal time As Integer, ByVal flags As AnimationFlags) As Integer End Function Friend Shared Sub AnimateWindow(ByVal control As Control, _ ByVal time As Integer, ByVal flags As AnimationFlags) Try Dim sp As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode) sp.Demand() AnimateWindow(New HandleRef(control, control.Handle), time, flags) Catch generatedExceptionName As SecurityException End Try End Sub End Class End Namespace
Am besten wendet man die Animation im VisibleChanged-Ereignis der Form an.
Private Sub myForm_VisibleChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.VisibleChanged If Me.Visible Then AnimatedWindow.AnimationMethods.AnimateWindow(Me, 200, _ AnimatedWindow.AnimationMethods.AnimationFlags.Activate Or _ AnimatedWindow.AnimationMethods.AnimationFlags.VerticalPositive Or _ AnimatedWindow.AnimationMethods.AnimationFlags.HorizontalPositive) Else AnimatedWindow.AnimationMethods.AnimateWindow(Me, 200, _ AnimatedWindow.AnimationMethods.AnimationFlags.Hide Or _ AnimatedWindow.AnimationMethods.AnimationFlags.VerticalNegative Or _ AnimatedWindow.AnimationMethods.AnimationFlags.HorizontalNegative) End If End Sub
Die Form wird in diesem Falle bei 'myForm.Show' von links-oben nach rechts-unten aufgemacht und beim Schließen von rechts-unten nach links-oben ausgeblendet.
Die Flags können also auch kombiniert werden; die Temposteuerung erfolgt über 'time' bspw. mit 200 ms.