This function is used to subclass A control. subclass (subclass) is one of the most common forms technologies in MFC. Subclass: attach a form class object to a Windows form entity (that is, assign the hwnd of a form to this class ). In addition, the message of this type of object is added to the message routing so that the class can capture messages. Subclassdlgitem can dynamically connect existing controls in the dialog box with a window object. This window object takes over the message processing of the control, so that the control has new features. the subclassdlgitem function is declared as bool subclassdlgitem (uint NID, cwnd * pparent). The NID parameter is the Control ID and pparent is the pointer to the parent window. if the connection is successful, the function returns true; otherwise, false. to sum up Program To use the derived control, you should follow the following two steps: place the basic control in the dialog box template. embedded the object of the derived control class in the dialog box class. call subclassdlgitem in oninitdialog to connect the control object of the derived class to the base class control in the dialog box. Then, this base class control becomes a derived control. To create a new control in a program, you obviously cannot use the automatic creation method, because the dialog box template has no idea about the features of the new control. the program can create controls manually. when calling the create function of a derived class, the derived class calls the create function of the base class to create controls. creating a control using the create function is troublesome. The program needs to specify a lot of control styles for the function and the coordinates and IDs of the control. in particular, the coordinates of controls. It is difficult for inexperienced programmers to accurately arrange the positions and sizes of controls. the dynamic connection function of cwnd: subclassdlgitem provided by MFC can avoid a lot of trouble in the create function. This function greatly simplifies the process of creating a derived control in the dialog box.
As you know, when creating a control manually, you must first build a control object and then use the create function to create a control window on the screen. That is to say, controls are created by control objects. the idea of dynamic connection is different. subclassdlgitem can dynamically connect the existing controls in the dialog box with a window object. This window object will take over the message processing of the control, so that the control has new features. the Declaration of the subclassdlgitem function is
Bool subclassdlgitem (uint NID, cwnd * pparent );
The NID parameter is the Control ID, and the pparent parameter is the pointer to the parent window. If the connection is successful, the function returns true; otherwise, false.
To sum up, to use a derived control in a program, you should follow the following two steps:
Place the basic controls in the dialog box template.
Embedded the object of the derived control class in the dialog box class.
Call subclassdlgitem in oninitdialog to connect the control object of the derived class to the base class control in the dialog box. Then, the base class control is changed to a derived control.
For example, if you want to use the newly designed edit box control in the dialog box, you should first place a normal edit box in the appropriate position in the dialog box template. Then, in the oninitdialog function, call subclassdlgitem as follows:
Bool cmydialog: oninitdialog ()
{
Cdialog: oninitdialog ();
M_myedit.subclassdlgitem (idc_myedit, this );
Return true;
}