Animated Bitmap Button using DIBs


“Up”, “Focused”, and “Down” bitmap                           

Disabled bitmap.

with five frames of animation.


Here is an actual example of an american flag button.  Notice

that the first row is the “Up” state.  The Second row is the Focused 

state.  It is exactly like the top row, except it has a red border

around the flag to show that it has the Input Focus.  The third row

is the “Sown” state.  It is exactly like the middle row, except the

shadow is removed and the flag is moved down and to the right a little

to give the impression that the button has been pressed…


How To Use:

Using the CAniButton class is simple.  First you will need to

add the CAniButton.h (cpp) and the CDib.h (cpp) files to your project. 

You might want to rebuild your class wizard database so that it knows about

the new classes in your project.  Next, add an owner drawn button

to a dialog.  Using class wizard, add a member variable for the button

using the CAniButton class as the variable type.  In the dialog’s

OnInitDialog method, add a call to the button’s AutoLoad method, and the

button will be created!  You will also want to handle the ON_WM_PALETTECHANGED

and ON_WM_QUERYNEWPALETTE messages inside of the dialog to do some palette

work.  Take a look at the example application’s code to see how this

is done.

BOOL CBtnTestDlg::OnInitDialog()
    // Initialize all of our CAniButtons before we 
    // do anything else! 
    m_btnGlobe.AutoLoad(IDC_GLOBE2,         // Resource ID 
                        this,               // Parent Window 
                        IDB_GLOBE_BUTTON,   // Main Bitmap Resource ID 
                        IDB_GLOBE_DISABLED, // Disabled Bitmap Resource ID 
                        10,                 // 10 Frames per Second 
                        0,                  // Calculate Number of Frames 
                        FALSE,              // Do NOT Stretch to fit 
                        TRUE,               // Replace Face Color 
                        IDC_PLANE_CURSOR);  // Cursor Resourse ID 


    // Set the icon for this dialog.  The framework does this automatically 
    //  when the application's main window is not a dialog 
    SetIcon(m_hIcon, TRUE);   // Set big icon 
    SetIcon(m_hIcon, FALSE);  // Set small icon 

    return TRUE;  // return TRUE  unless you set the focus to a control 

void CBtnTestDlg::OnPaletteChanged(CWnd* pFocusWnd)
    // If this window is not the window getting focus, 
    // redraw the AniButtons... 
    if(pFocusWnd != this)
        // A simple call to Invalidate will cause the 
        // button to be redrawn and it's palette realized. 

BOOL CBtnTestDlg::OnQueryNewPalette()
    // Make sure the AniButton's Palette 
    // becomes the foreground palette. 

    return CDialog::OnQueryNewPalette();

CAniButton Creation Syntax:


    BOOL CAniButton::AutoLoad(UINT  nID,


CWnd* pParent,


UINT  nBitmapID,


UINT  nDisabledID,


UINT  nFramesPerSecond,


UINT  nNumFrames = 0,


BOOL  bStretchToFit = FALSE,


BOOL  bChangeFaceColor = TRUE,


UINT  nCursorID = 0);

    BOOL CAniButton::AutoLoad(UINT  nID,


CWnd* pParent,


const char* szFilename,


const char* szDisabledFilename,


UINT  nFramesPerSecond,


UINT  nNumFrames,


BOOL  bStretchToFit,


BOOL  bChangeFaceColor,


UINT  nCursorID);

    Return Value

    Nonzero if successful; otherwise 0.


    nID   The button’s control ID.

    pParent   Pointer to the object

that owns the button.

    nBitmapID   The resource

ID of the buttons main bitmap for the “Up”, “Down”, and


“Focused” states.  Required.

    nDisabledID   The resource

ID of the button’s bitmap for the “Disabled” state.

    szFilename   The filename

of the buttons main bitmap for the “Up”, “Down”, and


“Focussed” states.  Required.

    szDisabledFilename   The

filename of the button’s bitmap for the “Disabled” state.

    nFramesPerSecond   The number

of animation frames to cycle through each second.

    nNumFrames   The total number

of frames that are in the animation.  If this argument


is set to 0, the number of frames will be determined by comparing


the width of the Disabled Bitmap with the width of the Main Bitmap.


Example: If the disabled bitmap is 50 pixels wide and the main bitmap


is 250 pixels wide,  250 / 50 = 5 frames of animation…

    bStretchToFit   If this flag

is set to TRUE, the bitmap will be stretched to fit the size


of the button.  A setting of FALSE will center the bitmap on the button.

    bChangeFaceColor   If this

flag is set to TRUE, all pixels with the same color as the


first pixel in the bitmap will be changed to match the windows


3-D face color.  This makes the button look transparent.  A


setting of FALSE will not modify any of the pixels.

    nCursorID   The resource

ID of the cursor to be displayed when the cursor is moved


over the button itself.  A setting of 0 means the cursor will not


Download demo project – 176 KB

Download source – 186 KB

More by Author

Previous articleEnumerate NT Services
Next articleGradient fill

Must Read