2.3. Windows and Messages in Windows

A window is one of the most essential concepts in Windows, as most of the user interface controls from conventional windows to buttons and text fields are referred to as windows. A window is the basis of message passing in Windows as say, a console application having no window, cannot receive or react to messages at all.

A message is a relatively simple struct that's been defined in MSDN as follows (Microsoft, 1999, MSG):

typedef struct tagMSG {
    HWND   hwnd;
    UINT   message;
    WPARAM wParam;
    LPARAM lParam;
    DWORD  time;
    POINT  pt;
} MSG;
HWND is the winddow handle, that is a numeric identifier distinguishing the window receiving the message. Similarly, message is an unsigned integer defining the type of the message. The interpretation of the next two integer arguments is dependent on the message type but is usually some information on the state of the message or an address of a larger data-structure. The last two fields are the time the message was sent and the current mouse coordinates.

Windows also have a message-loop that delivers most of the messages from the application-specific message queue to the receiving window's message handler called the window procedure. However, according to Petzold some of the most important messages get directed straight to the window procedure without having to wait in the message queue (Petzold, 1998, A Window of One's Own). An example message loop follows:

while(GetMessage(&msg, NULL, 0, 0))
{
	TranslateMessage (&msg) ;
	DispatchMessage (&msg) ;
} // while

GetMessage fetches the message from the message queue and DispatchMessage delivers it to the receiving window procedure. TranslateMessage, on the other hand, converts raw key codes into Windows character codes.

Each window is of a given type called the Window Class (examples include Button and Edit). Notice that this is not really a class in the C plus plus sense as the Win32 API is C-based. The class defines the default data members for a window and the event handler (called a window procedure in the Windows jargon). The window procedure for a window is an application defined callback function that takes care of processing messages sent to a window. The program usually processes the messages by their type on a per window basis but calls the default event handler known as DefWindowProc for most of the messages. Here's an example indicating the common structure of a window procedure.

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case messagetype1 :
			// Handling.
			return 0;
			break;
		case messagetype2:
			// Handling for this type of message.
			return 0;
			break;
	} // switch
	return DefWindowProc (hwnd, message, wParam, lParam);
} // windowProcedure

Messages in Windows are essential when considering the inner-workings of a screen reader. A screen reader must access the message traffic passed between all windows in the system and it needs to be able to interpret the messages conrrectly to read the graphical user interface efficiently and reliably.

Back to the Contents