Animation

 

Overview

An animation is a series of pictures put together to produce a video clip. It can be used to display the evolution of an ongoing task to the user. This makes such tasks less boring. For example, making a copy of a CD is usually a long process that can take minutes. To let the user know when such a task is being performed, you can display an animation.

Microsoft Windows provides a few small animations you can use for your applications. These animations, just like many other resources of Visual Studio or Visual C++, are not installed by default. During setup, you can install them if necessary. If you forgot to install them and you need them, run Setup again and select the Add/Remove button. Then click the check box of the Options checked list box. If you want only the videos or some resources, click the Graphics items and click the Change Options, and select the items you want. You can then click Continue to install the videos.

If you need an animation other than those supplied, you may have to create it. Visual C++ is not the place to create an animation. You may need a graphics software to do this.

To use a regular animation, the video must be a standard Microsoft Windows audio/video format: Audio Video Interleaved or AVI. Therefore, it must be a file with the avi extension. If the file has both audio and video, only the video part would be considered.

Animation Control and Properties

An animation first originates from an avi file created by an external application. Therefore, you must already have the video you want to play in your application. To provide an animation for your application, at design time, from the Controls toolbox, click the Animate button and click the desired area on the host.

To represent the frame of animation on the dialog box or form, the control draws a rectangle. Normally, the animation would be played inside of the area. The person who created the animation likely did not use the same rectangular dimensions when creating the video. Consequently, when it is asked to play, the animation’s upper-left corner would be set to correspond to your rectangle’s upper-left corner. If you want the animation to be centered in your assigned rectangle, set the control’s Center property to True. This is equivalent to adding the ACS_CENTER. In this case, the center of the video woud match the center of your rectangle, even though the animation may still not exactly match the dimensions of your rectangle.

When playing the video, you have the option of displaying the original background color of the video or seeing through. When creating a video, its author can do it with transparency to allow seeing the color of the host. In this case, to display the color of the host while the video is playing, set the Transparent property to True. If you are creating the control programmatically, add the ACS_TRANSPARENT style.

If you want the video to start displaying immediately once its host (the dialog box or form) comes up, set its Auto Play property to True. If you are dynamically creating the control and you want its video to play as soon as its parent comes up, add the ACS_AUTOPLAY style.

Animation Methods

The Animator control is based on the CAnimatorCtrl class. Therefore, if you want to programmatically create an animation, you must first declare a variable of type, or a pointer to, CAnimationCtrl. You can do this in the view or the dialog class. After declaring the variable or pointer, to initialize the object, call its Create() method. Here is an example:

 

class CControlsDlg : public CDialog
{
// Construction
public:
    CControlsDlg(CWnd* pParent = NULL); // standard constructor
    ~CControlsDlg();
    . . .
private:
    CAnimateCtrl *Player;
};
CControlsDlg::CControlsDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CControlsDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CControlsDlg)
        // NOTE: the ClassWizard will add member initialization here 
    //}}AFX_DATA_INIT
    
    Player = new CAnimateCtrl;
}

CControlsDlg::~CControlsDlg()
{
    delete Player;
}
. . .
BOOL CControlsDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    
    // TODO: Add extra initialization here
    RECT Recto = { 5, 5, 360, 360 };
    
    Player->Create(WS_CHILD | WS_VISIBLE |
                      ACS_TRANSPARENT | ACS_AUTOPLAY,
                      Recto, this, 0x1884);
              
    return TRUE; // return TRUE unless you set the focus to a control
                 // EXCEPTION: OCX Property Pages should return FALSE
}

After creating the control, you must provide a video to play. This is done by opening a video file using the CAnimateCtrl::Open() method. It is overloaded with two versions as follows:

BOOL Open(LPCTSTR lpszFileName);
BOOL Open(UINT nID);

The first version expects the path of the video file. Alternatively, you can first add the file as a resource to your project and use its identifier as argument to the second version. Here is an example:

BOOL CControlsDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here
	RECT Recto = { 5, 5, 360, 360 };

	Player->Create(WS_CHILD | WS_VISIBLE | ACS_TRANSPARENT,
	Recto, this, 0x1884);
	Player->Open("C:\\Program Files\\Microsoft Visual Studio
	                              \\Common\\Graphics\\Videos\\FileDel.AVI");

	return TRUE; // return TRUE unless you set the focus to a control
		    // EXCEPTION: OCX Property Pages should return FALSE
}

As mentioned already, an animation is made of various pictures. Each picture is called a frame. The number of frames that make up an animation can influence its length. A video to play as animation is a file that puts these pictures together. Once you have the video, you can play it in an animation. If you want the animation to start playing as soon as its parent window comes up, you can create it with the ACS_AUTOPLAY style. Otherwise, to play the animation, you can call the CAnimateCtrl::Play() method. Its syntax is:

BOOL Play(UINT nFrom, UINT nTo, UINT nRep);
  • The nFrom argument specifies the index number of the first frame to play. The frames are stored as a zero-based array. The first frame is 0, the second is 1, etc

  • The nTo argument is the last frame to play from the list of frames. If you want to play the video to the end, pass this argument with a value of -1

  • The nRep is the number of times the video should be played before stopping. If you want the video to play until you explicitly decide to stop it, pass this argument as -1

Here is an example of using the Play() method:

void CAnimation1Dlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_Animator.Play(0, -1, -1);

	CDialog::OnLButtonDblClk(nFlags, point);
}

Suppose you have a long video or one made of various special pictures, if you want to display just one particular frame of the video, you can call the CAnimateCtrl::Seek() method whose syntax is:

BOOL Seek(UINT nTo);

This method allows the animation to just straight to the nTo frame number.

At any time, you can stop the video playing by calling the CAnimateCtrl::Stop() method. Its syntax is:

BOOL Stop();

This member function simply stops the animation. Here is an example that stops an animation when the user right-clicks its parent dialog box:

void CAnimation1Dlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_Animator.Stop();

	CDialog::OnRButtonDown(nFlags, point);
}

If you had added your Animator control at design time to the dialog box or form or other parent window, when the parent goes out of scope, it takes the Animator control with it. If you dynamically created the control, you should make sure that the control is destroyed when its parent also is. This is usually taken care of with the delete operator as done above. Furthermore, when it comes to the Animator control, after using it, to free the memory space it space, you should call the CAnimateCtrl::Close() method. Its syntax is:

BOOL Close();

Here is an example of closing the animation when its parent dialog box gets closed:

void CAnimation1Dlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_Animator.Stop();
	m_Animator.Close();

	CDialog::OnClose();
}

 


Copyright © 2003-2005 FunctionX, Inc.