Detailed on the establishment of the coordinate system in the VC. BULL77
Establishing a suitable coordinate system can bring great convenience to our drawing. Here are how to establish the coordinate system we want in the VC.
A equipment coordinates and logical coordinates
Device Coordinate is also known as physical coordinate, refers to the coordinates on the output device. Typically call the device coordinates on the screen as screen coordinates. The horizontal distance and vertical distance of the device coordinate the upper left corner of the device are indicated by the position of the object, and the X axis right of the device coordinate is positive, the Y axial is positive, the coordinate original is located in the window. The upper left corner.
Logical Coordinate is the coordinate of the system as records. Under the default mode (mm_text), the direction of the logical coordinates is the same as the direction and unit of the device coordinates, but also in units of pixels, the X axis right is positive, the Y axial direction is positive, coordinate original Located on the upper left corner of the window. Logical coordinates and equipment coordinates do not be consistent even in the default mode, in addition to the following two cases:
1. Window is a non-rolling window
2. The window is the scroll window, but the vertical scroll bar is located at the top end of the scroll border, and the horizontal scroll bar is at the leftmost end, but if the two coordinates of the scroll bar are inconsistent.
The coordinate position of the mouse coordinate in the VC is represented by the device coordinate, but all GDI drawings are used.
The coordinates are indicated, so when drawing, the device coordinates must be converted to logic coordinates, and the CDC function DPTOLP () can be used to convert the device coordinates to logical coordinates, and the logical coordinates can be converted to the device coordinate.
Two coordinate mode
In order to use logic coordinates in different fields, Windows provides the following 8 coordinate modes:
MM_TEXT, MM_HIENGLISH, MM_LOENGLISH, MM_HIMETRIC, MM_LOMETRIC, MM_TWIPS, MM_ANISOTROPIC, and MM_ISOTROPIC.
Three instance analysis
(1) Establish coordinates of the original point, the X-axis and the Y-axis 1000, as shown below
We can use the following code:
Void cttview :: OnDraw (CDC * PDC) {CTTDOC * PDOC = getDocument (); assert_valid (pdoc); CRECT RECT; GetClientRect (& Re);
PDC-> setmapmode (mm_anisotropic); PDC-> SetViewportorg (0, 0); PDC-> SetViewPortext (Rect.right, Rect.Bottom);
PDC-> setWindoworg (0, 0); PDC-> SetWindowExt (1000, 1000);
PDC-> Moveto (50, 50); PDC-> Lineto (50, 950); PDC-> Lineto (950, 950); PDC-> LineTo (50, 50);
Code analysis:
1. getClientRect (& Re), get the rectangular area of the client area, store it in the RECT
2. Use pdc-> setmapmode (mm_anisotropic); set mapping mode
3. With PDC-> SetViewPortorg (0, 0); set the origin of logical coordinates
4. By PDC-> setViewPortext (Rect.right, Rect.Bottom);
PDC-> SetWindowext (1000, 1000); to determine the logical coordinates and the size corresponding to the size of the device coordinate 5. In mm_anisotropic mode, the X-axis unit and the Y-axis unit may not be the same.
6. The method of determining the direction of the coordinate direction is that if the logical window range and the viewport range symbol are the same, the direction of the logical coordinates is the same, that is, the X axis right is positive, the Y axial direction is positive.
7. If the display mode is changed to mm_isotropic, the X-axis unit and the Y-axis unit must be the same, and the reader of interest can make itself.
(2) Establish coordinates with the origin of the window, as shown below
Use the following code:
Void cttview :: OnDraw (CDC * PDC) {CTTDOC * PDOC = getDocument (); assert_valid (pdoc); CRECT RECT; GetClientRect (& Re);
PDC-> setmapmode (m_anisotropic); PDC-> setViewportorg (Rect.right / 2, Rect.Bottom / 2); PDC-> SetViewPortext (Rect.right, Rect.Bottom);
PDC-> setwindoworg (0, 0); PDC-> SetWindowext (1000, -1000);
PDC-> Moveto (150, 150); PDC-> LineTo (-150, -200); PDC-> LineTo (150, -150); PDC-> LineTo (150, 150);
Code analysis:
1. Use PDC-> setViewportorg (Rect.right / 2, Rect.Bottom / 2); set the origin of the viewport.
2. Use PDC-> SetViewPortext (Rect.right, Rect.Bottom); and PDC-> SetWindowext (1000, -1000); to determine the unit correspondence of the device coordinates and logical coordinates.
3. Because the symbols of the logical window and the viewport range are inconsistent, the orders are inverse, so the Y axial direction is positive.