Partager via


CDialog::Create

Call Create to create a modeless dialog box using a dialog-box template from a resource.

virtual BOOL Create( 
   LPCTSTR lpszTemplateName, 
   CWnd* pParentWnd = NULL  
); 
virtual BOOL Create( 
   UINT nIDTemplate, 
   CWnd* pParentWnd = NULL  
);

Parameters

  • lpszTemplateName
    Contains a null-terminated string that is the name of a dialog-box template resource.

  • pParentWnd
    Points to the parent window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object's parent window is set to the main application window.

  • nIDTemplate
    Contains the ID number of a dialog-box template resource.

Return Value

Both forms return nonzero if dialog-box creation and initialization were successful; otherwise 0.

Remarks

You can put the call to Create inside the constructor or call it after the constructor is invoked.

Two forms of the Create member function are provided for access to the dialog-box template resource by either template name or template ID number (for example, IDD_DIALOG1).

For either form, pass a pointer to the parent window object. If pParentWnd is NULL, the dialog box will be created with its parent or owner window set to the main application window.

The Create member function returns immediately after it creates the dialog box.

Use the WS_VISIBLE style in the dialog-box template if the dialog box should appear when the parent window is created. Otherwise, you must call ShowWindow. For further dialog-box styles and their application, see the DLGTEMPLATE structure in the Windows SDK and Window Styles in the MFC Reference.

Use the CWnd::DestroyWindow function to destroy a dialog box created by the Create function.

Example

void CMyDialog::OnMenuShowSimpleDialog()
{
   //m_pSimpleDialog initialized to NULL in the constructor of CMyDialog class
   m_pSimpleDlg = new CSimpleDlg();
   //Check if new succeeded and we got a valid pointer to a dialog object 
   if(m_pSimpleDlg != NULL)
   {
      BOOL ret = m_pSimpleDlg->Create(IDD_SIMPLEDIALOG, this);

      if(!ret)   //Create failed.
         AfxMessageBox(_T("Error creating Dialog"));

      m_pSimpleDlg->ShowWindow(SW_SHOW);
   }
   else
   {
      AfxMessageBox(_T("Error Creating Dialog Object"));
   }
}

Requirements

Header: afxwin.h

See Also

Reference

CDialog Class

Hierarchy Chart

CDialog::CDialog

CWnd::DestroyWindow

CDialog::InitModalIndirect

CDialog::DoModal

CreateDialog