Messages and the Window Procedure

A window procedure is a function that receives and processes all messages sent to the window. The window procedure in the sample program at the beginning of this chapter was called the MainWndProc. Every window class has a window procedure, and every window created with that class initially uses the same window procedure to respond to messages. Although you can set the window procedure for an individual window after the window is created, this is a more advanced programming technique.

The system sends a message to a window procedure by passing the message data as arguments to the procedure. The window procedure then performs an appropriate action for the message; it checks the message identifier and, while processing the message, uses the information specified by the message parameters.

A window procedure rarely ignores a message. If it does not process a message, it should pass the message along for default processing. The window procedure does this by calling the DefWindowProc function, which performs a default action and returns a message result. The window procedure must then return this value as its own message result. Most window procedures process just a few messages and pass the others on to DefWindowProc.

Window procedures can be, and often are, shared by more that one window. The handle of the specific window receiving the message is available as an argument of the window procedure.