The elements of a window class define the default behavior of windows created from that class. The application that registers a window class assigns elements to the class by setting appropriate members in a WNDCLASS structure and passing the structure to the RegisterClass function. An application can retrieve information about a given window class with the GetClassInfo function. The window class elements are as follows:
Element | Purpose |
Class name | Distinguishes the class from other registered classes. |
Window-procedure 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 or DLL that registered the class. |
Class cursor | Defines the shape of the cursor when the cursor is in a window of the class. |
Class icon | Defines the shape of the icon Windows displays when a window belonging to the class is minimized. |
Class background brush | Defines the color and pattern Windows uses to fill the client area when the window is opened or painted. If this parameter is set to NULL, the window must paint its own background whenever it receives the WM_ERASEBKGND message. |
Class menu | Specifies the default menu used for any window belonging to 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 extra memory, in bytes, that Windows should reserve at the end of the WNDCLASS structure. Windows initializes this memory to zero. |
Window extra | Specifies the amount of extra memory, in bytes, that Windows should reserve at the end of any window structure an application creates that has this class. Windows initializes this memory to zero. |
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.
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 member of the WNDCLASS structure to the address of a null-terminated string that specifies 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 zero, indicating failure. The 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.
Every class needs a window-procedure address. The address defines the entry point of the window procedure that is used to process all messages for windows in the class. Windows passes messages to the procedure when it requires the window to carry out tasks, such as painting its client area or responding to input from the user. An application assigns a window-procedure to a class by copying its address to the lpfnWndProc member of the WNDCLASS structure. The window procedure must be exported in the module-definition (.DEF) file. For more information about exporting functions, see the Microsoft Windows Guide to Programming.
Every window class needs an instance handle to identify the application or DLL that registered the class. As a multitasking system, Windows lets several applications or DLLs run at the same time, so it needs instance handles to keep track of all applications and DLLs. Windows assigns a unique handle to each copy of a running application or DLL.
Multiple instances of the same application or DLL all use the same code segment, but each has its own data segment. Windows uses an instance handle to identify the data segment that corresponds to a particular instance of an application or DLL.
Windows passes an instance handle to an application or DLL when the application first begins operation. The application or DLL assigns this instance handle to the class by copying it to the hInstance member of the WNDCLASS structure.
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 a predefined cursor shape by using the LoadCursor function, and then assigns the returned cursor handle to the hCursor member of the WNDCLASS structure. Alternatively, you can use Microsoft Image Editor (IMAGEDIT.EXE) to create your own custom cursor, and use Microsoft Windows Resource Compiler (RC) to add the cursor as a resource to your application's executable file. The application can then use the LoadCursor function to load the custom cursor from the application's resources.
Windows does not require a class cursor. If an application sets the hCursor member of the WNDCLASS structure to NULL, a class cursor is not defined. Windows assumes that the window will set the cursor shape each time the cur- sor moves into the window. A window can set the cursor shape by calling the SetCursor function whenever the window receives the WM_MOUSEMOVE message.
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 typi-cally loads the icon from the application's resources by using the LoadIcon function, and then assigns the returned icon handle to the hIcon member of the WNDCLASS structure.
Windows does not require that a window class have a class icon. If an application sets the hIcon member of the WNDCLASS structure to NULL, a class icon is not defined. In this case, Windows sends the WM_ICONERASEBKGND message to a window of the class whenever the window must paint the background of the icon. If the window does not process the WM_ICONERASEBKGND message, Windows draws an image of the contents of the window's client area onto the icon when it is minimized.
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 pre-vious images from that location whether they belonged to the window or not.
Windows notifies a window that its background needs to be painted by sending
the WM_ERASEBKGND message to the window.
To assign a background brush to a class, an application can create a brush by using the appropriate functions from the graphics device interface (GDI) and then assign the returned brush handle to the hbrBackground member of the WNDCLASS structure.
Instead of creating a brush, an application can use a standard system color by setting the hbrBackground member to one of the standard system color values. For a list of the standard system color values, see the description of the SetSysColors function in the Microsoft Windows Programmer's Reference, Volume 2.
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.
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 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 member of the WNDCLASS structure to the address of a null-terminated string that specifies 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 application can set the lpszMenuName member to that integer value by applying the MAKEINTRESOURCE macro before assigning the value.
Windows does not require a class menu. If an application sets the lpszMenuName member of the WNDCLASS structure to NULL, 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 for a class and a child window of that class is created, the menu is ignored. For more information about menus, see Section 1.2.19, “Menus.”