Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

Basic knowledge

SubclassDlgItem of MFC control

subclassdlgitem
This function is used to subclass a control.
Subclass (subclassing) is one of the most commonly used forms technology in MFC. Subclassing accomplishes two tasks: one is to attach the form class object to a windows form entity (that is, assign the hwnd of a form to the class). In addition, the messages of this class of objects are added to the message routing, so that this class can capture messages.
SubclassDlgItem can dynamically connect the existing control in the dialog box with a window object, which will take over the control's message processing, so that the control has new characteristics. The declaration of the SubclassDlgItem function is
BOOL SubclassDlgItem (UINT nID, CWnd * pParent);
The parameter nID is the ID of the control, and pParent is a pointer to the parent window. If the connection is successful, the function returns TRUE, otherwise returns FALSE.
In summary, to use derived controls in your program, you should follow these two steps:
Place the base class controls in the dialog template.
Objects of the derived control class are embedded in the dialog class.
Call SubclassDlgItem in OnInitDialog to connect the control object of the derived class with the control of the base class in the dialog box, then the control object of this base class becomes a derived control object

To create a newly designed control in a program, it is obviously not possible to use the automatic creation method, because the dialog box template knows nothing about the characteristics of the new control. Programs can create controls by manual methods. When the Create function of a derived class is called, the derived class will call the Create function of the base class to create the control. Creating a control with the Create function is a cumbersome task. The program needs to specify a lot of control styles for the function, as well as the coordinates and ID of the control. Especially the coordinates of the controls, it is difficult for inexperienced programmers to arrange the position and size of the controls accurately, often it needs to be adjusted repeatedly. Using the dynamic connection function provided by CWnd :: SubclassDlgItem of MFC can avoid many troubles of the Create function, which greatly simplifies the process of creating a derived control in a dialog box.
As you know, when you create a control by manual method, you first need to build a control object, and then use the Create function to create a control window on the screen. That is to say, the creation of the control is done by the control object. The idea of dynamic connection is different. SubclassDlgItem can dynamically connect the existing control in the dialog box with a window object, and the window object will take over the control's message processing, so that the control has new characteristics. The declaration of the SubclassDlgItem function is
 

BOOL SubclassDlgItem (UINT nID, CWnd * pParent);


The parameter nID is the ID of the control, and pParent is a pointer to the parent window. If the connection is successful, the function returns TRUE, otherwise returns FALSE.

In summary, to use derived controls in your program, you should follow these two steps:

Place the base class controls in the dialog template.

Objects of the derived control class are embedded in the dialog class.

Call SubclassDlgItem in OnInitDialog to connect the control object of the derived class with the base class control in the dialog box. Then the base class control becomes a derived control.


For example, if you want to use the newly designed edit box control in a dialog box, you should first place a common edit box in the appropriate position in the dialog box template, and then call SubclassDlgItem in the OnInitDialog function as follows:

BOOL CMyDialog :: OnInitDialog ()

{

CDialog :: OnInitDialog ();

m_MyEdit.SubclassDlgItem (IDC_MYEDIT, this);

return TRUE;

}
 

Another premise is that edit1 is still in its life cycle. You can try to create a pointer member variable of the CEditnew class in the main class, and then assign this pointer to the value of new CEditnew () in the initialization and continue as usual. Try
For example, the member variable CEditnew m_myEdit;
Then in initialization: m_myEdit = new CEditnew ();
m_myEdit-> SubclassDlgItem (IDC_EDIT1, this);
m_myEdit-> ShowWindow (SW_SHOW);
Try it like this.

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code