Animation in a Dialog

Create The Images

First create each of the images to be shown in the animation as an icon in the resource workshop.

The Dialog Window

In this example the animation is shown in a dialog. First create the dialog in the resource editor and associate it with a class (called Movie here). Once you have created the class Movie using ClassExpert, add the following functions.

SetupWindow

The dialog will have one data member for each icon, a data member for the current icon, plus for the WM_PAINT command we will need a TBrush, memory DC (just like a TDC, except in memory not the screen) and a TBitmap. To avoid flicker when we are showing the icons, we create them in a TMemoryDC. After they are created, we use BitBlt to display them. BitBlt is quickly transfers bitmaps to the screen. All of these variables are initialized in the SetupWindow function. We also setup a time that will show the icons at a fixed interval.

void Movie::SetupWindow ()
{
    TDialog::SetupWindow();

    // INSERT>> Your code here.
    
    SetTimer(1,200);
    HINSTANCE instance = *GetApplication();
    icon1 = new TIcon(instance, ICON_1); //ICON_i is the identifier associated with ith icon
    icon2 = new TIcon(instance, ICON_2);
    icon3 = new TIcon(instance, ICON_3);
    icon4 = new TIcon(instance, ICON_4);
    curIcon = icon1;            
    _myBrush = new TBrush(TColor::LtGray);
    TClientDC *dc = new TClientDC(*this);  //create device context for the window
    _bitmap = new TBitmap(*dc,32,32);  	 //create a btimap for the entire screen
    _memDC = new TMemoryDC(*dc); 	 //crate compatible memory dc
    delete dc;

}

The Timer

The timer hander changes the icon based on the current icon. This way the icons are shown in a fixed order. Try changing the order in the program. Next the bitmap is selected by the memory DC and the icon is drawn in the memory DC. Finally the WM_PAINT message is sent to the window.

void Movie::EvTimer (uint timerId)
{
    TDialog::EvTimer(timerId);

    // INSERT>> Your code here.
    
    if (curIcon == icon1)
        curIcon = icon4;
    else if (curIcon == icon2)
        curIcon = icon1;
    else if (curIcon == icon3)
        curIcon = icon2;
    else
        curIcon = icon3;
    _memDC->SelectObject(*_bitmap);
    _memDC->DrawIcon(0,0,*curIcon);
    Invalidate(false);
}

The EvPaint Function

In the EvPaint command, the bitmap is displayed on the screen. Notice that the bitmap is in _memDC, that's why it is the source of the bitmap.

void Movie::EvPaint ()
{
    TDialog::EvPaint();

    // INSERT>> Your code here.

    TRect cRect = GetClientRect();
    TClientDC *tdc= new TClientDC(*this);
    tdc->BitBlt((cRect.Width()-32)/2,cRect.Height()/3,32,32,*_memDC,0,0,SRCCOPY);
    delete tdc;
}

The Rest of The Functions

The EvCltColor is identical to that used to color dialog boxes, and the CleanupWindow deletes all of the pointer members of the dialog class.

Complete Code

The complete code for this program can be retrieved by clicking here.