Basic knowledge
Sorting of the SubclassDlgItem function in MFC
Um, don't talk nonsense. Above
Note: Source of this article 1: BBS Lonely Tobacco Station http://bbs.lzjtu.edu.cn/bbsanc.php?path=/groups/sci.faq/Computer/ProgramOld/VC/9/1/21.txt
2: ^ _ ^ Baidu's blog from the forest http://hi.baidu.com/mf_sky/blog/item/606517d2e84427063bf3cf54.html
3: lyl421985's Sina blog http://blog.sina.com.cn/s/blog_5ceda01b0100nacb.html
Text: First of all, it is still the easiest to understand in the desert desert station, the original text:
CWnd :: SubclassDlgItem
This method dynamically subclasses a control created from a dialog box templ
ate, and attach it to this CWnd object
When a control is dynamically subclassed, windows messages will route throug
h the CWnd message map and call message handlers in the CWnd class first. Me
ssages that are passed to the base class will be passed to the default messa
ge handler in the control.
Example in MSDN:
class CMyButton: public CButton {...};
// m_myButton is a CMyButton object member of CAboutDlg
BOOL CAboutDlg :: OnInitDialog ()
{
CDialog :: OnInitDialog ();
// IDC_BUTTON1 is the ID for a button on the
// dialog template used for CAboutDlg.
m_myButton.SubclassDlgItem (IDC_BUTTON1, this);
return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Originally, m_myButton is just a variable, not associated with any button. After calling SubclassDlgItem, m_myButton
myButton is associated with the button whose ID is IDC_BUTTON1
The remaining two original texts are more detailed, but I have n’t read them carefully, and I ca n’t use them now.
A control on the SubClass Dialog!
After a certain SubClass control, replace its original WindowProc with the WindowProc of your own CYourCWndClass, so that you can process all the messages of this control; use ClassWizard to maintain your own CYourCWndClass.
In addition to SubClassDlgItem in MFC, there are SubclassWindow functions to perform the same function. After being associated with a control ID, all messages of that control will have this class to respond. The general use process is as follows
First, derive a class from the control that wants Subclass, for example, from CEdit-> CMyEdit;
Second, complete the message you want to process in CMyEdit, such as OnChar, etc .;
Third, define a member variable m_myEdit of the CMyEdit class in the dialog class;
Fourth, add m_myEdit (IDC_EDIT, this) to Dialog's OnInitDialog;
So when OnChar occurs on IDC_EDIT, OnChar in CMyEdit will be called
In addition, the act of replacing another process with a window procedure is called SUBCLASSING, which is a derived subclass in the sense of Windows. It is a completely different concept from derived subclasses of object-oriented languages. SubClassDlgItem lets the specified child in the dialog control its own window procedure instead of the default window procedure to intercept the message.
In VC, functions such as DDX_Control in the DoDataExchange function will call SubClassDlgItem to complete the window subclassing, so that the control can process the message itself.
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:
1. Place the base control in the dialog template.
2. Embed the object of the derived control class in the dialog class.
3. 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;
}
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 base class control in the dialog box. Then this base class control becomes a derived control. To create a newly designed control in the program, obviously it cannot be created automatically. Because the dialog 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 template, and then call SubclassDlgItem in the OnInitDialog function as follows:
BOOL CMyDialog :: OnInitDialog ()
{
CDialog :: OnInitDialog ();
m_MyEdit.SubclassDlgItem (IDC_MYEDIT, this);
return TRUE