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 

    CDialog::OnInitDialog(); 

    // 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. 
        m_btnGlobe.Invalidate(); 
    } 
} 

BOOL CBtnTestDlg::OnQueryNewPalette() 
{ 
    // Make sure the AniButton's Palette 
    // becomes the foreground palette. 
    m_btnGlobe.RealizePalette(); 
  
    return CDialog::OnQueryNewPalette(); 
} 

CAniButton Creation Syntax:

CAniButton::AutoLoad

    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.

    Parameters

    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 change.

Download demo project - 176 KB

Download source - 186 KB



Comments

  • Assertion ! SubclassDlgItem inside CAniButton

    Posted by Legacy on 12/23/2000 12:00am

    Originally posted by: Ashwin

    CWnd::SubclassDlgItem(UINT nID, CWnd* pParent)
    if (hWndControl != NULL)
    return SubclassWindow(hWndControl);Ooops! Assertion
    Why ??..
    Someone help me out please

    Reply
  • error in Offset

    Posted by Legacy on 05/24/2000 12:00am

    Originally posted by: lesant

    Since bitmap starts to 0, the x offset is:
    int nXFrameOffset = m_nCurFrame * m_nFrameWidth ;

    AND NOT
    int nXFrameOffset = m_nCurFrame * m_nFrameWidth +1;

    HTH :)

    Reply
  • CDib class in a ControlPanel applet (DLL) don't mix

    Posted by Legacy on 04/13/1999 12:00am

    Originally posted by: Gregory Franz

    I have an application as a ControlPanel applet (DLL) that uses tabbed property pages. Everything builds well
    with the animated button classes, but when the RunDLL32.exe that runs the applet mysteriously collides with
    CDib class. This is seen only when you set the button resource to "owner draw". There appears to be
    a problem with the drawing of CDib and the RunDLL32.exe
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds