Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals


If you are working on an MFC application, you can derive a class from CFrameWnd, as
we have done so far. Each window that can display on the computer screen is based on a
class equipped with a method called Create. The structure of this method is different from
one type of window to another. Nevertheless, because all window objects are based on
CWnd, the CWnd class provides the primary functionality used by all the other window
controls.

The process of creating a frame window in an MFC application is done by using the
class' constructor and calling its Create() method. As we have seen in the past, the main
window object you must create is a frame because it gives presence to your application.
The most basic frame is created using the CFrameWnd class. We have learned that this
is done by deriving your own class from CFrameWnd. Therefore, in the constructor of
your CFrameWnd-derived class, call the Create() method. As a reminder from Lesson
2, the syntax of the CFrameWnd::Create() method is as follows:

BOOL Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle = WS_OVERLAPPEDWINDOW,
const RECT& rect = rectDefault,
CWnd* pParentWnd = NULL,
LPCTSTR lpszMenuName = NULL,
DWORD dwExStyle = 0,
CCreateContext* pContext = NULL );

If the method succeeds in creating the window, it returns TRUE. If it fails, it returns
FALSE.

Practical Learning: Initiating Window Creation



  1. Declare a handle to a window object as HWND and initialize it with the
    CreateWindowEx() function:


//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
HWND hWnd;
WNDCLASSEX WndClsEx;

RegisterClassEx(&WndClsEx);

hWnd = CreateWindowEx();

return 0;
}
//---------------------------------------------------------------------------


  1. Save All


10.2.2..The Window Class Name...................................................................


To create a window, you must provide its name as everything else in the computer has a
name. There are two main types of class names you will use in your applications. If the
Free download pdf