Add a button in SDI and respond to a click event

Source: Internet
Author: User

Search on the Internet a lot of information, found to be able to display the button on a single document, but is unable to respond to events,

For a long time, found M_pbutton->create () to be placed in the C***view class of OnCreate ().

But in VS2008 new single document program C***view class does not overload the OnCreate () function, to

Overload the function yourself, which can be generated using the wizard. Here is a detailed procedure:

A new SDI (single document) program button, the wizard generates the Cbuttonview class, and other classes

There are also buttonview.h,buttonview.cpp two files, and of course other documents.

Second, add the CButton pointer variable in the Cbuttonview class M_pbutton

Third, add OnCreate function in Cbuttonview class, generate Wm_create message function through Class Wizard.

Iv. adding in the Resoure.h file

#define IDC_TEST 1000

Add a button-generating function to the OnCreate function of the Cbuttonview class

int cbuttonview::oncreate (lpcreatestruct lpcreatestruct)

{

if (cview::oncreate (lpcreatestruct) = =-1)

return-1;

TODO: Add your own dedicated creation code here

M_pbutton = new CButton;

M_pbutton->create ("Please click", ws_child| Ws_visible, CRect (100,100,200,140), this,idc_test);

return 0;

}

VI. Add the test function to the Cbuttonview class, which can be generated by the wizard

In ButtonView.h

Class Cbuttonview:public CView

{

..............

Public

afx_msg int OnCreate (lpcreatestruct lpcreatestruct);

void test (void);

}

In ButtonView.cpp

void Cbuttonview::test (void)

{

AfxMessageBox ("You clicked on Me");

}

Vii. adding a message map to the Cbuttonview class, ButtonView.cpp file

Begin_message_map (Cbuttonview, CView)

..........

Add a click Message

On_bn_clicked (Idc_test, &cbuttonview::test)

On_wm_create ()

End_message_map ()

Eight, run the program, in the SDI customer area has a "please click" button, click on the pop-up "You clicked me" message box

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.