Simple Bitmap Buttons | CodeGuru

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: Step: Define button states (for instance: default, highlighted, pressed) and draw the bitmap for each state. Step: Derive a […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 18, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.