Monday 28 November 2011

How do you create an animated bitmap? in C programming

How do you create an animated bitmap?

Sometimes you will run across a Windows program that entertains you with an animated bitmap. How is this task accomplished? One method is to set up a timer event that switches the bitmap every second or two, thus making the bitmap “appear” to be animated. In fact, it is not animated, but rather several versions of the same bitmap are switched fast enough to make it appear as though the bitmap is moving.

The first step is to insert the following code into your WinMain() function:
SetTimer(hwnd, 1, 1000, NULL);

This code sets up a timer event that will be invoked every 1000 clock ticks (1 second). In your event (message)
loop, you can then trap the timer event, as shown here:
switch(message)
{
case WM_TIMER:
/* trapped timer event; perform something here */
}

Now, when the WM_CREATE message comes through, you can load the original bitmap:
case WM_CREATE:
hBitmap = LoadBitmap(hInstance, BMP_ButterflyWingsDown);
In this case, BMP_ButterflyWingsDown is a bitmap resource bound to the executable through the use of a
resource editor. Every time a WM_TIMER event is triggered, the following code is performed:
case WM_TIMER:
if (bWingsUp)
hBitmap = LoadBitmap(hInstance, BMP_ButterflyWingsDown);
else
hBitmap = LoadBitmap(hInstance, BMP_ButterflyWingsUp);

This way, by using the boolean flag bWingsUp, you can determine whether the butterfly bitmap’s wings are up or down. If they are up, you display the bitmap with the wings down. If they are down, you display the bitmap with the wings up. This technique gives the illusion that the butterfly is flying.

Cross Reference:

None.

No comments:

Post a Comment