Older Version Newer Version

JanetTerra JanetTerra Jan 14, 2008

Opening and Closing Windows with Animation


The AnimateWindow function allows four types of special effects when showing or hiding windows: roll, slide, expand, and fade. The call is made to #user32 , passing the handle of the window ( handle ), the duration of animation in milliseconds ( dwTime ), and the desired effects ( dwFlags ).

 Function AnimateWindow(handle, dwTime, dwFlags) 
CallDLL #user32, "AnimateWindow", _
handle as uLong, _
dwTime as Long, _
dwFlags as Long, _
result as Boolean
End Function
The functions returns a zero if the function fails, or a non-zero if the function is successful.

Specifying the Animation Types

AW_SLIDE Uses slide animation, the default is AW_ROLL
AW_HOR_POSITIVE Animates the window from left to right, used with AW_SLIDE , or the default AW_ROLL
AW_HOR_NEGATIVE Animates the window from right to left, used with AW_SLIDE , or the default AW_ROLL
AW_VER_POSITIVE Animates the window from top to bottom, used with AW_SLIDE , or the default AW_ROLL
AW_VER_NEGATIVE Animates the window from bottom to top, used with AW_SLIDE , or the default AW_ROLL
AW_CENTER Expands the window from centerpoint outward when opening, negates any other direction flags
AW_BLEND Uses a fade in effect when opening, or a fade out effect when closing, ignored if AW_CENTER is also assigned
AW_ACTIVATE Activates the window, should not be used with AW_HIDE
AW_HIDE Hides the window, the default is a visible window

Defining the Animation Values

Liberty BASIC recognizes many of the Windows constants. These animated window constants are not recognized. The values will need to be defined within the code.
 AW.SLIDE = HexDec("&H40000") 
AW.HOR.POSITIVE = HexDec("&H1")
AW.HOR.NEGATIVE = HexDec("&H2")
AW.VER.POSITIVE = HexDec("&H4")
AW.VER.NEGATIVE = HexDec("&H8")
AW.CENTER = HexDec("&H10")
AW.BLEND = HexDec("&H80000")
AW.ACTIVATE = HexDec("&20000")
AW.HIDE = HexDec("&H10000")
If these values need to be seen within several subs or functions, it may be best to declare them Global in the beginning of your program.
 Global AW.SLIDE 
Global AW.HOR.POSITIVE, AW.HOR.NEGATIVE
Global AW.VER.POSITIVE, AW.VER.NEGATIVE
Global AW.CENTER
Global AW.ACTIVATE, AW.HIDE
Global AW.BLEND

Showing and Hiding Windows

To show a window opening using an animation, that window must first be in AW_HIDE mode. Assign the stylebits _WS_VISIBLE in the removebits position before opening the window.
 Stylebits #a, 0, _WS_VISIBLE, 0, 0 

The Demo

The following demo uses various combinations of the AW values. Some effects are more pleasing than others. Not all combinations work with all types of windows. MSDN suggests 200 milliseconds as the value for dwTime . This demo uses a greater dwTime value for illustrative purposes. Adjust the value according to the needs of your program.
 
'Nomainwin
WindowWidth = 500
WindowHeight = 400
UpperLeftX = Int((DisplayWidth - WindowWidth) /2)
UpperLeftY = Int((DisplayHeight - WindowHeight) /2)
Button #demo.b1, " Horizontal L -> R ", AnimWindowOpen, UL, 100, 80, 120, 30
Button #demo.b2, " Horizontal R -> L ", AnimWindowOpen, UL, 100, 120, 120, 30
Button #demo.b3, " Vertical T -> B ", AnimWindowOpen, UL, 100, 160, 120, 30
Button #demo.b4, " Vertical B -> T ", AnimWindowOpen, UL, 100, 200, 120, 30
Button #demo.b5, " Diagonal L -> R", AnimWindowOpen, UL, 280, 80, 120, 30
Button #demo.b6, " Diagonal R -> L ", AnimWindowOpen, UL, 280, 120, 120, 30
Button #demo.b7, " Expand from Center ", AnimWindowOpen, UL, 280, 160, 120, 30
Button #demo.b8, " Fade In ", AnimWindowOpen, UL, 280, 200, 120, 30
Open "Animated Window" for Window as #demo
#demo, "Trapclose XbyTrap"

' Undefined Windows Constants
Global AW.HOR.POSITIVE, AW.HOR.NEGATIVE
Global AW.VER.POSITIVE, AW.VER.NEGATIVE
Global AW.CENTER, AW.HIDE, AW.ACTIVATE
Global AW.SLIDE, AW.BLEND
AW.HOR.POSITIVE = HexDec("&H1")
AW.HOR.NEGATIVE = HexDec("&H2")
AW.VER.POSITIVE = HexDec("&H4")
AW.VER.NEGATIVE = HexDec("&H8")
AW.CENTER = HexDec("&H10")
AW.HIDE = HexDec("&H10000")
AW.ACTIVATE = HexDec("&20000")
AW.SLIDE = HexDec("&H40000")
AW.BLEND = HexDec("&H80000")

Wait

Sub XbyTrap handle$
Close #demo
End
End Sub

Sub AnimWindowOpen handle$
Select Case Right$(handle$, 1)
Case "1"
dwFlags = AW.HOR.POSITIVE or AW.SLIDE
Button #a.1, "Close", AnimWindowClose, UL, 150, 100
title$ = "Horizontal Slide Left to Right"
Case "2"
dwFlags = AW.HOR.NEGATIVE or AW.SLIDE
Button #a.2, "Close", AnimWindowClose, UL, 150, 100
title$ = "Horizontal Slide Right to Left"
Case "3"
dwFlags = AW.VER.POSITIVE or AW.SLIDE
Button #a.3, "Close", AnimWindowClose, UL, 150, 100
title$ = "Vertical Slide Top to Bottom"
Case "4"
dwFlags = AW.VER.NEGATIVE or AW.SLIDE
Button #a.4, "Close", AnimWindowClose, UL, 150, 100
title$ = "Vertical Slide Bottom to Top"
Case "5"
dwFlags = AW.HOR.POSITIVE or AW.VER.NEGATIVE or AW.SLIDE
Button #a.5, "Close", AnimWindowClose, UL, 150, 100
title$ = "Diagonal Left to Right"
Case "6"
dwFlags = AW.HOR.NEGATIVE or AW.VER.POSITIVE or AW.SLIDE
Button #a.6, "Close", AnimWindowClose, UL, 150, 100
title$ = "Diagonal Right to Left"
Case "7"
dwFlags = AW.CENTER or AW.ACTIVATE
Button #a.7, "Close", AnimWindowClose, UL, 150, 100
title$ = "Expand from Center Outward"
Case "8"
dwFlags = AW.BLEND
Button #a.8, "Close", AnimWindowClose, UL, 150, 100
title$ = "Vertical Slide Bottom to Top"
End Select
WindowWidth = 400
WindowHeight = 300
UpperLeftX = 50
UpperLeftY = 50
BackgroundColor$ = "Darkpink"
Stylebits #a, 0, _WS_VISIBLE, 0, 0
Open title$ for Dialog_Modal as #a
#a, "Trapclose NoAnimWindowClose"
null = AnimateWindow(hWnd(#a), 1000, dwFlags)
End Sub

Sub AnimWindowClose handle$
Select Case Right$(handle$, 1)
Case "1"
dwFlags = AW.HOR.NEGATIVE or AW.SLIDE or AW.HIDE
Case "2"
dwFlags = AW.HOR.POSITIVE or AW.SLIDE or AW.HIDE
Case "3"
dwFlags = AW.VER.NEGATIVE or AW.SLIDE or AW.HIDE
Case "4"
dwFlags = AW.VER.POSITIVE or AW.SLIDE or AW.HIDE
Case "5"
dwFlags = AW.HOR.NEGATIVE or AW.VER.POSITIVE or AW.SLIDE or AW.HIDE
Case "6"
dwFlags = AW.HOR.POSITIVE or AW.VER.NEGATIVE or AW.SLIDE or AW.HIDE
Case "7"
dwFlags = AW.CENTER or AW.HIDE ' No effect, unsure why
Case "8"
dwFlags = AW.BLEND or AW.HIDE
End Select
null = AnimateWindow(hWnd(#a), 500, dwFlags)
Close #a
End Sub

Sub NoAnimWindowClose handle$
Close #handle$
End Sub

Function AnimateWindow(handle, dwTime, dwFlags)
CallDLL #user32, "AnimateWindow", _
handle as uLong, _
dwTime as Long, _
dwFlags as Long, _
result as Boolean
End Function

Demo Limitations

Animation while opening the window is easy to achieve. Just be sure the window is in the Hide state first. It hasn't been easy to find the code to close the window using animation. MSDN states
AW_BLEND worked well. I was unable to find the correct combination that would show a window closing in either AW_CENTER or AW_SLIDE animation.