The attributes of a CWnd object are stored in two places, the window object and the Windows registration class. A Windows registration class is different from a C++ class. When Windows creates a window, it requires the name of a registration class. All windows created with a particular registration class share the attributes contained in that registration class.
The window classes of the Microsoft Foundation Class Library also require that the name of a Windows registration class be passed to the Create member function. Most of the time you will pass NULL for the registration class name to get the default registration class for the particular Foundation window class that you are using. You can use the name of a different Windows registration class if you want to change the attributes of the window.
In traditional Windows programs, you use the Windows function RegisterClass to create a registration class. In a Microsoft Foundation Class Library windows program it is often easier to use the Microsoft Foundation function AfxRegisterWndClass to create a registration class.
Although a Windows registration class has numerous fields, there are four key attributes of a registration class that you will typically want to change for a window class:
The class style
The default mouse cursor
The brush used to paint the background
The icon used to represent the minimized window
The Microsoft Foundation Class Library automatically creates Windows registration classes for its predefined window classes (CFrameWnd, CMDIFrameWnd, and CMDIChildWnd). When you derive your own classes from one of these base classes, you inherit the attributes defined for the base class.
For those situations where you want to change one of the four window class attributes listed above, the Foundation function AfxRegisterWndClass allows you to create a Windows registration class by specifying as arguments the class style, cursor, background brush, and icon. When you supply NULL as the value for any of the four arguments except the class style, you specify that you want the default value for that attribute. Thus, it is easy to change any combination of the four attributes.
·To register a Windows registration class and change its four class attributes:
Specify the arguments for class style, cursor, background brush, and icon. AfxRegisterWndClass generates a synthetic name (it can be different each time your program runs) and creates a Windows registration class that contains your attribute arguments. You can use the name that is returned as an argument to the Create member function in your derived window classes.
CString className = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW,
::LoadCursor( NULL,IDC_UPARROW ),
( COLOR_WINDOW+1 ),
NULL);
Note:
The scope resolution operator “::” is used without a class name in front of the LoadCursor function name in the example above to show that the name refers to the Windows function rather than any class member function with the same name.
·To pass on the attributes of a Windows registration class to CWnd::Create:
You can assign the name of the newly registered class to a CString and then pass the string to CWnd::Create to create a window that has the attributes defined for that Windows registration class.
// assign class name to intermediate CString
CString myClassName = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW,
::LoadCursor( NULL,IDC_UPARROW ),
( COLOR_WINDOW+1 ),
NULL);
CMyWnd* myWnd = new CMyWnd;
myWnd->Create( myClassName, ... );
Since AfxRegisterWndClass returns a pointer to the registration class name and checks for redundant registration, it can also be used inline as the first argument to CWnd::Create, as shown by the following example from BOUNCE.CPP in the MDI sample program:
// use class name directly
CMDIChildWnd::Create( AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW,
::LoadCursor( NULL,IDC_UPARROW ),
( COLOR_WINDOW+1 ),
NULL),
szTitle,
style,
rect,
parent );
}