This topic has not yet been rated - Rate this topic

CWnd::PreCreateWindow

Called by the framework before the creation of the Windows window attached to this CWnd object.

virtual BOOL PreCreateWindow(
   CREATESTRUCT& cs 
);
cs

A CREATESTRUCT structure.

Nonzero if the window creation should continue; 0 to indicate creation failure.

Never call this function directly.

The default implementation of this function checks for a NULL window class name and substitutes an appropriate default. Override this member function to modify the CREATESTRUCT structure before the window is created.

Each class derived from CWnd adds its own functionality to its override of PreCreateWindow. By design, these derivations of PreCreateWindow are not documented. To determine the styles appropriate to each class and the interdependencies between the styles, you can examine the MFC source code for your application's base class. If you choose to override PreCreateWindow, you can determine whether the styles used in your application's base class provide the functionality you need by using information gathered from the MFC source code.

For more information on changing window styles, see the Changing the Styles of a Window Created by MFC.


// alter the styles of the mdi frame window
BOOL CMdiChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
   // Create a window without min/max buttons or sizable border
   cs.style |= WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;

   // Size the window to 1/3 screen size and center it
   cs.cy = ::GetSystemMetrics(SM_CYSCREEN) / 3;
   cs.cx = ::GetSystemMetrics(SM_CXSCREEN) / 3;
   cs.y = ((cs.cy * 3) - cs.cy) / 2;
   cs.x = ((cs.cx * 3) - cs.cx) / 2;

   return CMDIChildWnd::PreCreateWindow(cs);
}


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.
Bug in PreCreateWindow and CREATESTRUCT
There is a bug in PreCreateWindow() for VS2010: If your child window has an ID or cs.hMenu of 0, and only 0, it will be replaced with the CWnd *this* pointer cast to an HMENU. This will result in CWnd::RepositionBars() not displaying your child window.