Feb 25, 2014

Animated Bitmap in C

Sometimes you will run across a Windows program that entertains you through an animated bitmap. Bed not the culprit this task accomplished? One solution should be to create a timer event that switches the bitmap every second or two, thus making the bitmap “appear” to become animated. In fact, it's not necessarily animated, but instead several versions of the same bitmap are switched fast enough to make it appear as if the bitmap is moving.

The first task is always to insert this code into your WinMain() function:
SetTimer(hwnd, 1, 1000, NULL);
This code creates a timer event that will be invoked every 1000 clock ticks (1 second). Inside your event (message) loop, after that you can 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);

In this way, when using the boolean flag bWingsUp, you'll be able to detect whether the butterfly bitmap’s wings are up or down. If they are up, you display the bitmap while using wings down. When they are down, you display the bitmap while using wings up. This system provides the illusion how the butterfly is flying.

No comments:

Post a Comment

Thank you for your valuable comment. Stay tuned for Updates.