How to dynamically add controls authors used the code: bzshow
Reader level: VC beginners In the resource editor we can easily join the desired controls in the dialog, such as text editing boxes, list controls, etc. But if we need to dynamically generate these controls during operation? This article is to describe how to use code dynamically add controls and provide example projects. Download Sample Project 12.5K Running Interface For convenient demonstration, our gentleman is a dialog-based MFC project, named my next steps in cMydlg.h: public: // Plus this variable CEDIT M_MYDIT; PROTECTED: / / Plus this function to respond to the event AFX_MSG void onchangeedit () in response to the edit box; do the following actions in cMydlg.cpp: // Add this line, set the ID to 1820 # for the edit box define ID_MYEDT 1820 in BEGIN_MESSAGE_MAP (CMyDlg, CDialog) by adding the following ON_EN_CHANGE (ID_MYEDT, OnChangeEdit) add this one role is to map the contents of the edit control change messages to OnChangeEdit function, you have to realize OnChangeEdit () function in CMyDlg.cpp in; good Let us dynamically generate controls! m_MyEdit.CreateEx (WS_EX_CLIENTEDGE, // 3D-border appearance _T ( "EDIT"), NULL, ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | WS_CHILD, rect, this, ID_MYEDT); // generates edit control m_MyEdit.ShowWindow (SW_SHOW); / / Display control m_myedit.setfocus (); // Setting the focus // Dynamic delete can be used as the following statement getDLGITEM (ID_MYEDT) -> DestroyWindow (); // Destroy control In the code we only demonstrate the dynamic generation method of editing controls, others The method of dynamically generated by the control is basically consistent, all can refer to this process.