SAMPLE: Aligning the Logical and Physical Coordinate Systems

Last reviewed: February 15, 1996
Article ID: Q39090
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

The text below explains how to use the following functions in the Microsoft Windows operating system: SetViewportExt(), SetViewportOrg(), SetWindowExt(), and SetWindowOrg(). The explanation is based on the SHAPES sample application that was provided with the Microsoft Windows SDK versions 2.x. A version of this sample for Windows 3.0 and later is available in the Microsoft Software Library.

Download SHAPES.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download SHAPES.EXE (size: 21675 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get SHAPES.EXE (size: 21675 bytes) 
    

MORE INFORMATION

An application uses the SetViewportExt(), SetViewportOrg(), SetWindowExt(), and SetWindowOrg() to align the two separate coordinate systems that the Windows graphics device interface (GDI) provides. SetWindowOrg() and SetViewportOrg() provide a one-to-one mapping of the two specified origin points (one in each coordinate system). The SetWindowExt() and SetViewportExt() functions instruct GDI how much to stretch (or compress) the coordinate system after mapping the two specified points to each other.

The following code, drawn from the SHAPES sample, maps point (-10, 110) in the logical coordinate system to point (0, 0) in the client coordinate system. Then the code sets the width of the viewport to 120 logical units and the height of the viewport to -120 logical units.

   RECT ClientRect;

   GetClientRect(hDC, ClientRect);
   SetWindowOrg(hDC, -10, 110);
   SetViewportOrg(hDC, 0, 0);
   SetWindowExt(hDC, 120, -120);
   SetViewportExt(hDC,
                  (short)(ClientRect.right - ClientRect.left),
                  (short)(ClientRect.bottom - ClientRect.top));

Negative coordinate scaling is required because the specified origin point is above the displayed image. These concepts are perhaps best illustrated on graph paper. Consider the following example. In the logical coordinate system, an image of a star starts in the upper-left quadrant of the coordinate system, between (-10, 110) and (110, -10). Mark the specified origin (-10, 110) on the graph paper. To include the image of the star in the viewport, the viewport "extent" must include 120 units to the right (positive x direction) and 120 units down (negative y direction). The specified viewport extents provide the required viewport.

In the example, the left and top members of the ClientRect data structure are always zero. The subtraction is unnecessary but included to demonstrate how the application aligns the rectangles in the logical and client coordinate systems.

To reduce the size of the star, reduce the size of the viewport by reducing the size of the arguments to SetViewportExt(), or increase the amount of logical area mapped into the viewport by increasing the arguments to SetWindowExt().

The easiest method of becoming familiar with these functions is to change only one argument value at a time, and change it by only a small amount. For example, start by changing the code above to include the following:

   SetWindowOrg(hDC, -30, 110);

Changing the window origin in this manner trims some of the right side of the star from the screen. Change the window origin back to its original location, then change other values. You may need to change the viewport coordinates by a larger amount than the window coordinates to see the changes.

Changing one coordinate at a time is effective only in the MM_ANISOTROPIC mapping mode, which allows the units in the coordinate system to be rectangular rather than square. An application can change units on the y-axis without changing the x-axis, and vice versa. In contrast, the MM_ISOTROPIC mapping mode enforces square units, with an equivalent measure along each axis.


Additional reference words: 3.00 3.10 softlib SHAPES.EXE
KBCategory: kbprg kbfile
KBSubcategory: GdiDc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.