Use of cbitmapbutton
Cbitmapbutton is not used by many people as the control class of MFC, because there are various button classes derived from cbutton on the Internet, the most famous of which is
The cbuttonst class. However, the cbitmapbutton class has been used for several problems recently in csdn. However, program crashes or errors may occur due to incorrect use or improper use. Institute
This section summarizes the use of the cbitmapbutton class, hoping to help beginners.
You can refer to the built-in msdn example "ctrltest" to learn how to use cbitmapbutton.
My personal summary is as follows:
1. Select the owner draw of the button during resource editing. You do not need to select the bitmap attribute!
2. Define a cbitmapbutton member variable in the program. You cannot use classwizard to map a cbutton variable to a cbitmapbutton. In this case, the button cannot be directly mapped to a cbitmapbutton object, but an initialization error may occur.
3-1. Use cbitmapbutton: loadbitmaps to load images of various States and associate them with the desired button using subclassdlgitem. Use
Cbitmapbutton: sizetocontent function makes the button fit the image size .. Note that loadbitmaps must be performed before the associated button!
3-2, or use the cbitmapbutton: autoload function to associate the desired button. Note: A. It cannot be used before.
Cbitmapbutton: loadbitmaps loads images of various States. Otherwise, an error occurs. B. Associate the autoload function and change the button size
Cbitmapbutton: function of sizetocontent. C. cbitmapbutton: The bitmap used by autoload is the default resource ID.
That is, it will automatically load the corresponding resource bitmap. The bitmap resource ID format is: "button caption + u", "button caption + D", "button caption + F", "button
Caption + X ", indicating the up, down, focus, and disable statuses respectively. For example, if you want the associated button caption to be test during resource editing, its default value is
The ID of the loaded bitmap resource is "testu"/"testd"/"testf"/"testx". Note that Semicolon "" is also part of its ID.
Over
Sometimes we want different buttons to be displayed in different forms. For example, the four statuses of buttons
Normal display (up) Click (down) to get the focus (Focus) unavailable (disable)
Therefore, we can use four bitmaps to correspond to these four States. Instead of loading bitmaps for each State, we only need to use
Cbitmapbutton class
There are two methods to achieve this:
First:
Assume there are four bitmaps: idb_bmp _up idb_bmp _down idb_bmp _focus idb_bmp _disable
Add a cbitmapbutton Class Object m_bmp BTN to the dialog box;
At the top of the dialog box, set the owner draw attribute by clicking ID: idc_btn_img.
Then in oninitdialog:
M_bmp btn.loadbitmaps (idb_bmp _up, idb_bmp _down, idb_bmp _focus, idb_bmp _disable );
M_bmp btn.subclassdlgitem (idc_btn_img, this );
Subclassdlgitem (uint NID, cwnd * pparent) is used to associate m_bmp BTN with idc_btn_img.
Method 2:
Use autoload (uint NID, cwnd * pparent), as the name suggests, to automatically load bitmap for the button
But it seems that this function has nothing to do with bitmap ???
In this case, we need to work hard to name the bitmap. It is not feasible to directly use the above bitmap. We need to rename it.
Assume that the button caption is: image (must have
), The four bitmaps are:
"Imageu" "imaged" "imagef" "imagex"
Note that the IDs of the four bitmaps contain double quotation marks.
Of