แก้ไข

แชร์ผ่าน


Adding Controls By Hand

You can either add controls to a dialog box with the dialog editor or add them yourself, with code.

To create a control object yourself, you will usually embed the C++ control object in a C++ dialog or frame-window object. Like many other objects in the framework, controls require two-stage construction. You should call the control's Create member function as part of creating the parent dialog box or frame window. For dialog boxes, this is usually done in OnInitDialog, and for frame windows, in OnCreate.

The following example shows how you might declare a CEdit object in the class declaration of a derived dialog class and then call the Create member function in OnInitDialog. Because the CEdit object is declared as an embedded object, it is automatically constructed when the dialog object is constructed, but it must still be initialized with its own Create member function.

class CCustomDialog : public CDialog
{
   CEdit m_edit;
   virtual BOOL OnInitDialog();
};

The following OnInitDialog function sets up a rectangle, then calls Create to create the Windows edit control and attach it to the uninitialized CEdit object.

BOOL CCustomDialog::OnInitDialog()
{
   CDialog::OnInitDialog();
   CRect rect(85, 110, 180, 210);

   m_edit.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP |
                     ES_AUTOHSCROLL | WS_BORDER,
                 rect, this, IDC_EXTRA_EDIT);
   m_edit.SetFocus();
   return FALSE;
}

After creating the edit object, you can also set the input focus to the control by calling the SetFocus member function. Finally, you return 0 from OnInitDialog to show that you set the focus. If you return a nonzero value, the dialog manager sets the focus to the first control item in the dialog item list. In most cases, you'll want to add controls to your dialog boxes with the dialog editor.

See also

Making and Using Controls
Controls
CDialog::OnInitDialog