Broad Network


Basics of Window Classes

Getting to know Windows – Part 3

Volume - Windows User Interface

Forward: In this part of the series we look at the basics of window classes and the basics of the window procedure.

By: Chrysanthus Date Published: 28 Aug 2012

Introduction

This is part 3 of my series, Getting to know Windows. I assume that you have read the previous part of the series before reaching here. You should be reading the tutorials in the order given. In this part of the series we look at the basics of window classes and the basics of the window procedure.

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.

What is a Window Class?
Anybody who is computer literate today has come across many different types of windows. Well, as I said, you, the reader of these volumes should be more than computer literate. You should at least know the basics of one high-level language, in particular, C++. If you do not know the basics of C++, just search this (my) blog.

The different windows you have seen have different characteristics. These characteristics are called Attributes. A set of these attributes determines a windows appearance and behavior. A set of these attributes is called a Window Class. Many windows can have the same set of attributes (characteristics). In other words, many windows can have the same class.

The Window Class and Procedure
A window class has an associated procedure (function). This procedure is the same procedure we talk about in the previous part of the series.

You can send information to an application using the keyboard or mouse or some other input device. So you would have one procedure that will process such messages for a group of windows as long as these windows belong to the same class (have the same set of declared attributes).

Class Name
A class is a set of characteristics (attributes) for a window. More than one window can belong to the same class. All the windows of the same class have one procedure (function). This set of characteristics has to be given a name, and the name given is the class name. Different classes (set of attributes) have to be given different names. The name of a class should be a short string.

Instance Handle
It is possible to have one application and then open it twice. When that is done, you see two of the application buttons at the task bar. You can use a word processor to demonstrate this. I have Microsoft Word as my word processor. I have just used the Start>>All programs>>etc. to click Microsoft Word twice. The first time I clicked Microsoft Word, the application opened. I followed the same sequence (Start>>All programs>>etc) again and clicked Microsoft Word the second time. The application (Microsoft Word) opened again. So two instances of the same application have been opened and are running. Two of the application buttons are seen at the taskbar. Such instances have to be identified. An instance is identified by what is known as a handle (pointer).

The WNDCLASSEX Structure
The User Interface of the Windows API has a struct object called, WNDCLASSEX. This is a struct object similar to the struct object in C++. It has 12 members (attributes - properties). These members are identifiers for 12 different types of attributes (characteristics) that a window class can have. The value assigned to each member gives you a particular characteristic (attribute) for the window. Different values can be given to the same member in different occasions.

Remember, more than one window can have the same class. So, all windows of a class have the same characteristics and all have one procedure. Note: the procedure is considered as a member of the class.

These 12 members can be set to NULL, except three. The three members are the class name, a pointer to the class procedure, and the instance handle of the application. These three features do not look like window characteristics, but they are considered as window characteristics. You may not know this, but a C++ function can have a pointer pointing to it, and be called using the pointer. In this light a pointer can point to the procedure for all the windows of a class. If you do not want any value for any of the other 9 members of the WNDCLASSEX struct, then assign NULL to it. In that case, the default value would be chosen for the windows, for the characteristic with NULL value.

The WNDCLASSEX struct is already created by the operating system. You just have to use it. We shall see more about the WNDCLASSEX struct later.

Registering a Class
Before a window is created, the operating system has to be informed that a class (set of window characteristics) is about to be used for creating the window. This is called registering the class. There is a function called, the RegisterClassEx function. It is used by an application to register the class. We shall see more about it later.

After registering the class, the application can create more than one window of that class.

Other High-Level Languages
The Windows API has been written using the C++ language format. It is like a language on its own. You need to learn the Windows API language. So, you need to learn its data types, built in functions and structs. You can use C++ to write the core of your application (word processor, game, etc). You do not use the Windows API to write the core of your application. Windows API stands between your application proper and the computer hardware and facilitates communication and does interpretation between the two.  However, your application core does not only have to be written in C++. You can use another high-level language like Visual-Basic-Dot-Net or Java or Borland Delphi to write your application core. There are other high-level languages you can use. Just search the Internet to know which ones work with Windows API. Windows API is part of your application but it is not the core of your application.

What is a Procedure?
We have seen above that a class is a set of characteristics (attributes) for a window. Many windows can have the same set of characteristics. This means many windows can belong to the same class. A procedure belongs to a class and many windows if the windows are of that class.

At this point, you should know what is called a function in a high-level language like C++. A function has parameters and has a return value. Here, a procedure has four parameters and one returned value. When messages are sent to a window, it is the procedure that processes the messages, and then the result takes effect at the window.

Procedure Prototype
A prototype is a function declaration, showing the function parameters, preceded by the returned value and ending with a semicolon. The procedure prototype is:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

So many things look strange here. Let me simplify them (you will have full details later). I said above, that Windows API is like a language that you have to learn. In the parentheses for the parameters of this prototype, hwnd is a data identifier and HWND is the corresponding data type; uMsg is a data identifier and UINT is the data type; wParam is a data identifier and WPARAM is the data type; lParam is a data identifier and LPARAM is the data type. LRESULT is the returned type for the procedure. CALLBACK is a modifier for LRESULT; so you can say LRESULT CALLBACK forms the complete returned type.

Each window has a handle (pointer), which you (your code) can use to do something on the window. HWND is the data type for a window’s handle.

A message comes in the form of integers (not a string). An integer in the message has a meaning, which can be expressed as text (a string). The second parameter data type, UINT, means Unsigned (positive) Integer. It identifies the message. The core message itself comes as the WPARAM parameter and as the LPARAM parameter. WPARAM is an integer and LPARAM is also an integer. We shall see details later. For now, know that the last three parameters in the parentheses contain the message in integers. While the core message is in the WPARAM and LPARAM data type parameters, UINT identifies the core message. The values of WPARAM and LPARAM depend on the value of UINT.

LRESULT is a singed (positive or negative) number. This return value is the result of the message processing and depends on the message received by the procedure.

You have to write code in the block of the procedure that will process the message received. We shall see details later.

Default Window Procedure
The windows operating system has a default procedure called, DefWindowProc. This default procedure defines certain fundamental behavior shared by all windows. The default window procedure provides the minimal functionality for a window. An application-defined window procedure (like the one we talk above) should pass any messages that it does not process to the DefWindowProc procedure for default processing.

What you need to note from all what has been said above is, that the application provides a procedure for a class of windows and this procedure should call the default procedure for what you should not waste your time coding, since the default procedure already has basic coding to process messages.

We are at the end of this part of the series. You want to see coding? Just be patient; we are gradually coming to that. Windows API (User Interface) is not a straightforward thing to learn. That is why I title this series, “Getting to know Windows”. Many people have tried to learn Windows API and have not succeeded because it is not straightforward, the way it is officially presented.

The User Interface has many new principles to learn first. After that you combine them and then you can continue to learn in different directions. That is the approach I have taken in order to simplify things. That is why I have the different divisions (series) of which, this is the first everybody should go through. In this way, I hope, every serious would be coder (learning from my source) will easily understand Windows API. So just be patient; we shall soon get there; we have to get there methodically (so that you understand).

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message