MFC dynamically creates a button and implements a toggle display of the bitmap on the button "

Source: Internet
Author: User
Tags response code

Create a button dynamically and add a bitmap to the button to display a different bitmap by clicking the button, which can be set to show the button pressed and bounced in two states. Just determine the a value and enter a different response code.

1, add in the header file:

CButton *pbtn;

2. Add in the initialization function:

PBTN = new CButton ();

Pbtn->create (_t ("My button"), ws_child| Ws_visible|bs_pushbutton|bs_bitmap, CRect (0,0,100,40), this, Idc_button);//button display Name, property, ID number

Pbtn->movewindow (CRect (400,100,500,140));//position and button size

Pbtn->setbitmap (HBITMAP) LoadImage (AfxGetInstanceHandle (), "1.bmp", Image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection));//insert Bitmap, the position of the bitmap to be placed in the program

At this point, the program can display the button, and if there is a bitmap, the bitmap will appear on top of the button;

3, in the Resource.h

Add ID Number: such as # define Idc_button 1002

4. Below the header file

Protected

{{afx_msg (Cmy3view)

Note-the ClassWizard would add and remove member functions here.

Do not EDIT these blocks of generated code!

}}afx_msg

afx_msg void Onbutton ();//This Statement of function

Declare_message_map ()

5, in

Begin_message_map (Cmy3view, CFormView)

{{Afx_msg_map (Cmy3view)

Note-the ClassWizard would add and remove mapping macros here.

Do not EDIT these blocks of generated code!

}}afx_msg_map

Standard printing commands

On_bn_clicked (Idc_button, Onbutton)//Outside (ID number, response function number name)

End_message_map ()

6. Add the response function manually

void Cmyview::onbutton ()

{

MessageBox ("response function added test succeeded");

}

7. Add an integer member variable to the header file

int A;

8. Initialization in constructors

A = 0;

9. Add in the button response function

void Cmyview::onbutton ()

{

if (a%2 = = 0)

{

Pbtn->setbitmap (HBITMAP) LoadImage (AfxGetInstanceHandle (), "2.bmp", Image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection));

}

else if (a%2 = = 1)

{

Pbtn->setbitmap (HBITMAP) LoadImage (AfxGetInstanceHandle (), "1.bmp", Image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection));

}

a++;

}

It is recommended to use the CBitmapButton class if you simply implement a bitmap switch for the button.

http://blog.csdn.net/bigtree_mfc/article/details/40625523

MFC dynamically creates a button and implements a toggle display of the bitmap on the button "

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.