Windows and message handling. Part 1


Windows and message handling.

As far as you know, a window in an application written for the Microsoft Windows operating system is a rectangular area of the screen where the application displays output and receives input from the user. A window can be a target of messages – memory area that contains two parameters.

It’s easy to use windows for inter-thread communication.

Creating windows.

Each thread can create windows. The thread that creates a window must contain the code for its window procedure (a function that receives and processes all messages sent to the window) and for a message loop (a function that retrieves messages from the thread's message queue and dispatches them to the appropriate windows).

Messages.

An application can generate messages to direct its own windows to perform tasks.

A window procedure receives a set of four parameters: a window handle, a message identifier, and two 32-bit values called message parameters.

The window handle identifies the window for which the message is intended.

A message identifier is a named constant that identifies the purpose of a message. When a window procedure receives a message, it uses a message identifier to determine how to process the message. Message numbers in the range from WM_USER through 0x7FFF can be defined and used by an application to send private messages.

Message parameters can contain any application-defined data.

Sending a message.

There are two ways to place a message into message queue:

1. Posting a message.

The PostMessage() function places (posts) a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message. You should specify window handle, message identifier and two 32-bit parameters.

2. Sending a message.

The SendMessage() function sends the specified message to a window. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, Windows switches to that thread and calls the appropriate window procedure. The sending thread is blocked until the receiving thread processes the message.

Note: Messages are processed only by a thread that owns a target window.

Message loop.

To be able to process messages the thread code should contain message loop.

The simple message loop looks like following:

Repeat

{retrieve message from the queue}

{dispatch message to appropriate window procedure}

until false

The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure (TMSG).

You can modify this basic scheme in a variety of ways.

The copyright of the article Windows and message handling. Part 1 in Delphi Programming is owned by Lyapin Ilya. Permission to republish Windows and message handling. Part 1 in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic