Microsoft KB Archive/134421

= HOWTO: How to Use CBitmapButton to Resize a Bitmap to Fit a Button =

Article ID: 134421

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q134421

<div class="summary_section">

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. <pre class="fixed_text">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.

<div class="moreinformation_section">

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

<ol> Implement the owner drawn CBitmapButton using one of the standard procedures described in the Books Online help for the CBitmapButton class.</li> 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.</li> To override the AutoLoad member function:

<ol style="list-style-type: lower-alpha;">  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 ...     };                                </li> <li> 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; </li></ol> </li> <li>To override the DrawItem member function:

<ol style="list-style-type: lower-alpha;"> <li> 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 ...     };                                </li> <li> 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); </li></ol> </li> <li>Modify the declaration of the CBitmapButton member variable from step 1 so that it is now of type CMyButton.</li></ol>

Additional query 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

Keywords: kbbitmap kbbutton kbcode kbctrl kbhowto kbuidesign KB134421

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.