The mapping mode governs how Windows translates logical coordinates into device coordinates within the current device context. Logical coordinates represent the graphics and text values used by an application, while device coordinates represent the resulting pixel positions within a window.
The mapping mode also determines the orientation of the X-axis and Y-axis, and whether the values of X and Y increase or decrease relative to the origin.
In the default device context, logical units are the same as pixels. The X-axis increases to the right, the Y-axis increases downward, and the coordinate origin is located at the upper-left corner of the window.
The different mapping modes are listed below
| Mapping Mode | Logical Unit | x-axis and y-axis |
| MM_TEXT | Pixel | Positive x is to the right; positive y is down |
| MM_LOMETRIC | 0.1 mm | Positive x is to the right; positive y is up. |
| MM_HIMETRIC | 0.01 mm | Positive x is to the right; positive y is up. |
| MM_LOENGLISH | 0.01 in | Positive x is to the right; positive y is up. |
| MM_HIENGLISH | 0.001 in | Positive x is to the right; positive y is up. |
| MM_TWIPS | 1/1440 in | Positive x is to the right; positive y is up. |
| MM_ISOTROPIC | user-specified | user-specified |
| MM_ANISOTROPIC | user-specified | user-specified |
To select a different mapping mode, use the function SetMapMode()
int SetMapMode(HDC hdc,int iMode);
where
hdc – A handle to the device context.
iMode – The new mapping mode.
If the function succeeds, the return value is the previous mapping mode. If the function fails, the return value is zero.
Programmable Mapping Modes
The MM_ISOTROPIC and MM_ANISOTROPIC mapping modes differ from the other mapping modes in that the units used to transform logical coordinates into device coordinates are user-defined.
These two mapping modes also differ from each other. With MM_ISOTROPIC, the scaling of the x-axis and y-axis must be equal so that the units are the same in both directions. With MM_ANISOTROPIC, the scaling of the x-axis and y-axis can be different, allowing independent scaling in each direction.
Selecting either of these modes requires the developer to explicitly set the window dimensions.
To set the logical extents of the window associated with the device context, use the SetWindowExtEx function. To map the corresponding device size, known as the viewport, onto these logical coordinates, use the SetViewportExtEx function.
SetWindowExtEx
Sets the horizontal and vertical extents of the window for a device context by using the specified values.
BOOL SetWindowExtEx( HDC hdc, int x, int y, LPSIZE lpsz );
hdc – A handle to the device context.
x – The window’s horizontal extent in logical units.
y – The window’s vertical extent in logical units.
lpsz – A pointer to a SIZE structure that receives the previous window extents, in logical units. If lpSize is NULL, this parameter is not used.
If the function succeeds, the return value is nonzero. Otherwise, the return value is zero.
SetViewportExtEx
Sets the horizontal and vertical extents of the viewport for a device context by using the specified values.
BOOL SetViewportExtEx( HDC hdc, int x, int y,LPSIZE lpsz);
hdc – A handle to the device context.
x – The horizontal extent, in device units, of the viewport.
y – The vertical extent, in device units, of the viewport.
lpsz – A pointer to a SIZE structure that receives the previous viewport extents, in device units. If lpSize is NULL, this parameter is not used.
If the function succeeds, the return value is nonzero. If the function fails the return value is zero.
Moving the Origin
By default, the origin of a device context, regardless of the mapping mode, is located in the upper-left corner of the display. This origin can be changed using the API functions SetWindowOrgEx and SetViewportOrgEx.
The former changes the window origin, while the latter changes the viewport origin. The prototypes for these functions are as follows:
BOOL SetWindowOrgEx(HDC hdc,int x,int y,LPPOINT lppt); BOOL SetViewportOrgEx( HDC hdc, int x, int y, LPPOINT lppt);
hdc – A handle to the device context.
x – The x-coordinate of the new viewport origin.
y – The y-coordinate of the new viewport origin.
lppt – A pointer to a POINT structure that receives the previous viewport origin, in device coordinates. If lpPoint is NULL, this parameter is not used.
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
Example
The following short program draws 5 squares under different mappings to illustrate the different display characteristics of each Mapping Mode in Windows.
