Click here to Skip to main content
15,886,017 members
Articles / Desktop Programming / MFC

Animated Child Window Movement

Rate me:
Please Sign up or sign in to vote.
4.21/5 (7 votes)
8 Oct 2009CPOL2 min read 35.7K   2.3K   31   4
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:

C++
// 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)


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
QuestionGet's my 5 Pin
.dan.g.1-Dec-11 15:16
professional.dan.g.1-Dec-11 15:16 
GeneralGood Pin
billowqiu10-Oct-09 1:27
billowqiu10-Oct-09 1:27 
QuestionIs it really better than a popup window (or other less intrusive notification)? Pin
Maximilien9-Oct-09 3:27
Maximilien9-Oct-09 3:27 
AnswerRe: Is it really better than a popup window (or other less intrusive notification)? Pin
Chris Losinger9-Oct-09 4:38
professionalChris Losinger9-Oct-09 4:38 

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

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