Dialog Box and dynamic create button cbutton in MDI, and respond to the event

Source: Internet
Author: User

1. Dynamic create button cbutton in the dialog box:
(1) Add a cbutton data member to the class in the dialog box (such as caboutdlg ).
Class caboutdlg: Public cdialog
{......
: Cbutton m_btn; // button
}

(2) create a button object in the initialization function of the caboutdlg dialog box:
Bool caboutdlg: oninitdialog ()
{
If (! M_btn.create (_ T ("123"), ws_child | ws_visible | bs_pushbutton,
Crect (, 20, 70), this, id_button2 ))
{
Trace0 ("button not created/N ");
Return false; // creation failed
}

Return true;
}
Bs_pushbutton indicates that the button is under pressure (normal button), and id_button2 indicates the ID of the button defined by the user to respond to the event.

Function binding. id_button2 should be defined as follows:
For example, # define id_button2 2000

(3) bind id_button2 of the button to the Event Response Function:
(3.1) Event Response Function declaration:
Class caboutdlg: Public cdialog
{......
: Cbutton m_btn; // button

Afx_msg void onbnclickedbutton1 (); // event declaration, which must be prefixed with afx_msg
}
(3.2) The id_button2 of the button is bound to the Event Response Function.
Begin_message_map (caboutdlg, cdialog)
......
On_bn_clicked (id_button2, & caboutdlg: onbnclickedbutton1)
End_message_map ()
(3.3) definitions of Event Response functions:
Void caboutdlg: onbnclickedbutton1 ()
{
// Todo: add the control notification handler code here
MessageBox (_ T ("donow "));
}

========================================================== ======================================
2. The dynamic create button cbutton in MDI:
(A) Create a cbutton in the view (the button is displayed in the View window ):
(1) Add a cbutton data member to the cmdi_testview class:
Class cmdi_testview: Public cview
{......
: Cbutton m_btn; // button
}
-------------------------
(2) Reload oncreate ():
----------
(2.1) oncreate () Statement:
Class cmdi_testview: Public cview
{......
: Cbutton m_btn; // button

Int oncreate (maid );
}
------------
(2.2) intercept oncreate (). Note that this is required. Otherwise, the oncreate () function will not be called.
Begin_message_map (cmdi_testview, cview)
On_wm_create () // intercept oncreate ()
On_bn_clicked (id_button1, & cmdi_testview: MSG) // id_button1 indicates the ID of the button and is bound to MSG ().
End_message_map ()
-------------
(2.3) oncreate () Definition:
Int cmdi_testview: oncreate (maid)
{
If (cview: oncreate (lpcreatestruct) =-1)
Return-1;
 
If (! BTN. Create (_ T ("123"), ws_child | ws_visible | bs_pushbutton, crect

(70,100,), this, id_button1 ))
// If the ID is idok, but the program does not bind the idok with a message function, it will not work.
{
Trace0 ("button not created/N ");
Return-1; // creation failed
}

Return 0;
}
---------------------------------------
(3) define the event processing function MSG () of the button ():
-------------
(3.1) MSG () Statement:
Class cmdi_testview: Public cview
{......
: Cbutton m_btn; // button

Afx_msg void MSG (); // event declaration. afx_msg must be added before.
}
-------------
The id_button2 of the (3.2) button is bound to the MSG () function:
Begin_message_map (cmdi_testview, cview)
......
On_wm_create () // intercept oncreate ()
On_bn_clicked (id_button1, & cmdi_testview: MSG) // id_button1 indicates the ID of the button and is bound to MSG ().
End_message_map ()
-------------
(3.3) MSG () Definition:
Void cmdi_testview: MSG ()
{
// Todo: add the control notification handler code here
MessageBox (_ T ("donow "));
}

 

========================================================== ===
Note:
(1) it is best to create a cbutton in the view to respond to the button event. If it is created in cchildframe or cmainframe

Cbutton, only buttons are displayed, but events cannot be responded !!!!!!
This may be because the view in MDI (or SDI) is blocked before the mainframe. In this case, write the mouseclick for mainframe.

Event, there will be no response.

(2) on_bn_clicked is the only message of interest sent by cbutton. It is equivalent to the on_command message in cwnd,

A synonym that is simpler and more convenient.

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.