Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Animated Child Window Movement

0.00/5 (No votes)
8 Oct 2009 1  
A simple class to animate the movement of child windows

Introduction

ChildWndMover helps you to animate the movement child controls.

Background

I wrote this because I needed a way to tell users that a new version of one of our programs was available, but I didn't want to do a modal message box (because I find that annoying). Instead, when the program starts-up and detects that there's a new version available, I use this code to slide a bunch of controls to the side of the main window, then I insert a CHTMLCtrl window on the dialog in their place and show a little web page describing the upgrade. After a few seconds, I remove the CHTMLCtrl and the rest of the controls slide back to their original position. I find this to be much less obtrusive than a modal dialog; there's nothing to click, all the original controls still work - they've just moved a few pixels to the left, for a couple of seconds. And yes, this notification can be disabled.

The code in this article describes what I use to move the controls.

Using the Code

Here's the basic usage:

// if it's not already moving...
if (!ChildWndMover::Moving(myControl.GetSafeHwnd()))              
{
   // ... move it!
   ChildWndMover::AddMover(
          parentWnd.GetSafeHwnd(),   // typically the parent dialog
          m_control.GetSafeHwnd(),   // HWND of the control you want to move
          -50,                       // move to the left 50 pixels
          0,                         // don't move vertically
          10,                        // grow the width by 10
          -20,                       // reduce the height by 20
          .5,                        // duration of the move is .5 seconds (approximately)
          20,                        // do it in 20 steps
          true,                      // tell the window to redraw itself after each move
          false                      // don't hide at the end
          );
}

After that call, m_control will start moving to the left while changing its shape.

There are three public function calls:

  1. AddMover - Creates a mover object from your control information, starts a timer, and moves the control
  2. Moving(HWND) - Test to see if a specific control is currently being moved
  3. Moving(void) - Test to see if anything is moving. In the demo app, the OnOK handler checks this before starting new movers, to avoid interrupting any existing movers.

Everything else in the namespace is an internal utility - no need to call it directly.

Points of Interest

It's asynchronous! When you call AddMover, a timer is created on the window you're moving, and the control is moved in response to events from that timer. So, even though the AddMover function will return immediately, the window will start moving and continue to move, regardless of what the rest of your code is doing, as long as Windows can get WM_TIMER events through the message queue. Because this movement will occur outside the scope of your AddMover call, the map of control objects has to be global; global scope is the only scope big enough to handle this kind of asynchronous operation.

I put the code in a namespace rather than make it a static class, because I didn't see any reason to create even a dummy CChildWndMover object - all state is in the global map and the timers. There really is no object to maintain, so I saw no need to bother with the concept ... a namespace does the job just as well.

That's it. The code is very simple and using it is even simpler.

History

  • Oct 8th 2009: First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here