In the dialog box, perform message ing (cedit *) getdlgitem (idc_edit_test)-> setwindowtext ("This is a edit box ")

Source: Internet
Author: User
Message ing in the dialog box


One advantage of the dialog box is that you can use classwizard to map messages generated by various controls in the dialog box. classwizrd can list messages that can be used by various controls and automatically generateCode. This section uses an example to illustrate how to map subwindow messages in the dialog box and how to initialize subwindows in the dialog box.

First, an edit dialog box is generated. The following table lists the controls and IDs used in the dialog box:

ID Type
Idc_radio_test_1 Circle button
Idc_radio_test_2 Circle button
Idc_button_test Button
Idc_check_test Check button
Idc_tree_test Tree Control
Idc_list_ctrl List CTRL
Idc_tab_ctrl Tab CTRL
Idc_list_test List box
Idc_combo_test Combo box
Idc_edit_test Input box

First, we need to initialize each control in the oninitdialog () function of the dialog box. Here we use the cwnd * getdlgitem (int nid) to obtain the sub-window pointer through the ID. (Similar functions include uint getdlgitemint (int nid, bool * lptrans = NULL, bool bsigned = true). Use the ID number to obtain the number entered in the subwindow. Int getdlgitemtext (int nid, cstring & rstring) use the ID to obtain the text entered in the subwindow ). The Code is as follows:

Bool cmy54_s1dlg: oninitdialog () {cdialog: oninitdialog ();/* Add initialization Code * // initialization input box (cedit *) getdlgitem (idc_edit_test )) -> setwindowtext ("this is a edit box"); // initialize the ListBox clistbox * plistb = (clistbox *) getdlgitem (idc_list_test ); plistb-> addstring ("item 1"); plistb-> addstring ("item 2"); plistb-> addstring ("item 3 "); // initialize the combo box ccombobox * PCB = (ccombobox *) getdlgitem (idc_combo_test); PCB-> addstring ("item 1"); PCB-> addstring ("item 2 "); PCB-> addstring ("item 3"); // initialize the tab CTRL ctabctrl * PTAB = (ctabctrl *) getdlgitem (idc_tab_test); PTAB-> insertitem (0, "tab page1"); PTAB-> insertitem (1, "tab page2"); PTAB-> insertitem (2, "tab page3 "); // initialize listctrl clistctrl * plist = (clistctrl *) getdlgitem (idc_list_ctrl); plist-> insertcolumn (0, "column 1", lvcfmt_left, 100 ); plist-> insertitem (0, "item 1"); plist-> insertitem (1, "item 2"); plist-> insertitem (2, "item 3 "); // initialize treectrl ctreectrl * ptree = (ctreectrl *) getdlgitem (idc_tree_test); ptree-> insertitem ("node1", 0, 0 ); htreeitem hnode = ptree-> insertitem ("node2",); ptree-> insertitem ("Node2-1", hnode); ptree-> insertitem ("Node2-2, hnode); ptree-> Expand (hnode, tve_expand); Return true; // return true unless you set the focus to a control}

Next, we need to use classwizard to map the messages generated by the control, open the classwizard dialog box, select the ID of the control, and display the available messages in the list on the right. For example, we map the button message. After selecting the button ID (idc_button_test), we will see two messages: bn_clicked and bn_doubleclicked. Double-click bn_clicked and enter the function name in the pop-up dialog box. classwizard will generate a message ing when the button is pressed.

Next, let's take a look at the ing of the tabctrl tcn_selchange message. When the current page of tabctrl is changed, the message will be sent, therefore, by ing the message, you can be notified in time when the current page changes.

Finally, we map the en_change message in the input box. After the text in the input box changes, the message will be sent. The related code is as follows:

 // define the relevant message processing function in the header file afx_msg void onbuttontest (); afx_msg void onselchangetabtest (nmhdr * pnmhdr, lresult * presult); afx_msg void onchangeedittest (); //} afx_msg callback () // message ing code callback (response, onbuttontest) on_notify (tcn_selchange, idc_tab_test, onselchangetabtest) on_en_change (idc_edit_test, onchangeedittest) in the CPP File) ///} afx_msg_map end_message_map () // message processing function void cmy54_s1dlg: onbuttontest () {afxmessagebox ("You pressed a button");} void cmy54_s1dlg :: onselchangetabtest (nmhdr * pnmhdr, lresult * presult) {trace ("tab select changed \ n"); * presult = 0;} void cmy54_s1dlg: onchangeedittest () {trace ("edit_box text changed \ n") ;}

similar methods can be used for message ing for other controls. Download an example. In addition, if you are not familiar with the messages that can be used by various controls, you can use the dialog box and then use classwizard to generate the relevant code, you can also copy the code generated by classwizard directly to other places you need (it is a tip to say that I learned it in the first place ).

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.