www.pudn.com > mfc资源大全1.rar > bitmap_text_button.shtml
Controls - PushButton with bitmap & text
The class CSXButton is a simple replacement for the MFC CButton class that enables you to put both an image and text onto the same button. MFC CButton only allows an image or text, but not both. The dialog below is quite overwhelming with unnecessary graphics (except maybe for the big button on the right), but it gives you an idea of what you can do with CSXButton.
For a bitmap, the image is loaded with ::CreateMappedBitmap. Using this function allows CSXButton to give the bitmaps a transparent quality, mapping one color which you specify, to the system button face color, retrieved from GetSysColor( COLOR_BTNFACE ). If you use a bitmap with a mask, a disabled image is also created mapping the masked color to white, RGB(255, 255, 255). This will allow the windows embossing drawing effect (the effect used to create the 'disabled' look) to give the illusion of ignoring the masked area.
1. Add the source files to your project.
2. Create a button on your dialog, and make sure you turn on the "Owner Draw" property as shown below:
3. Create the button member variable.
Option 1: Add a member variable for the button in the ClassWizard, and specify the variable type to be CSXButton as shown below. NOTE: Since you did not create the CSXButton class through the ClassWizard, you will have to rebuild the .clw ClassWizard file for your project. For instructions on how to do this, search the VC++ documentation for "Rebuilding the ClassWizard (.CLW) File".
Option 2: Create you own member variable in your dialog of type CSXButton, and subclass the variable. For example:
// In Dialog Class Header File CSXButton m_brock; // In Dialog OnInitDialog() m_brock.SubclassDlgItem( IDC_BUTTON_ROCK, this /*parent*/ );
4. Call any one of the following functions to attach an image to the button, and note that the nWidth and nHeight do NOT have to be the actual size of the image, the image will be scaled to the size you specify. You can only use bitmaps with 16-colors or less, and icons with 256 colors or less. Use caution on the images you select because the colors may not be displayed correctly on machines that do not support the color palette in your images.
CSXButton::SetIcon( nID, nWidth, nHeight ); CSXButton::SetBitmap( nID, nWidth, nHeight ); CSXButton::SetMaskedBitmap( nID, nWidth, nHeight, crTransparentMask );
Examples:
m_brock.SetIcon( IDI_ROCK, 40, 150 ); m_bhelp.SetIcon( IDI_HELP, 16, 16 ); m_brock.SetBitmap( IDB_ROCK, 40, 150 ); m_brock.SetBitmap( IDB_ROCK, 12, 67 ); // Use the bitmap IDB_ROCK, and replace any instance of // RGB( 255, 0, 0 ) (bright red), with the system // color of the button face. m_brock.SetMaskedBitmap( IDB_ROCK, 40, 150, RGB( 255, 0, 0 ) );
5. If it is the default button, call CSXButton::SetDefaultButton() to make it the default button. If you need to de-activate, or switch default buttons, call CSXButton::SetDefaultButton( FALSE );
Example:
m_bok.SetDefaultButton( FALSE ); // Deactivate m_brock.SetDefaultButton(); // Activate
6. Set the positioning of the image and text. Default positioning is the image offset 4 pixels from the left border, and the text is offset 8 pixels from the right of the image. Both image and text are centered vertically. The buttons in the above sample, except for "This Space For Rent", use this default positioning. You can change these offsets with CSXButton::SetImageOffset( int nPixelsFromLeftofBorder ), and CSXButton::SetTextOffset( nPixelsFromLeftOfImage ).
For added flexibility, you can use CSXButton::SetTextPos( CPoint p), and CSXButton::SetImagePos( CPoint p ), with (0,0) being the upper left corner of the button. You can also use the #define SXBUTTTON_CENTER for either the x or y in the CPoint to automatically center the text or image. The "This Space For Rent" button in the above example uses this method.
Examples:
m_bplanet.SetIcon( IDI_PLANET, 32, 32 ); m_bplanet.SetImagePos( CPoint( SXBUTTON_CENTER, 6 ) ); m_bplanet.SetTextPos( CPoint( SXBUTTON_CENTER, 42 ) );
1. multi-line text.
2. Use of default image sizes.
3. State images like CBitmapButton.
Special thanks to Mark Findlay originally asking for such a button in the CodeGuru's message area, and for later on becoming the guinea pig.
| Goto HomePage |
|
Contact me: zafir@home.com
|