Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture


4.2.1 Window Creation.................................................................................


WM_CREATE: When an object, called a window, is created, the frame that creates the
objects sends a message identified as ON_WM_CREATE. Its syntax is:

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

This message calls the CFrameWnd::Create() method to create a window. To do that, it
takes one argument, which is a pointer to a CREATESTRUCT class. The
CREATESTRUCT class provides all the information needed to create a window. It is
defined as follows:

typedef struct tagCREATESTRUCT {
LPVOID lpCreateParams;
HANDLE hInstance;
HMENU hMenu;
HWND hwndParent;
int cy;
int cx;
int y;
int x;
LONG style;
LPCSTR lpszName;
LPCSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT;

This class provides the same types of information as the WNDCLASS object. When
sending the OnCreate() message, the class is usually created without your intervention
but when calling it, you should check that the window has been created. This can be done
by checking the result returned by the OnCreate() message from the parent class. If the
message returns 0, the window was created. If it returns -1, the class was not created or it
would simply be destroyed. This can be done as follows:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// Create the window and make sure it doesn't return - 1
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// else is implied
return 0;
}

To use this message, in the class definition, type its syntax. In the message table, type the
name of the message ON_WM_CREATE():

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()

Practical Learning: Creating a Window



  1. To create an ON_WM_CREATE message, change the file as follows:


#include <afxwin.h>
Free download pdf