Drawing units and mapping modes

Drawing units

Device units are the individual display points on the output device, i.e. pixels on a screen, or dots on a printer. The resolution of a device can be obtained from a Device Context. For example:

GetDeviceCaps(LOGPIXELSX/Y) returns the number of device units per inch in the x or y directions, e.g. 600 dots per inch for a printer. The screen scaling is fixed at either 96 pixels per inch if "small fonts" are selected, or 120 pixels per inch if "large fonts" are selected.

GetDeviceCaps(HORZ/VERTRES) returns the total number of device units in the x or y directions, i.e. the total usable area of the screen or printed page.

Logical units are arbitrary units (which can be "real world" units such as millimetres), which are mapped to device units according to the current mapping mode.

One point is 1/72 inch.

One twip is a twentieth of a point, or 1/1440 inch. Windows often uses this unit for device independent measurements: if a unit isn't mentioned in the documentation, it's often a twip.

Design units are used for font sizes. For example, 16 point text on a 96 pixel per inch screen has a height (in design units) of 16/72 x 96 = 21 pixels.

Dialog units are used in dialogue box templates. One vertical dialog unit is 1/8 of the height of the dialogue box font. One horizontal dialog unit is 1/4 of the average width of the dialogue box font.

Mapping modes

Drawing using a Device Context (i.e. co-ordinates and sizes) uses logical units. These drawing commands are then scaled and offset to fit the actual display points (i.e. the device units) on the output device - either a Windows window, or a printed page.

The device co-ordinate system always has the origin in the top left corner, and the y-axis moving down the screen or page:

Offset

The origin of the logical co-ordinate system can be set using:

SetWindowOrg: which specifies the location of the device origin in logical co-ordinates.


Device co-ordinates

Logical co-ordinates

or:

SetViewportOrg: which specifies the location of the logical origin in device co-ordinates.


Device co-ordinates

Logical co-ordinates

Scaling

The scaling between logical and device units is firstly defined by the mapping mode, set using the SetMapMode function. The default mapping mode is MM_TEXT, which means each logical unit corresponds to one device unit, i.e. one pixel. Another fixed scaling mode is MM_HIMETRIC, where each logical unit corresponds to 0.01 millimetres; remember there is a fixed relationship defining the number of device units per inch. The y-axis is also reversed, to move up the page.

A mapping mode such as MM_ANISOTROPIC allows an arbitrary scaling to be defined, by first calling SetWindowExt to set the size of a "window" in logical units, and then calling SetViewportExt to set the size of a "viewport" in device units. The window and viewport are deemed to be identical in size, and therefore specify the relative scaling in the x and y directions:


Window (logical units)

Viewport (device units)

Note that the "window" and "viewport" have no relationship to a Windows window nor to the physical output device, and have no effect on the clipping area.