Mapping Modes


Although the enumeration Unit documents the available mapping modes, further elaboration is in order. The mapping mode is a device context attribute that affects virtually all drawing that can be performed on the client area of a window. Closely related to the mapping mode are the device context attributes of

Most functions that output to a device context require the specification of coordinates. For example, the function TextOut requires a vector to position the text to be rendered (or it draws text at the current position). The point supplied to position the text is relative to the logical, two dimensional coordinate system in effect. When rendering graphics, the operating system translates logical coordinates into device coordinates. The transformation that is performed is governed by:

currently in effect. The mapping mode determines the orientation of the x and y axes. The enumeration Unit contains eight mapping modes, including the mapping mode Unit::Twips. The expression twip comes from the phrase "twientieth of a point". A point is a typographic value representing 1/72 of an inch. Therefore, 1 twip = 1/1440 inches. The mapping mode may be set and queried (respectively) by the functions:

The default mapping mode is Unit::Pixel. In this mode, logical units map directly to pixels (starting at the top, left corner of the window and proceeding to the right for positive x-values and down for positive y-values).

The Device Coordinate System

Although most of the measurements that have been discussed lie within the client area of a window, the operating system uses two other coordinate systems. All device coordinate systems are expressed in terms of pixels. When reference is made to the entire area of the display, the coordinate system is referred to as screen coordinates, where, the upper left corner of the screen is the origin (0,0). When dealing with main (non-child) windows, screen coordinates are used for the message Message::Move. Screen coordinates are also used for creating and moving main windows. Other functions that make use of screen coordinates are:

Whole window coordinates refer to coordinates relative to the actual origin of the window rather than the origin of the client portion of the window. This is usually the upper-left point of the sizing border of the window. The function GetWindowDeviceContext yields a device context whose logical coordinate system maps to whole window coordinates.

The third coordinate system is the client coordinate system. Logical coordinates are mapped to client coordinates when a device context is obtained through:

Client coordinates may be converted to screen coordinates and vice-versa via the C functions:

The Window and the Viewport

The following functions are applicable to changing window and viewport extents:

The unit or mapping mode defines how windows maps logical coordinates to device coordinates. The window coordinate system is specified in logical coordinates whereas the viewport coordinate system is measured in device coordinates. For all mapping modes of a device context, logical (window) coordinates are mapped to device (viewport) coordinates according to the following formulae:

xViewport = (xWindow - xWindowOrigin) * xViewportExtent/xWindowExtent + xViewportOrigin
yViewport = (yWindow - yWindowOrigin) * yViewportExtent/yWindowExtent + yViewportOrigin

where:

(xWindow,yWindow) is the point being mapped,
(xWindowOrigin,yWindowOrigin) is the window origin in logical coordinates,
(xWindowExtent,yWindowExtent) is the window extent in logical coordinates,
(xViewportOrigin,yViewportOrigin) is the viewport origin in device coordinates and
(xViewportExtent,yViewportExtent) is the viewport extent in device coordinates.

Each of the extents has no meaning in itself; however, the ratios of the window extents to the viewport extents provide the scaling factors when performing the conversion of logical units to device units and vice-versa. The extents may be negative. In most mapping modes, the extents are implied by the mapping mode. The formulae for translating viewport (device) coordinates to window (logical) coordinates are shown below.

xWindow = (xViewport - xViewportOrigin) * xWindowExtent/xViewportExtent + xWindowOrigin
yWindow = (yViewport - yViewportOrigin) * yWindowExtent/yViewportExtent + yWindowOrigin

The function DevicePointsToLogicalPoints may be used to convert device points to logical points; whereas, the function LogicalPointsToDevicePoints converts logical points to device points.

The Mapping Mode Unit::Pixel

The mode Unit::Pixel is such that the programmer may interpret coordinates as pixels relative to the upper-left corner of the window. Other unit modes have their origin as the upper-left corner of the window; however, positive y-coordinates extend upwards instead of downwards (which yields a right handed coordinate system). In the case of Unit::Pixel, the mapping from logical to device coordinates reduces to the following equations.

xViewport = xWindow - xWindowOrigin + xViewportOrigin
yViewport = yWindow - yWindowOrigin + yViewportOrigin

The default origins and extents are shown in the table below.

Entity Default Changeability
Window Origin (0,0) Can be changed.
Window Extent (1,1) Cannot be changed.
Viewport Origin (0,0) Can be changed.
Viewport Extent (1,1) Cannot be changed.

The Metric Mapping Modes

Five of the available mapping modes map logical coordinates to fixed physical measurements. These are: Unit::LowMetric, Unit::HighMetric, Unit::LowEnglish, Unit::HighEnglish and Unit::Twips. For each of these units, the default viewport origin and the default window origin is (0,0), which may be adjusted. The window and viewport extents are calculated to obtain the correct mapping of logical to device units and are based on the aspect ratio and resolution of the device. Clearly, these extents may not be changed.

For all units apart from Unit::Pixel, coordinates within the client window have negative y values; although, the functions SetViewportOrigin and SetWindowOrigin may be used to change this. This fact is illustrated by the snapshot (below) of the program of this section running. Irrespective of what coordinate system is in effect, windows uses device coordinates for all messages - such as: Message::Move, Message::Size and Message::MouseMove.

All non-graphics functions and some graphics functions also use device coordinates. Each function in the reference documents whether the coordinates are device coordinates or logical coordinates.

Unit::Isotropic and Unit::Anisotropic

The only unit modes for which the viewport and window extents can be changed are Unit::Isotropic and Unit::Anisotropic. These unit modes allow the physical size of a logical unit to be adjusted - whereas the other unit modes do not. Isotropic coordinates are such that equal logical coordinates on the x and y axes represent the same physical distance. This is partially constrained in the sense that the operating system adjusts the viewport and window extents such that mapping from logical to physical coordinates yields equal physical values for both directions. Mapping mode Unit::Anisotropic is unconstrained in the sense that the viewport and window extents may be altered without the operating system making other adjustments. All other unit modes are fully constrained and the window and viewport extents may not be adjusted.

What Size is the Window

The program of this section demonstrates the various mapping modes applicable to drawing in a window. The output of the program is shown below.