The mapping mode defines how Windows maps logical coordinates that are specified in GDI functions to device coordinates, where the particular device coordinate system depends on the function you use to obtain the device context. To continue our discussion of the mapping mode, we need some additional terminology: The mapping mode is said to define the mapping of the ”window“ (logical coordinates) to the ”viewport“ (device coordinates).
The use of the words window and viewport is unfortunate. In other graphics interface languages, viewport often implies a clipping region. We've been using the word window to talk about the area that a program occupies on the screen. We'll have to put aside our preconceptions about these words during this discussion.
The ”viewport“ is in terms of device coordinates (pixels). Most often, the viewport is the same as the client area, but it can also refer to whole-window coordinates or screen coordinates if you've obtained a device context from GetWindowDC or CreateDC. The point (0, 0) is the upper left corner of the client area (or the whole window or the screen). Values of x increase to the right, and values of y increase going down.
The ”window“ is in terms of logical coordinates, which may be pixels, millimeters, inches, or any other unit you want. You specify logical window coordinates in the GDI functions.
For all mapping modes, Windows translates window (logical) coordinates to viewport (device) coordinates by the use of two formulas:
xViewExt
xViewport = (xWindow - xWinOrg) * -------- + xViewOrg
xWinExt
yViewExt
yViewport = (yWindow - yWinOrg) * -------- + yViewOrg
yWinExt
where (xWindow, yWindow) is a logical point to be translated, and (xViewport, yViewport) is the translated point in device coordinates. If the device coordinates are client-area coordinates or whole-window coordinates, then Windows must also translate these device coordinates to screen coordinates before drawing an object.
These formulas use two points that specify an ”origin“ of the window and the viewport: (xWinOrg, yWinOrg) is the window origin in logical coordinates; (xViewOrg, yViewOrg) is the viewport origin in device coordinates. In the default device context, these two points are set to (0, 0), but they can be changed. The formulas imply that the logical point (xWinOrg, yWinOrg) is always mapped to the device point (xViewOrg, yViewOrg).
The formulas also use two points that specify ”extents“: (xWinExt, yWinExt) is the window extent in logical coordinates; (xViewExt, yViewExt) is the viewport extent in device coordinates. In most mapping modes, the extents are implied by the mapping mode and cannot be changed. Each extent means nothing by itself, but the ratio of the viewport extent to the window extent is a scaling factor for converting logical units to device units. The extents can be negative: This implies that values on the logical x-axis don't necessarily have to increase to the right and that values on the logical y-axis don't necessarily have to increase going down.
Windows can also translate from viewport (device) coordinates to window (logical) coordinates:
xWinExt
xWindow = (xViewport - xViewOrg) * ------- + xWinOrg
xViewExt
yWinExt
yWindow = (yViewport - yViewOrg) * ------- + yWinOrg
yViewExt
Windows provides two functions that let you convert device points to logical points and vice versa within a program. The following function converts device points to logical points:
DPtoLP (hdc, lpPoints, nNumber) ;
The variable lpPoints is a long pointer to an array of POINT structures, and nNumber is the number of points to be converted. You'll find this function useful for converting the size of the client area obtained from GetClientRect (which is always in terms of device units) to logical coordinates:
GetClientRect (hwnd, &rect) ;
DPtoLP (hdc, (LPPOINT) &rect, 2) ;
This function converts logical points to device points:
LPtoDP (hdc, lpPoints, nNumber) ;