Click here to Skip to main content
Click here to Skip to main content

Animated Child Window Movement

, 8 Oct 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

Comments and Discussions

 
QuestionGet's my 5 Pinmember.dan.g.1-Dec-11 16:16 
GeneralGood PinmemberToCpp10-Oct-09 2:27 
QuestionIs it really better than a popup window (or other less intrusive notification)? PinmemberMaximilien9-Oct-09 4:27 
AnswerRe: Is it really better than a popup window (or other less intrusive notification)? PinmemberChris Losinger9-Oct-09 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 8 Oct 2009
Article Copyright 2009 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid