The mapping mode governs how Windows translates logical coordinates into device coordinates within the current device context. Logical coordinates represent the graphics and text application values and the device coordinates are the resulting pixel positions within a window. The mapping mode also determines the orientation of the X-axis and the Y-axis and whether the values of X and Y increase or decrease with respect to the origin. The default device context sets logical units the same as pixels with the X-axis being right positive, the Y-axis being positive down, and sets the coordinate origin to 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 unit of measure used to transform logical coordinates to device coordinates is user-defined. The MM_ISOTROPIC and MM_ANISOTROPIC mapping modes differ from each other in that with the former, the range of the x-axis and y-axis must be the same, and with the latter, the range of the x-axis and y-axis can be different. Selecting either of these modes means the developer will need to 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 viewpoint onto these logical coordinates use the SetViewportExtEx() member 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, a device context’s origin, regardless of the mapping mode, is in the upper left corner of the display. This device context’s origin can be changed using API functions SetWindowOrgEx() and SetViewportOrgEx(). The former moves the window's origin, the latter the viewport origin. The prototype for these functions is
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.