VC uses bitmap and icon to create buttons

Source: Internet
Author: User

Implementation of Bitmap Buttons:
First, create a dialog box-based application cmydialog;
Middleware. MFC cbitmapbutton class, which is also the simplest and most powerful bitmap button. We can take the following steps:
1. specify a unique button title for the button (in this example, set the button title to OK), select the ownerdraw attribute, and add some bitmap Resources in the project, these resources are marked by their names instead of numerical IDs. The IDS are "Oku", "OKD", "okf", and "okx" (double quotation marks must be added ), it corresponds to the buttons in the "up", "down", "focused", and "Disable" statuses.
2. Add the cbitmapbutton m_abmpbtn and data member to the dialog box class.
3. Call for this member during initialization:
...
M_abmpbtn. autoload (idok, this );
...
Click the compile button to run the program. The bitmap button is created.
To change the title of the cancle button, you can set the title to icon or bitmap: (Here we demonstrate the usage of Bitmap. The icon button reader can follow the following code to process it)

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. So I would like to summarize the use of the cbitmapbutton class, hoping to help some 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. The two variables cannot exist at the same time, causing program crash.
3-1. Use cbitmapbutton: loadbitmaps to load images of various States, associate the desired button with subclassdlgitem, and use the cbitmapbutton: sizetocontent function to make 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. Previously, cbitmapbutton: loadbitmaps cannot be used to load images of various States. Otherwise, an error may occur.
B. Functions of the cbitmapbutton: sizetocontent function after the autoload function is complete and the button size is changed.
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 ", the options include Up, down, focus, and disable. For example, if you want the associated button caption to be test during resource editing, the default bitmap resource ID is: "testu"/"testd"/"testf"/"testx", note that Semicolon "" is also part of its ID.

Ii. Use the icon to create a button
1. Open the attribute page of the icon button and select the icon in the style.
2. Define the member variable in the header file of the dialog box class (use classwizard to add this member variable)
Cbutton M _ iconbtn; // corresponds to the icon button
3. Create corresponding icons or bitmap resources:
Icon Resource: idi_iconbutton
4. Add the following code in Initialization:
...
// Corresponds to the icon button
Hicon = afxgetapp ()-> loadicon (IDI _ iconbutton );
M_iconbtn.seticon (hicon );
...
Re-compile and run our program, and the amazing image button is present in front of us.

Iii. Use bitmap to create a button
1. Open the properties page of the bitmap button and select bitmap in the style.
2. Define the member variable in the header file of the dialog box class (use classwizard to add this member variable)
Cbutton m_iconbtn;
3. Create a bitmap Resource:
Bitmap Resource: idb_bitmapbutton
4. Add the following code in Initialization:
// Corresponds to the bitmap button
...
Hbitmap hbmp =: loadbitmap (AfxGetInstanceHandle (),
Makeintresource (IDB _ bitmapbutton ));
M_bmp btn.setbitmap (hbmp );

Original article addressHttp://blog.csdn.net/huahuamoon/archive/2008/02/28/2128753.aspx

Bytes ------------------------------------------------------------------------------------------------

Excerpt from another webpage

Implementation of Bitmap Buttons:
First, create a dialog box-based application cmydialog;
Middleware. MFC cbitmapbutton class, which is also the simplest and most powerful bitmap button. We can take the following steps:
1. specify a unique button title for the button (in this example, set the button title to OK), select the ownerdraw attribute, and add some bitmap Resources in the project, these resources are marked by their names instead of numerical IDs. The IDS are "Oku", "OKD", "okf", and "okx" (double quotation marks must be added ), it corresponds to the buttons in the "up", "down", "focused", and "Disable" statuses.
2. Add the cbitmapbutton m_abmpbtn and data member to the dialog box class.
3. Call for this member during initialization:
...
M_abmpbtn. autoload (idok, this );
...
Click the compile button to run the program. Haha, check the effect. Our bitmap button has been created.
/* If the preceding method does not work, check your bitmap resources. Resource names in appstudio, such as "Oku" and "OKD" must be enclosed by quotation marks. autoload fails, this is probably the result. */
To change the title of the cancle button, you can set the title to icon or bitmap: (Here we demonstrate the usage of Bitmap. The icon button reader can follow the following code to process it)
Ii. Use the icon to create a button
1. Open the attribute page of the icon button and select the icon in the style.
2. Define the member variable in the header file of the dialog box class (use classwizard to add this member variable)
Cbutton M _ iconbtn; // corresponds to the icon button
3. Create corresponding icons or bitmap resources:
Icon Resource: idi_iconbutton
4. Add the following code in Initialization:
...
// Corresponds to the icon button
Hicon = afxgetapp ()-> loadicon (IDI _ iconbutton );
M_iconbtn.seticon (hicon );
...
Re-compile and run our program, and the amazing image button is present in front of us.
Iii. Use bitmap to create a button
1. Open the properties page of the bitmap button and select bitmap in the style.
2. Define the member variable in the header file of the dialog box class (use classwizard to add this member variable)
Cbutton m_iconbtn;
3. Create a bitmap Resource:
Bitmap Resource: idb_bitmapbutton
4. Add the following code in Initialization:
// Corresponds to the bitmap button
...
Hbitmap hbmp =: loadbitmap (AfxGetInstanceHandle (),
Makeintresource (IDB _ bitmapbutton ));
M_bmp btn.setbitmap (hbmp );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.