Simple Bitmap Buttons

This is an easy and flexible way to use bitmaps as buttons in your application, however it has its limitations. It is based on CStatic class and its SetBitmap member function.

I did it this way:

  1. Step: Define button states (for instance: default, highlighted, pressed) and draw the bitmap for each state.
  2. Step: Derive a class from CStatic, let's call it CBmpButton.
  3. Step: Add CStatic controls to your dialog template and associate member variable to them. The type of the variables should be CBmpButton.
  4. Step: Add a CBitmap member variable to the CBmpButton class for each button state. Like this:
    protected:
      CBitmap m_default;     // normal state
      CBitmap m_highlighted; // the mouse is moving over it
      CBitmap m_pressed;     // the button is pressed
    
  5. Step: Add an initialization function to the class, which will load the bitmaps (from the resource or from file):
    BOOL CBmpButton::LoadButtonBitmaps( int idDefault, 
                                        int idHighlighted, 
                                        int idPressed)
    {
      BOOL retVal = TRUE;
    
      retVal &= m_bmpDefault.LoadBitmap(idDefault);
    
      if (idUp < 0) m_highlighted.LoadBitmap(idDefault);
      else retVal &= m_highlighted.LoadBitmap(idHighlighted);
    
      if (idDown < 0) m_pressed.LoadBitmap(idDefault);
      else retVal &= m_pressed.LoadBitmap(idPressed);
    
      return retVal;
    }
    
  6. Step: Catch windows messages you want to react to (WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_LBUTTONUP) and change the bitmap for each one:
    void CBmpButton::OnLButtonDown( UINT nFlags, 
                                         CPoint point)
    {
      // TODO: Add your message handler code here 
      //       and/or call default
      SetBitmap(HBITMAP(m_bmpDown));
    CStatic::OnLButtonDown(nFlags, point); } void CBmpButton::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here // and/or call default SetBitmap(HBITMAP(m_bmpUp)); CStatic::OnLButtonUp(nFlags, point); }

    etc.

  7. Step: I needed another function to reset the button - to restore the default state, after the mouse left the area ( SetBitmap(HBITMAP(m_bmpDefault)) ); This function is called from the parent dialog member function OnMouseMove

And that's all...

Problem: I didn't managed to persuade the bitmaps to clean themselves from the memory--only when the application exits. If you use this method on popup dialogs in a SDI or MDI application it can cause memory overflow.

In my demo project I used the code of David Gallardo Llopis, downloaded from CodeGuru to make a Bitmap Dialog.

Downloads

Download demo project - 47 Kb


Comments

  • free resources

    Posted by brigante on 04/30/2009 10:55am

    http://brigante.sytes.net/c++.aspx

    Reply
  • good

    Posted by eidolon on 12/14/2005 05:29am

    good

    Reply
  • one problem

    Posted by Legacy on 11/06/2002 12:00am

    Originally posted by: Kevin Smith

    This is really great, thankyou.
    I am having a minor problem.

    I have integrated your class into my program. But when I left-click on one of my buttons, I cannot hold the button down... as soon as I left-click the button, it performs the BN_CLICKED for the button.

    This should be happening when I release the left-click, what am I doing wrong?

    Reply
  • error!!

    Posted by Legacy on 06/11/2002 12:00am

    Originally posted by: young

    BmpButs.rc(20) : fatal error RC4214: Codepage not valid:  ignored
    
    Error executing rc.exe.

    BmpButs.rc file 20 line : LANGUAGE LANG_HUNGARIAN, SUBLANG_DEFAULT


    Please answer..

    Reply
  • flaw with unhighlighting buttons

    Posted by Legacy on 06/03/2002 12:00am

    Originally posted by: Michael Yee

    There is a flaw in the way you are unhightlighting buttons (via trapping WM_MOUSEMOVE messages). For example, if you place the buttons together such that there is little or no gap between them, or if you place a button near the edge of the dialog.

    In such cases, the dialog may not receive a WM_MOUSEMOVE message and one or more buttons can remain highlighted.

    A solution I found is to use mouse capturing instead of trapping WM_MOUSEMOVE.


    *Added June 03, 2002*

    I found a small bug with using mouse capturing (which I could not fix). Hence, I found a cleaner solution, which is to use TrackMouseEvent and trapping WM_MOUSELEAVE. This works great so far!

    - Michael Yee

    Reply
  • thank's your code

    Posted by Legacy on 05/23/2002 12:00am

    Originally posted by: lee

    This is very useful for me..

    Reply
  • A Question!

    Posted by Legacy on 04/04/2002 12:00am

    Originally posted by: Hidy

    It's a very good sample!
    I want to ask what's the use of function shown below
    void CBmpButsDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
    // TODO: Add your message handler code here and/or call default
    CDialog::OnLButtonDown(nFlags, point);
    PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));
    }

    If I put "//" before "PostMessage(......)", I got the same
    result after I run it. why?
    What is the function of Message of WM_NCLBUTTONDOWN?

    thanks

    Reply
  • Very nice sample

    Posted by Legacy on 01/23/2002 12:00am

    Originally posted by: Petr Havlicek

    Very nice sample, thanks for a good job!

    Reply
  • Easy to understand and implement

    Posted by Legacy on 01/22/2002 12:00am

    Originally posted by: Franz

    You 've done a great job!
    

    Reply
  • Cool

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: Zoltan Nagy

    This is a cool Application.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds