MFC dynamically created control message processing

zhaozj2021-02-16  143

A few days ago, someone asked how to respond to the news after dynamically create Treectrl, so I wrote my own experience. Now I will finish it.

This example is dynamically created in the View in View and responds to its NM_Click message, first write the code that is dynamically created:

??? New SDI project, add a member variable for View: • CLISTCTRL M_LIST; ??? Create it in the view of View:? If (! M_List.create (WS_Border | WS_CHILD | WS_VISIBLE | LVS_ICON | LVS_AUTOARRANGES | LVS_SHOWSELALWAYS LVS_EDITLABELS, CRECT (10, 10, 310, 210), this, ID_ListCtrl))? {?? trace0 ("failed to create listctrl window / n"); ?? RETURN-1;?}? // Add some item? CString STR; ? for (int i = 0; i <10; i )? {??? str.format ("item% d", i); ?? m_list.insertitem (i, str);?} ??? where ID_ListCtrl is the ID of this control. Of course, you can use the value directly, but in order to define a constant, click the New button in the dialog box pop-up in the main menu view-> Resource Symbols, enter ID_ListCtrl under Name .

??? After the creation, you can run after the compilation link, but how do you click it without reacting, of course, because we haven't written the message processing code. The usual practice is to manually write message processing functions and message mappings, at least three steps: 1. Define the prototype of the message processing function in the header file, 2, implement this function in the CPP, 3, write between begin_MESSAGE_MAP and End_MAP () The message mapping. If it is a single hand, then you can endure, the message is not only troublesome. ??? So I take the following method: ??? 1. Open the About dialog resource, put it in a listCtrl control to the dialog box, and set it to ID_ListCtrl. ??? 2.ctrl w, turn the class Name is changed from the original caboutdlg to cxxxView, select ID_ListCtrl in the Object IDs on the left, then the corresponding notification message will appear on the right, then double-click Add message to usually map. ??? 嘿嘿, is it followed? As the message responding to the control in the dialog, it is very convenient to note. Also pay attention to delete the ListCtrl control on the dialog.

转载请注明原文地址:https://www.9cbs.com/read-8003.html

New Post(0)