Elements of a Window Class and the WNDCLASSEX struct
Window Classes – Part 2
Volume - Windows User Interface
Forward: In this part of the series, we look at elements of a window class.
By: Chrysanthus Date Published: 29 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Characteristics of a Window
Elements of a window class are the characteristics of a window. These are the items for the WNDCLASSEX struct. Remember, you type the characteristics of a window class in the WNDCLASSEX struct. You do not type them in any way. We see how to type them as we go along.
Elements of a window class define the default behavior of windows belonging to the class.
List and Description of the Elements
There are 10 of these elements. However, you need 12 members of the struct WNDCLASSEX to indicate the elements. Here are the 10 elements and their descriptions.
Class Name:
A class needs a class name to distinguish it from other classes.
Window Procedure Address:
A class has a procedure. This procedure is used for all windows of the class. So the address (pointer) of the procedure is considered as a class element.
Instance Handle
We talked about this in the previous series. The Instance handle is a handle that identifies the instance of the application or library (.dll) that registered the class.
Class Cursor
The class cursor element defines the shape of the mouse (mouse cursor) that the operating system displays when the mouse is over a window of the class.
Class Icons
An icon is like a logo for an application. If you open a directory you will see application icons and their names. When an application is running, you see its icon at the left of the title bar. A Windows application has two icons of the same design: a large one and a small one. The Class Icons element defines the large and the small icons. The WNDCLASSEX has one member for the large icon and another member for the small icon.
Class Background Brush
This element defines the color and pattern that fill the client area when the window is opened or painted. Consider a brush here as a physical painting brush that has a pattern and at the same time has a color on it (that never finishes).
Class Menu
This element defines a menu (menu bar) for the windows of the class.
Class Styles
This element defines how to update (re-present) the window after moving or resizing it, how to process double-clicks of the mouse, how to allocate space for the device context (see later), and other aspects of the window.
Extra Class Memory
Under normal circumstances when an application starts, the operating system gives it an amount of memory. If you think your application would need extra memory, you can ask for that. This element specifies the amount of extra memory, in bytes, that the operating system should reserve for the class. All windows in the class share the extra memory and can use it for any application-defined purpose. The operating system initializes this memory to zero.
Extra Window Memory
This element specifies the amount of extra memory, in bytes, that the operating system should reserve for each window belonging to the class. The extra memory can be used for any application-defined purpose. The operating system initializes this memory to zero. The above is extra memory for the class that is used by all windows of the class. This one is for each window and each window uses it in its own way.
You would normally declare a WNDCLASSEX struct as follows (in WinMain):
WNDCLASSEX wcx;
wcx is the identifier, for the struct, which can be any name of your choice. After this you assign values to the wcx struct as you would for C++ structs.
There are 12 members of the WNDCLASSEX struct. Apart from the first member of the WNDCLASSEX struct, the rest of the members are the class elements. In the struct two members are used for the class icons: one member for the large icon and the other member for the small icon. In this way you have 12 members for the struct and not 10.
Members of the WNDCLASSEX struct
The syntax for the WNDCLASSEX struct is:
typedef struct tagWNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
HICON hIconSm;
} WNDCLASSEX, *PWNDCLASSEX;
In the syntax, on the left column, you have the data type and on the right column you have the corresponding identifiers for the struct members.
This is the size in bytes of the WNDCLASSEX struct structure itself. You should set this number to sizeof(WNDCLASSEX), where sizeof is a predefined function that will determine the size for you.
style (UINT)
This member is for the class styles. I will address this in the next part of this series.
lpfnWndProc (WNDPROC)
This is a pointer to the window class procedure. The name of the procedure is a pointer.
cbClsExtra (int)
This is a number in bytes to allocate for the extra class memory (of the class). The system initializes this number to zero.
cbWndExtra (int)
This is a number in bytes to allocate for the extra window memory (of the window instance). The system initializes this number to zero.
hInstance (HINSTANCE)
This is a handle to the instance that contains the window procedure for the class.
hIcon (HICON)
I will address this member in an independent series om icons. It is for the large icon. For now set it to NULL.
hCursor (HCURSOR)
An example value for this member, is the expression, LoadCursor(NULL, IDC_ARROW). I will address this expression and struct member toward the end of this series.
hbrBackground (HBRUSH)
This is a handle to the class background brush, to paint the client area of the window. An example of this member’s value is, (HBRUSH)(COLOR_BACKGROUND+1). Note the use of the two parentheses. The first parenthesis will always have, HBRUSH. The next parenthesis has the identifier for the color plus 1 attached to it. The color identifier can be from one of the following standard system colors:
COLOR_ACTIVEBORDER
COLOR_ACTIVECAPTION
COLOR_APPWORKSPACE
COLOR_BACKGROUND
COLOR_BTNFACE
COLOR_BTNSHADOW
COLOR_BTNTEXT
COLOR_CAPTIONTEXT
COLOR_GRAYTEXT
COLOR_HIGHLIGHT
COLOR_HIGHLIGHTTEXT
COLOR_INACTIVEBORDER
COLOR_INACTIVECAPTION
COLOR_MENU
COLOR_MENUTEXT
COLOR_SCROLLBAR
COLOR_WINDOW
COLOR_WINDOWFRAME
COLOR_WINDOWTEXT
This is a pointer to a string that specifies the resource name of the class menu, as the name appears in the resource file. I will address this in a different series. For now set it to NULL.
lpszClassName (LPCTSTR)
The value here is the class name in a double quotes string.
hIconSm (HICON)
I will address this member in an independent series on icons. It is for the small icon. For now set it to NULL.
A coding example for the WNDCLASSEX struct is as follows:
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "MainWClass";
wcx.hIconSm = NULL;
RegisterClassEx(&wcx);
No icon is provided for this particular class. The operating system will give you a default icon, which you probably will not like. We shall solve this problem in a different series. After deciding on a class, you have to register it. That is what the last statement in the coding example does. All the above coding should be done in WinMain.
That is enough stuff for now. We take a break here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT