Windows applications draw graphics using a logical unit in which the size of each logical unit is determined by the mapping mode, which can be the same as a device unit (a pixel on the screen or a printer), a physical unit (such as millimeters), or a user-defined unit. In a Windows application, you use the mapping mode whenever you have a relationship with the output.
First of all, the basic knowledge of mapping mode, when the Windows application in its client area to draw graphics, you must give the location of the customer area, its position in X and y two coordinates, x represents the horizontal axis, Y is the ordinate. In all GDI draw functions, these coordinates use a "logical unit". When the GDI function sends the output to a physical device, Windows converts the logical coordinates into device coordinates, such as the pixel points of the screen or printer. The transformation of logical coordinates and device coordinates is determined by the mapping mode. The mapping mode is stored in the device environment. The Getmapmode function is used to get the current mapping mode from the device environment, and the Setmapmode function is used to set the mapping mode for the device environment.
Equipment coordinates (device coordinate), also known as physical coordinates (physical coordinate), refer to the coordinates on the output device. Typically, the device coordinates on the screen are called screen coordinates. Device coordinates specify the position of the object by the horizontal distance and vertical distance of the object from the upper-left corner of the window, expressed in pixels, the x-axis of the device coordinate is positive, the y-axis is positive, and the coordinate origin is in the upper-left corner of the window. When a programmer calls a line-drawing GDI function LineTo and draws a 25.4mm (1-inch) long line, he does not need to consider what device the output is. If the device is a VGA monitor, Windows automatically converts it to 96 pixels, and Windows automatically converts it to 300 pixels if the device is a 300dpi laser printer.
Logical coordinates (Logical coordinate) are the coordinates that the system uses as records. In the default mode (Mm_text), the direction and units of the logical coordinates are the same as the direction and units of the device coordinates, are expressed in pixels, the x-axis is positive, the y-axis is positive, and the coordinate origin is in the upper-left corner of the window.
Logical coordinates and device coordinates are generally consistent in the following default modes:
1. Window is not a scrolling window
2. The window is a scrolling window, the vertical scroll bar is at the top of the scroll border, the horizontal scroll bar is at the leftmost end
The following 3 device coordinates are included in Windows to meet a variety of different needs:
1. Customer area coordinates, including the application's customer area, the upper-left corner of the customer area is (0,0).
2. Screen coordinates, including the entire screen, the upper left corner of the screen is (0,0). Screen coordinates are used in Wm_move messages (for non-child windows) and in the following Windows functions: CreateWindow and MoveWindow (both for non-child windows), GetMessage, GetCursorPos, GetWindowRect , Windowfrompoint and setbrushorg. With functions ClientToScreen and screentoclient, you can convert customer area coordinates to screen area coordinates, or vice versa.
3. Full window coordinates, including a program's entire window, including the title bar, menu, scroll bar and window box, the upper left corner of the window is (0,0). Using the GETWINDOWDC window device environment, you can convert logical units to window coordinates.
The coordinate system for logical coordinates in Windows is called a window, and the coordinate system in which the device coordinates are located is referred to as a viewport.
Windows relies on logical coordinates, which can be pixels, millimeters, or other scales that programmers want.
The viewport depends on the device coordinates (pixel points). Typically, the viewport and the customer area are equivalent. However, if a programmer acquires a device environment with GETWINDOWDC or CREATEDC, the viewport can also refer to full window coordinates or screen coordinates. The point (0,0) is the upper-left corner of the customer area. The value of x increases to the right and the value of y increases downward.
For all mapping modes, Windows uses the following two formulas to convert window coordinates into viewport coordinates:
xviewport= (xwindow-xwinorg) * (Xviewext/xwinext) +xvieworg
yviewport= (ywindow-ywinorg) * (Yviewext/ywinext) +yvieworg
where (Xwindow,ywindows) is the logical point to be converted, (Xviewport,yviewport) is the converted device point, (xwinorg,ywinorg) is the logical coordinates of the window origin; (xvieworg, yvieworg) is the viewport origin of the device coordinates. In the default device environment, both points are set to (0,0), but they can be changed. This formula means that the logical point (xwinorg,ywinorg) is always mapped to a device point (xvieworg,yvieworg). If the device coordinates are customer area coordinates or full window coordinates, Windows must also convert these coordinates to screen coordinates before drawing an object.
Windows can also convert viewport (device) coordinates to window (logical) Coordinates:
xwindow= (xviewport-xvieworg) * (Xwinext/xviewext) +xwinorg
ywindow= (yviewport-yvieworg) * (Ywinext/yviewext) +ywinorg
You can use the two functions provided by Windows DPTOLP and LPTODP to convert between device coordinates and logical coordinates.
Windows defines 8 ways of mapping.
The above mapping pattern can be divided into the following 3 categories:
Mm_text mapping mode This mapping pattern is called "text" mapping, not because it is most appropriate for text, but rather the direction of the axis is consistent with the direction of the read text. Windows provides functions setviewportorg and setwindoworg used to set the origin of Viewports and windows. The default window origin and viewport origin are all (0,0) and can be changed, and the default window range and viewport range are all () and cannot be changed.
The measure mappings mm_lometric, Mm_himetric, Mm_loenglish, Mm_hienglish, and mm_twips map 1 logical units to a fixed physical unit where 1TWIP equals 0.0176mm (1/1440 inches). See table 1 for the physical units corresponding to the other mapping modes. After setting the mapping mode, Windows automatically sets the range of the window and viewport, the value of the range itself is not important, but the range ratio is a fixed value for the Mm_lometric,windows calculation range than the number of horizontal pixels in xviewext/xwinext=0.1mm.
Custom mapping modes Mm_isotropic and mm_anisotropic Two mapping modes allow programmers to set their own window and viewport ranges. The difference between Mm_isotropic and Mm_anisotropic is that the range of the X and Y axes set must be the same, and the range of the X and Y axes set by mm_anisotropic can be different. Isotropi means "in all directions the same", anisotropic means the opposite. The origin and scope of windows and Viewports can be changed in custom mapping mode, and programmers can set their own mapping patterns. Functions SetWindowExt and SetViewportExt are used to change the scope of the window and viewport. The following code maps 1 logical units to 0.396mm (1/64 inches).
Setmapmode (hdc,mm_isotropic);(wince not supported)
SetWindowExt (64,64);
SetViewportExt (Hdc,getdevicecaps (hdc,logpixelsx), GetDeviceCaps (Hdc,logpixelsy));
Logical coordinates and device coordinates--Summary of full window coordinates, screen coordinates, client area coordinates