CREATESTRUCT

Attributes of main frame of a doc view/dialog based application can be described in CREATESTRUCT structure. Each memeber of this structure holds the corresponding attributes of a frame window.

typedef struct tagCREATESTRUCT {
    LPVOID      lpCreateParams; /* Create param */
    HINSTANCE   hInstance;      /* Application instance */
    HMENU       hMenu;          /* Handle to menu */
    HWND        hwndParent;     /* Parent Window */
    int         cy;             /* Height */
    int         cx;             /* Width */
    int         y;              /* Y position */
    int         x;              /* X position */
    LONG        style;          /* Window styles */
    LPCSTR      lpszName;       /* Window name */
    LPCSTR      lpszClass;      /* Class name (fixed) */
    DWORD       dwExStyle;      /* Extended style */
} CREATESTRUCT, *LPCREATESTRUCT;

PreCreateWindow event

DOC view framework creates CFrameWnd object after that calls PreCreateWindow() event. Verious frame attibutes can be changed in this event. Here is an example of altering style,position and height width of the window.


BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    /* Call the base-class version */
   if( !CFrameWnd::PreCreateWindow(cs) )
      return FALSE;

    /* 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.= ((cs.cy * 3) - cs.cy) / 2; 
    cs.= ((cs.cx * 3) - cs.cx) / 2;

   return TRUE;
}

Here is the call stack of PreCreateWindow() from MFC framework.

CMainFrame::PreCreateWindow()
CFrameWnd::GetIconWndClass()
CFrameWnd::LoadFrame()
CDocTemplate::CreateNewFrame()
CSingleDocTemplate::OpenDocumentFile()
CDocManager::OnFileNew()
CWinApp::OnFileNew()
_AfxDispatchCmdMsg()

OnCreate() event

OnCreate() event is an window event triggered by WM_CREATE message. Base function CFrameWnd::OnCreate() is called with same CREATESTRUCT pointer which is already altered in PreCreateWindow(). This is the actual creation of the frame window.

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
  return 0;
}

Here is the call stack of OnCreate() from MFC framework.

CMainFrame::OnCreate()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#