1.2.7 Elements of a Window Class

The elements of the window class define the default behavior of the windows created from that class. The application that registers the window class assigns elements to the class by setting appropriate fields in a WNDCLASS data structure and passing the structure to the RegisterClass function. An application can retrieve information about a given window class with the GetClassInfo function.

Table 1.1 shows the window class elements:

Table 1.1 Window Class Elements

Element Purpose
Class name Distinguishes the class from other registered classes.
Window-function address Points to the function that processes all messages that are sent to windows in the class, and defines the behavior of the window.
Instance handle Identifies the application that registered the class.
Class cursor Defines the shape of the cursor when the cursor is in a window of the class.
Class menu Specifies the default menu used for any window in the class that does not explicitly define a menu.
Class styles Defines how to update the window after moving or resizing, how to process double-clicks of the mouse, how to allocate space for the display context, and other aspects of the window.
Class extra Specifies the amount of memory (in bytes) that Windows should reserve at the end of the class data structure.
Window extra Specifies the amount of memory (in bytes) that Windows should reserve at the end of any window structure an application creates with this class.

The following sections describe the elements of a window class and explain the default values for these elements if no explicit value is given when the class is registered.

Class Name

Every window class needs a class name. The class name distinguishes one class from another. An application assigns a class name to the class by setting the lpszClassName field of the WNDCLASS structure to the address of a null-terminated string that contains the name.

In the case of an application global class, the class name must be unique to distinguish it from other application global classes. If an application registers another application global class with the name of an existing application global class, the RegisterClass function returns FALSE, indicating failure. A conventional method for ensuring this uniqueness is to include the application name in the name of the application global class.

The class name must be unique among all the classes registered by an application. An application cannot register an application local class and an application global class with the same class name.

Window-Function Address

Every class needs a window-function address. The address defines the entry point of the window function that is used to process all messages for windows in the class. Windows passes messages to the function when it wants the window to carry out tasks, such as painting its client area or responding to input from the user. An application assigns a window function address by copying the address to the lpfnWndProc field of the WNDCLASS structure. The window function must be exported in the module-definition (.DEF) file.

For details about the window function, see Section 1.2.13, “Window Function.”

Instance Handle

Every window class needs an instance handle to identify the application that registered the class. As a multitasking system, Windows lets several applications run at the same time, so it needs instance handles to keep track of all applications. Windows assigns a unique handle to each copy of a running application.

Windows passes an instance handle to an application when the application first begins operation. The application assigns this instance handle to the class by copying it to the hInstance field of the WNDCLASS structure.

Class Cursor

The class cursor defines the shape of the cursor when the cursor is in the client area of a window in the class. Windows automatically sets the cursor to the given shape as soon as the cursor enters the window's client area, and ensures that the cursor keeps that shape while it remains in the client area. To assign a cursor shape to a window class, an application typically loads the shape from the application's resources by using the LoadCursor function, and then assigns the returned cursor handle to the hCursor field of the WNDCLASS structure.

Windows does not require a class cursor. If a class cursor is not defined, Windows assumes that the window will set the cursor shape each time the cursor moves into the window.

Class Icon

The class icon defines the shape of the icon used when the window of the given class is minimized. To assign an icon to a window class, an application typically loads the icon from the application's resources by using the LoadIcon function, and then assigns the returned icon handle to the hIcon field of the WNDCLASS structure.

Windows does not require a class icon. If a class icon is not defined, Windows assumes the application will draw the icon whenever the window is minimized. In this case, Windows sends appropriate messages to the window procedure, requesting that the icon be painted.

Class Background Brush

A class background brush is the brush used to prepare the client area of a window for subsequent drawing by the application. Windows uses the brush to fill the client area with a solid color or pattern, thereby removing all previous images from that location whether they belonged to the window or not.

To assign a background brush to a class, an application typically creates a brush by using the appropriate functions from GDI, and then assigns the returned brush handle to the hbrBackground field of the WNDCLASS structure.

Instead of creating a brush, an application can use a standard system color by
setting the field to one of the following color values:

COLOR_ACTIVECAPTION

COLOR_APPWORKSPACE

COLOR_BACKGROUND

COLOR_BTNFACE

COLOR_BTNSHADOW

COLOR_BTNTEXT

COLOR_CAPTIONTEXT

COLOR_GRAYTEXT

COLOR_HIGHLIGHT

COLOR_HIGHLIGHTTEXT

COLOR_INACTIVECAPTION

COLOR_MENU

COLOR_MENUTEXT

COLOR_SCROLLBAR

COLOR_WINDOW

COLOR_WINDOWFRAME

COLOR_WINDOWTEXT

To use a standard system color, the application must increase the background-color value by one. For example, COLOR_BACKGROUND + 1 is the system background color.

Class Menu

A class menu defines the default menu to be used by the windows in the class if no explicit menu is given when the windows are created. A menu is a list of commands that appears at the top of a window, under the title bar, from which a user can select actions for the application to carry out. To assign a menu to a class, an application sets the lpszMenuName field of the WNDCLASS structure to the address of a null-terminated string that contains the resource name of the menu. The menu is assumed to be a resource in the given application. Windows automatically loads the menu when it is needed. Note that if the menu resource is identified by an integer and not by a name, the lpszMenuName field can be set to that integer value by applying the MAKEINTRESOURCE macro before assigning the value.

Windows does not require a class menu. If a menu is not given, Windows assumes that the windows in the class have no menu bars. Even if no class menu is given, an application can still define a menu bar for a window when it creates the window.

Windows does not allow menu bars with child windows. If a menu is given and a child window is created using the class, the menu is ignored.