Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
int cy;
int cx;
int y;
int x;
LONG style;
LPCSTR lpszName;
LPCSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT;
As you can see, the member variables of this structure are very similar to the arguments
of the Win32's CreateWindow() and CreateWindowEx() functions. Its member variables
correspond as follows:
CREATESTRUCT
CreateWindow
and
CreateWindowEx
Meaning
x x Distance from left border of monitor to left border of
window frame
Y y Distance from top border of monitor to top border of
window frame
cx nWidth
Distance from left border of monitor to right border of
window frame
cy nHeight
Distance from top border of monitor to bottom border of
window frame
Therefore, in your CFrameWnd::PreCreateWindow() event, assign the desired values
for the location and size of the window. In the following example, the original
dimensions of the window are set to 450x325 (width x height):
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.cx = 450;
cs.cy = 325;
return TRUE;
}
Of course, you can also use the PreCreateWindow() event to customize the appearance
of the window frame.
Once a regular window, that is, an overlapped window, has been created, it displays a
title bar, its system buttons, and borders. As stated already, if you created an application
using AppWizard, the window may appear in a random location, which you can control.
If you want the window to be positioned in the center of the screen, call the
CWnd::CenterWindow() method. Its syntax is:
void CenterWindow(CWnd* pAlternateOwner = NULL);
By default, this window positioned the caller (the window that called it) in the middle-
center of the main window, also called its owner. For a main frame of a window, the
owner would be the monitor screen. As long as the window is owned by another, the