This topic has not yet been rated - Rate this topic

CWnd::Create

Creates the specified child window and attaches it to the CWnd object.

virtual BOOL Create(
   LPCTSTR lpszClassName,
   LPCTSTR lpszWindowName,
   DWORD dwStyle,
   Const RECT& rect,
   CWnd* pParentWnd,
   UINT nID,
   CCreateContext* pContext = NULL
);
[in] lpszClassName

Pointer to a null-terminated string that contains the name of a registered system window class; or the name of a predefined system window class.

[in] lpszWindowName

Pointer to a null-terminated string that contains the window display name; otherwise NULL for no window display name.

[in] dwStyle

Bitwise combination (OR) of window styles. The WS_POPUP option is not a valid style.

[in] rect

The size and location of the window relative to the top-left corner of the parent window.

[in] pParentWnd

Pointer to the parent window.

[in] nID

ID of the window.

[in] pContext

Pointer to a CCreateContext structure that is used to customize the document-view architecture for the application.

TRUE if the method was successful; otherwise FALSE.

Use the AfxRegisterWndClass function to register window classes. User defined window classes are available in the module where they are registered.

The CWnd::OnCreate method is called before the Create method returns, and before the window becomes visible.


// Dynamically create static control using CWnd::Create,
// instead of with CStatic::Create, which doesn't
// need the "STATIC" class name.
void CMyDlg::OnCreateStatic() 
{
   // m_pWndStatic is a CWnd* member of CMyDlg
   m_pWndStatic = new CWnd;
   m_pWndStatic->Create(_T("STATIC"), _T("Hi"), WS_CHILD | WS_VISIBLE,
       CRect(0, 0, 20, 20), this, 1234);
}


Header: afxwin.h

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
The ID of a child window can no longer be 0.
http://connect.microsoft.com/VisualStudio/feedback/details/648513/cwnd-precreatewindow-change-causing-problems

Any child window must now have an ID other than zero.
CWnd::PreCreateWindow will assign one for you if you do not assign one.
This is in order for GetDlgItem to work correctly.
The change fixes a crash in managed/native interop scenarios.
There is a TRACE statement in CWnd::Create which attempts to alert the developer of the problem.