Message-Driven Architecture

The first time I saw a graphical user interface in action, I was puzzled. The demonstration included a rudimentary word processor running in a window. The word processor would reformat its text when the program's window was resized.

It was obvious to me that the operating system was handling the details of the window-resizing logic, and that the program was capable of responding to this system function. How did the program know when its window was resized? What was the mechanism the operating system used to convey this information to the window? My previous programming experience was useless in understanding how this worked.

It turns out that the answer to this question is central to understanding the architecture used in graphical user interfaces. In Windows, when a user resizes a window, Windows sends a message to the program indicating the new window size. The program can then adjust the contents of its window to reflect the new size.

”Windows sends a message to the program.“ I hope you didn't read that statement without blinking. What on earth could it mean? We're talking about program code here, not an electronic mail system. How can an operating system send a message to a program?

When I say that ”Windows sends a message to a program,“ I mean that Windows calls a function within the program. The parameters to this function describe the particular message. This function located in your Windows program is known as the ”window procedure.“