function, usage and precautions of SubclassDlgItem function in VC + +

Source: Internet
Author: User
Tags bool joins connect

SubclassDlgItem

This function is used to subclass a control.

Subclass (subclass) is one of the most common forms techniques in MFC. Subclass completes two tasks: one is to attach the form class object to a Windows Forms entity (that is, to assign a form's HWND to that class). The other is to add the message of the object to the message route so that the class can capture the message.

SubclassDlgItem can dynamically connect a control in a dialog box to a Window object that takes over the message processing of the control, giving the control a new attribute. The declaration of the SubclassDlgItem function is

BOOL SubclassDlgItem (UINT NID, cwnd* pparent);

Parameter nid is the id,pparent of the control that is a pointer to the parent window. The function returns True if the connection succeeds, or false.

To sum up, to use a derived control in your program, you should follow these steps:

Place the base class control in the dialog box template.

Embeds the object of the derived control class in the dialog class.

Calling SubclassDlgItem in OnInitDialog joins the control object of the derived class with the base class control in the dialog box, the base class control becomes a derived control

To create a newly designed control in your program, it is obvious that you cannot use automatic creation because the dialog box template knows nothing about the properties of the new control. A program can create a control by hand, and when you call the CREATE function of a derived class, the derived class calls the base class's create function and creates the control. Creating a control with the CREATE function is a tricky task, and the program needs to specify a large stack of control styles for the function and the coordinates and IDs of the controls. In particular, the coordinates of the control, it is difficult for inexperienced programmers to accurately arrange the position and size of the control, often need to adjust repeatedly. With the dynamic connectivity features provided by MFC's Cwnd::subclassdlgitem, you can avoid many of the problems with 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 hand, you build a control object and then create a control window on the screen using the CREATE function, which means that the control's creation is done by the control object. The idea of a dynamic connection is different, and SubclassDlgItem can dynamically connect the controls that are already in the dialog box to a Window object that takes over the message processing of the control, giving the control a new feature. The declaration of the SubclassDlgItem function is

BOOL SubclassDlgItem (UINT NID, cwnd* pparent);

Parameter nid is the id,pparent of the control that is a pointer to the parent window. The function returns True if the connection succeeds, or false.

To sum up, to use a derived control in your program, you should follow these steps:

Place the base class control in the dialog box template.

Embeds the object of the derived control class in the dialog class.

Otherwise the function will fail, be sure to pay attention!

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/vc/

Calling SubclassDlgItem in OnInitDialog joins the control object of the derived class with the base class control in the dialog box, the base class control becomes a derived control.

For example, if you want to use a newly designed edit box control in a dialog box, place a normal edit box in the appropriate place 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;

}

The following code demonstrates creating a custom button from a configuration file, using "SubclassDlgItem",

And resolve a method that does not place the base class control in the dialog box template

Cxskinbutton *pskinbnt = NULL;  
PSKINBNT = new Cxskinbutton; Del at Clear ();  
      
Sub-class control  
BOOL ret = Pskinbnt->subclassdlgitem (id,parent);  
      
If execution fails, the base class control if  
(!ret)  
{  
    //Create button  
    UINT stype = ws_child| is not placed in the dialog box template. ws_visible;  
    Pskinbnt->create (NULL, Stype, rect, parent, id);  
}

I called the "SubclassDlgItem" function successfully,

Why is the button not displayed?

A total of two parameters, id,parent, check the parameters passed the correct bar!

This occurs if a duplicate ID is used.

In addition, if you do a variable mapping, calling this function triggers: Asert (m_hwnd== NULL);

Author: csdn blog Micro WX Smile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.