How to Use CBitmapButton to Resize a Bitmap to Fit a Button

Last reviewed: October 10, 1997
Article ID: Q134421
1.50 1.51 1.52 | 2.00 2.10 2.20 4.00 4.10 4.20
WINDOWS        | WINDOWS NT
kbprg kbui kbcode

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with:

        - Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52
        - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2,
          4.0, 4.1, 4.2
    

SUMMARY

You can override the default behavior of CBitmapButton so the bitmap sizes to fit the button. How CBitmapButtons behave during initialization depends on which function is used to load the bitmaps.

Function        Behavior                  When used
AutoLoad()      button sizes to bitmap    button is in dialog box

LoadBitmaps()   button remains at size    button is in non-dialog
                specified when created    window


If you want the bitmap to stretch to fit the dimensions of the button you create, you must override these functions:

   CBitmapButton::AutoLoad()
   CBitmapButton::DrawItem()

NOTE: You need only override AutoLoad() if you call it to initialize a button.

MORE INFORMATION

The following steps give the necessary changes to AutoLoad and DrawItem:

  1. Implement the owner drawn CBitmapButton using one of the standard procedures described in the Books Online help for the CBitmapButton class.

  2. Derive a class from CBitmapButton.

    NOTE: If you are using Classwizard, you can not derive directly from CBitmapButton; use a base class lower in hierarchy.

  3. To override the AutoLoad member function:

    a. Include a function prototype in your CBitmapButton-derived class

          header (.h) file, as in this example:
    

          class CMyButton: public CBitmapButton
          {
    
            ...
          public:
            CMyButton(){};
            BOOL AutoLoad(UINT nID, CWnd* pParent);     // ADD THIS LINE
            ...
          };
    
       b. Implement CMyButton::AutoLoad() in the .cpp file by copying the
          CBitmapButton::AutoLoad() function from Winbtn.cpp file in the MFC
          Src directory and changing the following line near the end of the
          function into a comment:
    
          // change following to a comment to prevent button resizing
          // SizeToContent();
    
    

  4. To override the DrawItem() member function:

    a. Include a function prototype in your CBitmapButton-derived class

          header (.h) file, as in this example:
    

          class CMyButton: public CBitmapButton
          {
    
            ...
          public:
            CMyButton(){};
            void DrawItem(LPDRAWITEMSTRUCT lpDIS);     // ADD THIS LINE
            ...
          };
    
       b. Implement CBitmapButton::DrawItem() in the .cpp file by copying the
          CBitmapButton::DrawItem() function from Winbtn.cpp in the MFC Src
          directory and making these changes:
    
          Replace this code:
    
             pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
                 &memDC, 0, 0, SRCCOPY);
    
          with this code:
    
             // determine bitmaps size for use in StretchBlt
             BITMAP bits;
             pBitmap->GetObject(sizeof(BITMAP),&bits);
             pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),
                &memDC,0,0,bits.bmWidth, bits.bmHeight, SRCCOPY);
    
    

  5. Modify the declaration of the CBitmapButton member variable from step 1

        so that it is now of type CMyButton.
    


Additional reference words: kbinf 1.50 1.51 1.52 2.50 2.51 2.52 2.00
2.10 2.20 3.00 3.10 4.00 4.10 4.20
KBCategory: kbprg kbui kbcode
KBSubcategory: MfcUI
Keywords : MfcUI kbcode kbprg kbui
Technology : kbMfc
Version : 1.50 1.51 1.52 | 2.00 2.10 2.20
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.