1.2.13 Window Function

A window function processes all messages sent to a window in a given class. Windows sends messages to a window function when it receives input from the user that is intended for the given window, or when it needs information or the procedure to carry out some action on its window, such as painting in the client area.

A window function receives input messages from the keyboard, mouse, and timer. It receives requests for information, such as a request for the window title. It receives reports of changes made to the system by other windows, such as a change to the WIN.INI file. It receives messages that give it an opportunity to modify the standard system response to certain actions, such as an opportunity to adjust a menu before it is displayed. It receives requests to carry out some action on its window or client area, such as a request to update the client area. And a window function receives information about its status in relation to other windows, such as losing access to the keyboard or becoming the active window.

Most of the messages a window function receives are from Windows, but it can also receive messages from other windows, including windows it owns. These messages can be requests for information or notification that a given event has occurred within another window.

A window function continues to receive messages from the system and possibly other windows in the system until it, or the window function of a parent window, or the system destroys the window. Even in the process of being destroyed, the window function receives additional messages that give it the opportunity to carry out any clean-up tasks before terminating. But once the window is destroyed, no more messages are passed to the function for that particular window. If there is more than one window of the class, however, the window function continues to receive messages for the other windows until they, too, are destroyed.

A window function defines how a given window actually behaves; that is, it defines what response the window makes to commands from the user or system. The messages the window function receives from the system contain information that the function knows; for example, the user clicked the scroll bar or selected the Open command in the File menu, or double-clicked in the client area. The window function must examine these messages and determine what action, if any, to take. For example, if the user clicks the scroll bar, the window function may scroll the contents of the client area. Windows provides detailed information about what happens and provides some tools to carry out tasks, such as drawing and scrolling, but the window function must carry out the actual task.

A window function can also choose not to respond to a given message. If it does not respond, the function must give the system the opportunity to respond by passing the message to the DefWindowProc function. This function carries out default actions based on the given message and its parameters. Many messages, especially nonclient-area messages, must be processed, so the DefWindowProc function is required in all window functions.

A window function also receives messages that are really intended to be processed by the system. These messages, called nonclient-area messages, inform the function either that the user has carried out some action in a nonclient area of the window, such as clicking the title bar, or that some information about the window is required by the system to carry out an action, such as for moving or adjusting the size of the window. Although Windows passes these messages to the window function, the function should pass them to the DefWindowProc function and not attempt to process them. In any case, the window procedure must not ignore the message or return without passing it to DefWindowProc.

Window Messages

A window message is a set of values that Windows sends to a window function when it requests some action or informs the window of input. Every message consists of four values: a handle that identifies the window, a message identifier, a 16-bit message-specific value, and a 32-bit message-specific value. These values are passed as individual parameters to the window function. The window function then examines the message identifier to determine what response to make and how to interpret the 16- and 32-bit values.

Windows has a wide variety of messages that it or applications can send to a window function. Most messages are sent to a window as a result of a given function being executed or as input from the user.

To send a message to a window procedure, Windows expects the window function to have four parameters and use the Pascal calling convention. The following illustrates the window procedure syntax:

LONG FAR PASCAL WndProc(hWnd, wMsg, wParam, lParam)
HWND hWnd;
WORD wMsg;
WORD wParam;
DWORD lParam;

The hWnd parameter identifies the window receiving the message; the wMsg parameter is the message identifier; the wParam parameter is 16 bits of additional message-specific information; and lParam is 32 bits of additional infor-mation. The window procedure must return a 32-bit value that indicates the result of message processing. The possible return values depend on the actual message sent.

Windows expects to make an intersegment call to the window function, so the function must be declared with the FAR attribute. The window-function name must be exported by including it in an EXPORTS statement in the application's module-definition file.

Default Window Function

The DefWindowProc function is the default message processor for window functions that do not or cannot process some of the messages sent to them. For most window functions, the DefWindowProc function carries out most, if not all, processing of nonclient-area messages. Those are the messages that signify actions to be carried out on parts of the window other than the client area. Table 1.3 lists the messages DefWindowProc processes and the default actions for each:

Table 1.3 Default Actions for Messages

Message Default Action
WM_ACTIVATE Sets or kills the input focus.
WM_CANCELMODE Terminates internal processing of standard scroll bar input, terminates internal menu processing, and releases mouse capture.
WM_CLOSE Calls the DestroyWindow function.
WM_CTLCOLOR Sets the background and text color and returns a handle to the brush used to fill the control background.
WM_ERASEBKGND Fills the client area with the color and pattern specified by the class brush, if any.
WM_GETTEXT Copies the window title into a specified buffer.
WM_GETTEXTLENGTH Returns the length (in characters) of the window title.
WM_ICONERASEBKGND Fills the icon client area with the background brush of the parent window.
WM_NCACTIVATE Activates or deactivates the window and draws the icon or title bar to show the new state.
WM_NCCALCSIZE Computes the size of the client area.
WM_NCCREATE Initializes standard scroll bars, if any, and sets the default title for the window.
WM_NCDESTROY Frees any space internally allocated for the window title.
WM_NCHITTEST Determines what part of the window the mouse is in.
WM_NCLBUTTONDBLCLK Tests the given point to determine the location of the mouse and, if necessary, generates additional messages.
WM_NCLBUTTONDOWN Determines whether the left mouse button was pressed while the mouse was in the nonclient area of a window.
WM_NCLBUTTONUP Tests the given point to determine the location of the mouse and, if necessary, generates additional messages.

Table 1.3 Default Actions for Messages (continued)

Message Default Action
WM_NCMOUSEMOVE Tests the given point to determine the location of the mouse and, if necessary, generates additional messages.
WM_NCPAINT Paints the nonclient parts of the window.
WM_PAINT Validates the current update region, but does not paint the region.
WM_PAINTICON Draws the window class icon when a window is minimized.
WM_QUERYENDSESSION Returns TRUE.
WM_QUERYOPEN Returns TRUE.
WM_SETREDRAW Forces an immediate update of information about the clipping area of the complete window.
WM_SETTEXT Sets and displays the window title.
WM_SHOWWINDOW Opens or closes a window.
WM_SYSCHAR Generates a WM_SYSCOMMAND message for menu input.
WM_SYSCOMMAND Carries out the requested system command.
WM_SYSKEYDOWN Examines the given key and generates a WM_SYSCOMMAND message if the key is either TAB or ENTER.