Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

How the Windows Message System Works

A Windows application's message system has three key components:

  • Message queue—Windows maintains a message queue for each application. A Windows application must get messages from this queue and dispatch them to the proper windows.

  • Message loop—This is the loop mechanism in a Windows program that fetches a message from the application queue and dispatches it to the appropriate window, fetches the next message, dispatches it to the appropriate window, and so on.

  • Window procedure—Each window in your application has a window procedure that receives each of the messages passed to it by the message loop. The window procedure's job is to take each window message and respond to it accordingly. A window procedure is a callback function; a window procedure usually returns a value to Windows after processing a message.

NOTE

A callback function is a function in your program that's called by Windows or some other external module.

Getting a message from point A (some event occurs, creating a message) to point B (a window in your application responds to the message) is a five-step process:

  1. Some event occurs in the system.

  2. Windows translates this event into a message and places it into the message queue for your application.

  3. Your application retrieves the message from the queue and places it in a TMsg record.

  4. Your application passes on the message to the window procedure of the appropriate window in your application.

  5. The window procedure performs some action in response to the message.

Steps 3 and 4 make up the application's message loop. The message loop is often considered the heart of a Windows program because it's the facility that enables your program to respond to external events. The message loop spends its whole life fetching messages from the application queue and passing them to the appropriate windows in your application. If there are no messages in your application's queue, Windows allows other applications to process their messages. Figure 3.1 shows these steps.

Figure 3.1 The Windows Message system.

  • + Share This
  • 🔖 Save To Your Account