Window Animation

The Pitch

Ever get bored minimizing, maximizing, and restoring windows? Ever wanted to catch the user unaware and make him notice an app that's just that little bit special? Ever wanted to make a stealth program that literally slides into the background? Well, with the CWindowAnima class you can now animate the appearance and disappearance of windows in ways that are a little different from the norm. There are twelve effects in total, six "vanishing" effects and six opposite "appearing" effects. They include Blind, Melt, Wipe, Scatter4, Scatter6, SlideVanish, UnBlind, UnMelt, UnWipe, ReAssemble4, ReAssemble6, and SlideAppear.

What's more, these effects can be done on buttons, dialogs, and anything else that's a CWnd derivative. The sample project (seen above) demonstrates all twelve effects on a simple dialog box. The project also includes an exploding button (using the Scatter6 effect) but I didn't include the screen shot; you'll have to download the project to see it!

CWindowAnima works by capturing images of both the screen and the window and animating the image of the window. There is a comprehensive list of functions contained within the class and several flags and options that need explaining; let's examine a few examples to get us going.

The following code is pulled straight from the example project but I'll reproduce it here nonetheless. Further down this page you'll find a few notes -- the parameters to the various effects (because where's the fun if you can't tweak it a little to suit your needs?) -- and a summary of the public functions. If you manage to get past all of that, you're rewarded with a link to the project files and source code.

Example #1 -- Melting and UnMelting

// set the window for animation and specify that it should
// not draw over the task bar
CWindowAnima wa(this, WA_KEEP_TASKBAR_ONTOP);
// melt the window to the left side of the screen,
// using 6 segments and stagger each segment 80 pixels
wa.Melt(WA_LEFT,6,80);
// wait 3 secs
Sleep(3000);
// move the window to a new position
wa.MoveWindowPosition(600,400);
// Unmelt the window using the default (WA_AUTO), which unmelts
// the window from the side of the screen closest to the
// top-left corner of the window
wa.UnMelt();

Example #2 -- SlideVanish and SlideAppear

// create the animation object but specify that
// it should not draw over the task bar
CWindowAnima WA(this,WA_KEEP_TASKBAR_ONTOP);
// slide the window out of view
wa.SlideVanish();
// sleep for three seconds
Sleep(3000);
// skip window capture and do not restore window nor image behind the
// window when a "restoring" animation is performed
wa.FlagSet(WA_WND_SKIPCAPTURE|WA_WND_SKIPRESTORE);
// replace the window with the image whose resource ID is "IDB_HOUSE"
wa.ReplaceWindowImage(IDB_HOUSE);
// slide our window into view, except now it's the image IDB_HOUSE.
// Also, don't restore the window until we're specifically told to.
wa.SlideAppear();
// wait 2 secs
Sleep(2000);
// called like this, Restores the window irrespective of the
// WA_WND_SKIPRESTORE flag.
wa.RestoreWindow();

Example #3 -- Blind and UnBlind

// Create the object using the default constructor
CWindowAnima WA;
// sets the window for animation to be this dialog window
wa.SetWindow(this);
// Blind the window into invisibility using horizontal blinds
wa.Blind(WA_HORZ);
// Sleep for a second
Sleep(1000);
// Blind the window into visibility using the default
// vertical blinds
wa.UnBlind();

Author's Notes

I have just a few things to say about this class. First of all, while it should (gotta cover myself for any eventualities) work on any CWnd derivative, it's probably best only to animate relatively small dialogs and buttons or at least give the user the opportunity of animating the window or not. Basically, large windows will crawl sluggishly around your screen and the user will get bored and then annoyed.

Secondly, if you find that the window sometimes gets smeared across the screen, such as when the old image during an animation isn't overwritten, look at the HideWindow() function; it may help.

Thirdly, if anyone could provide some code to do some fast image rotation or, even better, a new effect or two, I'd appreciate it.

So, that's pretty much it....enjoy!

Effects Function Parameters

Animation
Effect

Function
Parameters

Blind() & UnBlind

Parameter Name

Default Value

nDirection

WA_VERT

nSegments

8

nGapFactor

4

nSleeptime

100

Melt & UnMelt

Parameter Name

Default Value

nDirection

WA_AUTO

nSegments

6

nStagger

40

nSleeptime

1

Wipe & UnWipe

Parameter Name

Default Value

nDirection

WA_RAND

nWipeFactor

4

nSleeptime

20

Scatter4 & ReAssemble4

Scatter6 & ReAssemble6

Parameter Name

Default Value

nDistance

90

nMoveFactor

4

nSleeptime

1

SlideVanish & SlideAppear

Parameter Name

Default Value

nSlideFactor

1

nScaleFactor

15

nSlideDelay

10

nVanishDelay

0

Summary of Public Functions

CWindowAnima()

There are two constructors for CWindowAnima objects. The default constructor takes no arguments and any object created this way needs to call SetWindow() before any animations are done. The second constructor takes a pointer to a CWnd object or derivative as its first parameter and any flags as its second parameter. Internally the constructor calls FlagSet() and SetWindow() in that order.

FlagSet()

Sets the flags member variable with the nFlags parameter. Returns the current state of the flags.

FlagUnset()

Unsets the flag's member variable with the nFlags parameter. Returns the current state of the flags.

FlagIsSet()

Returns either TRUE or FALSE, depending on whether or not a flag or set of flags is set.

CopyWindowCapture()

Grabs a copy of the window that another CWindowAnima object has gone to all the trouble of capturing. Also makes sure (in debug mode) that some idiot hasn't passed us a pointer to a CWindowAnima object that hasn't got a window image.

CopyScreenCapture()

Grabs a copy of the screen that another CWindowAnima object has gone to all the trouble of capturing. Also makes sure (in debug mode) that some idiot hasn't passed us a pointer to a CWindowAnima object that hasn't got a screen image.

ReplaceWindowImage()

Replaces a window's image and size using an image resource ID. Useful if you wanted to animate the initial appearance of a window whose image has not been captured before. This function is overloaded and the user may also choose to replace a window's image and size with an image contained in a CDC object.

SetClippingRegion()

Sets the clipping region rectangle for the animation and updates the screen dimensions. Takes a CRect object as an input parameter or four integers describing a bounding box.

GetClippingRegion()

Returns the current Clipping Region and the classes' idea of how big the screen is. Clipping region and screen size are set to the same values internally.

SetWindow()

Sets the window to be animated and captures an image of the window. Asserts (in debug mode) if the window given as a parameter is NULL.

GetWindow()

Returns a pointer to the window that was stored when the object was initialized in the constructor or when the SetWindow function was called.

GetWindowRect()

Copies the window's bounding rectangle into the empty CRect structure passed to it as a parameter.

MoveWindowPosition()

Repositions the window being animated to the new coordinates specified by x and y integers and updates any internal variables that keep track of window position.

Restores the window and the proper background of the window to their proper location. Internally, RestoreWindow() is called with its parameter set to FALSE so it will check for the WA_KEEP_WINDOW_HIDDEN, WA_KEEP_BKGRND_HIDDEN, or WA_WND_SKIPRESTORE flags (see the flags section for more information on what these flags do). Calling RestoreWindow() externally with no parameters causes the function to use the default value TRUE as its parameter and therefore ignore the value of any of the flags. It is assumed that the only reason a user would call this function explicitly is to restore a window irrespective of what the flags are set to.

Animation Functions

Blind()

Creates a blinds disappearing effect on the window. There are two types of effect: vertical blinds or horizontal blinds. Use the WA_VERT or WA_HORZ direction values to choose the desired effect.

UnBlind()

Creates a blinds appearing effect on the window. This is similar to the "Blinding" effect described above and the possible direction values are identical.

UnWipes the window into existence. The type of Unwipe animation depends of the direction value given. Valid directions are the same as those in the "Wiping" animation above.

Melt()

Melts the window off the screen. The direction the window will melt towards is dependent on the direction value given. Valid directions for this animation are WA_LEFT, WA_RIGHT, WA_UP, WA_DOWN, WA_RAND, WA_AUTO (default).

UnMelt()

UnMelts the window onto the screen. The screen edge that the window will unmelt from is dependent on the direction value given. Valid directions are the same as those for the "Melting" animation above.

Scatter4()

Splits the window into four separate rectangles and sends each part flying away from the window's center point

ReAssemble4()

Four separate parts of a window fly from different directions to a central point, where they join to make a full window.

Scatter6()

Splits the window into six separate rectangles and sends each part flying away from the window's center point

ReAssemble6()

Six separate parts of a window fly from different directions to a central point, where they join to make a full window.

SlideVanish()

Two partitions slide horizontally away from each other underneath the window. The window then disappears into the black void. Finally, the partitions close back together, leaving the screen intact minus the window.

SlideAppear()

Two partitions slide horizontally away from each other, opening into a black void. Zooming out of the void, the window appears. The partitions then close, sliding back together again behind the now fully visible window.

Flags

WA_SCR_SKIPCAPTURE

Each time a CWindowAnima object performs an animation, it captures the area of the screen inside the clipping region. By setting this flag, the animation will not capture the screen when it executes. You might use this if you've copied the screen image from another CWindowAnima object; otherwise, the window will disappear and appear in a very short space of time and the screen appearance is unlikely to change in the meantime.

WA_WND_SKIPCAPTURE

When a CWindowAnima object is initialized, it tries to capture an image of the window being animated. By setting this flag, CWindowAnima will not capture the screen image. It's important to set this flag if the window you're going to animate is invisible when you initialize the CWindowAnima object or when you're copying the image of the window between two objects.

WA_WND_SKIPRESTORE

By setting this flag, the real window will not be restored to view after "appearing" effects such as UnBlind, UnMelt, SlideAppear, UnWipe, ReAssemble4, or ReAssemble6. This flag overrides both the WA_KEEP_WINDOW_HIDDEN and WA_KEEP_BKGRND_HIDDEN flags.

WA_KEEP_WINDOW_HIDDEN

Normally when an "appearing" effect has finished executing, the correct window background is printed onto the screen and the real window is then set to visible. By setting this flag, the correct window background is painted to the screen but the window is not made visible. A call to either RestoreWindow or ShowWindow is needed.

WA_KEEP_BKGRND_HIDDEN

Normally when an "appearing" effect has finished executing, the correct window background is printed onto the screen and the real window is then set to visible. By setting this flag, the window is made visible but the window background is never painted to the screen. This usually results in having an image of the window stuck behind the real window instead of the correct background. A call to RestoreWindow is needed to correct the problem.

WA_KEEP_TASKBAR_ONTOP

Setting this flag adjusts the clipping region to exclude the Windows Taskbar. When this flag is set, animations will seem to appear from and disappear under the Taskbar instead of the default, which is to animate over the Windows Taskbar.

WA_SKIPHIDEONCAPTURE

When an animation captures the screen, it calls the window's CWnd ShowWindow() function with the SW_HIDE parameter. Setting this flag ensures that the window is not hidden when a screen capture is performed.

Directions

WA_LEFT

WA_RIGHT

WA_UP

WA_DOWN

WA_VERT

WA_HORZ

WA_RAND

In the Melt, UnMelt, Wipe, and UnWipe effects, causes the animation to execute in a random direction.

WA_AUTO

In the Melt and UnMelt effects, causes the window to melt towards or unmelt from the side of the screen closest to the window's top-left corner.