Dynamic addition of VC controls
Flyli
Sometimes, for some purpose, we hope that the system controls can be automatically added based on our needs instead of being drawn in the dialog box in advance. At this time, we need to dynamically generate the controls.
For details, we can use CEdit (text box Control) as an example.
Step 1:
You must add a CEdit pointer and then open up the corresponding space for the pointer. it should be noted that we must define pointers instead of directly defining this class. The specific reason is not explained here. Here we write this statement;
CEdit * Text = new CEdit ();
Step 2:
Create a control. I use create to read and initialize text and display it on the desktop. The Code is as follows:
RECT rect;
Rect. top = 0;
Rect. left = 0;
Rect. Rights = 100;
Rect. top = 100;
Text-> Create (WS_CHILD | WS_VISIBLE, rect, (CWnd *) this, FID_ADDRESS );
The first parameter indicates the style of the control, which can be obtained from MSDN. The second parameter indicates the position of the control displayed in the window, the third control is the control window, and the fourth parameter is the Control ID. Its definition must be in resource. h and String table are defined in the resource. If the test is used, it can be defined as an undefined constant, for example: 2008.
The subsequent TEXT can be controlled like a common DDX_Control function. For example, to get a value, you only need
CString xxx;
Text-> GetWindowText (xxx );
In this way, the value in the control is read into the xxx string.
If this article needs to be reproduced, please enter the author's name. Thank you for your cooperation.