Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


window you are creating is a known type of window, its class is already registered with
the operating system. In this case you can provided it. If you are creating your own fresh
class, which means you are in charge of its characteristics (properties), then define a null-
terminated string as the class' name.

For a Win32 application, the class name of your main window must be provided to the
lpszClassName member variable of your WNDCLASS or WNDCLASSEX variable.
You can provide the name to the variable or declare a global null-terminated string. The
name must also be passed to the lpClassName argument of either the CreateWindow()
or the CreateWindowEx() functions. Here is an example:

If you are creating an MFC application, the class name is passed as the first argument of
the CFrameWnd::Create() method. You can use a null terminated string as done for the
CreateWindow() or the CreateWindowEx() function. If you have initialized the
window class and registered it using AfxRegisterWndClass(), you may remember that
this function returns a null-terminated string. Therefore, you can pass its return value to
the Create() method. This can be done as follows:

CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;

...


const char *StrClass = AfxRegisterWndClass(WndCls.style, WndCls.hCursor,
WndCls.hbrBackground, WndCls.hIcon);

Create(StrClass, ;
}

Once an application is created, although you can, you should refrain from ever changing
the name of a class. It may take more than simply assigning a new value to the
AfxRegisterWndClass() function.

Practical Learning: Naming a Window Class



  1. To provide a name for the window being created, declare a null-terminated string
    variable. Initialize the lpszClassName member variable of your window application
    and pass it to the CreateWindowEx() function:


#include <windows.h>
//---------------------------------------------------------------------------
char StrClassName[] = "Win32Exercise";
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM
lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
HWND hWnd;
WNDCLASSEX WndClsEx;

...
WndClsEx.hbrBackground = (HBRUSH)(COLOR_ BACKGROUND + 1);

Free download pdf