Window Procedure

From ReliCodia

Window procedure is a callback function. The Windows operating system calls this function whenever an event, such as a mouse-click, a keystroke, etc., occurs. In general, a windows program is driven by such events. The information about the event is encapsulated in a Window Message.

A separate window procedure should be written for each class of windows. The pointer to such procedure is registered with the system through Windows Class structure in the call to RegisterClassEx.

The signature of a window procedure must be this:

LRESULT CALLBACK MyProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

where hwnd is the handle to the window to which the message is directed; and message, wParam, and lParam, describe the Window Message

Usually, a windows procedure is implemented as a big switch statement keyed on the message parameter. Each case executes the code that processes a given message and returns the appropriate message-specific return code in the form of LRESULT. If a given message is not processed, default procedure should be called.

RSWL

In object-oriented programming, the use of a switch statements is discouraged in deference to polymorphic dispatching. So rather than letting every programmer implement the Window Procedure again and again, it is better to implement it once, in the library.

It is still a huge switch statement, but its purpose is to call, for each Window message, an appropriate method of the polymorphic object, the Window Controller. The parameters for each call are derived from the essentially typeless lParam and wParam and encapsulated into meaningful, message-specific, data structures.

For instance, instead of adding case WM_SIZE: to a Window procedure, and remembering that LOWORD (lParam) is the new width, HIWORD (lParam) the new height, and wParam a flag; the user of RSWL will override the virtual method:

bool OnSize (int width, int height, int flag);

defined in the base Window Controller class.