2. Set coordinate mapping
(1) Windows coordinate system
The Windows coordinate system is divided into logical and device coordinate systems, both of which are supported by GDI. In general, GDI's text and graphics output functions use logical coordinates, while the mouse position in the client area is moved or the mouse is pressed with device coordinates.
the <1> logical coordinate system is a DC-oriented coordinate system that does not take into account the specific device type, and when drawing, Windows converts the logical coordinates to device coordinates based on the mapping mode currently set.
The <2> device coordinate system is a coordinate system that targets the physical device, in units of the minimum length that the pixel or device can represent, the x-axis direction to the right, and the y-axis downward. The origin location of the device coordinate system (0, 0) is not limited to the upper-left corner of the device display area.
The device coordinate system divides into the screen coordinate system, the window coordinate system and the customer area coordinate system three mutually independent coordinate systems.
The screen coordinate system is the origin of the upper-left corner of the screen, and some functions related to the entire screen use screen coordinates such as GetCursorPos (), Setcursorpos (), CreateWindow (), MoveWindow ( )。 The pop-up menu also uses screen coordinates.
The window coordinate system is the origin of the coordinates in the upper-left corner of the window, including the window title bar, menu bar, and toolbar.
the customer area coordinate system is the origin point in the upper left corner of the window client area, which is mainly used for drawing output and window message processing of the client area. The coordinate parameter of the mouse message uses the client area coordinates, and the CDC class drawing member function uses the logical coordinates that correspond to the client area coordinates.
(2) mutual conversion between coordinates
When programming, it is sometimes necessary to convert between three device coordinates or with logical coordinates, depending on the current situation.
MFC provides two functions for CDC::D PTOLP () and CDC:: LPTODP () for mutual conversion between device coordinates and logical coordinates.
MFC provides two functions Cwnd::screentoclient () and Cwnd::clienttoscreen () to convert the screen coordinates to the client area coordinates.
(3) Mapping mode
The mapping mode determines the coordinate system in which the drawing is drawn, defining the actual size of the logical units, the direction of the coordinate growth, and the coordinates origin of all mapping modes in the upper-left corner of the device output area, such as the customer or print area. In addition, for some mapping modes, the user can also customize the length and width of the window to set the physical extent of the view area.
Windows defines 8 mapping modes, as shown in the following table.
mapping mode allows programmers to draw in a unified logical coordinate system without having to consider the specific device coordinate system of the output device.
Mapping method (Mapping Mode) |
Logical units |
Axis direction |
Mm_text (default mode) |
1 pixel |
The x-axis is in the right direction and the y-axis is facing downwards. |
Mm_lometric |
0.1 mm |
The x-axis is in the right direction and the y-axis is facing upwards. |
Mm_himetric |
0.01 mm |
The x-axis is in the right direction and the y-axis is facing upwards. |
Mm_loenglish |
0.01 inch |
The x-axis is in the right direction and the y-axis is facing upwards. |
Mm_hienglish |
0.001 inch |
The x-axis is in the right direction and the y-axis is facing upwards. |
Mm_twips |
1/1440 inch |
The x-axis is in the right direction and the y-axis is facing upwards. |
Mm_isotropic |
Custom (X=y) |
Custom |
Mm_anisotropic |
Custom (X!=y) |
Custom |
The Mm_isotropic and mm_anisotropic mappings are generally chosen when the drawing needs to change automatically as the size of the window changes. The only difference is that the logical units of the X and Y axes are the same size, and the word "isotropic" is meant to be equal in each direction, and this mapping is suitable for drawing circles or squares. In practical applications, the x-axis and y-axis are often given different proportions, when the Mm_anisotropic mapping method is chosen. The word "anisotropic" is the meaning of different directions.
(4) Custom mapping mode
the concepts of window and viewport:
window: Corresponds to the area set by the programmer on the logical coordinate system
viewport (Viewport): Corresponds to the area set by the programmer on the actual output device
The window origin is the position of the origin of the logical window coordinate system in the viewport (device) coordinate system, and the viewport origin refers to the origin of the actual output area of the device.
In addition to mapping patterns, windows and viewports are also a factor in determining how a point's logical coordinates are converted to device coordinates. The logical coordinates of a point are converted to device coordinates according to the following formula:
Device (viewport) coordinates = logical coordinates – window origin coordinates + viewport origin coordinates
// defining coordinate mapping methods
Wingdiapi int WINAPI setmapmode(HDC, int);
This API function is encapsulated in MFC as Cdc::virtual int setmapmode(int nmapmode);
// defines the logical window area, in logical units ( Logical )
Wingdiapi BOOL WINAPI Setwindowextex (HDC, int, int, lpsize);
This API function is encapsulated in MFC as Cdc::virtual CSize setwindowext(int cx, int cy);
// sets the origin coordinates of the logical window, the default origin is ( 0 , 0 ).
Wingdiapi BOOL WINAPI setwindoworgex(HDC, int, int, lppoint);
This API function is encapsulated in MFC as Cdc::cpoint setwindoworg(int x, int y);
Note: Set windoworg (Ex) only makes sense if the mapping mode is Mm_anisotropic or mm_isotropic.
// defines the axis direction and region, defined field, and range of the viewport, in pixels ( Pixel )
Wingdiapi BOOL WINAPI Setviewportextex(HDC, int, int, lpsize);
This API function is encapsulated in MFC as Cdc::virtual CSize setviewportext(int cx, int cy);
Note: SetViewportExt (Ex) only makes sense if the mapping mode is Mm_anisotropic or mm_isotropic.
// sets the origin coordinates of the viewport, the default origin is ( 0 , 0 ).
Wingdiapi BOOL WINAPI setviewportorgex(HDC, int, int, lppoint);
This API function is encapsulated in MFC as CDC:: Virtual CPoint setviewportorg(int x, int y);
Reference: "Coordinate mapping problem inGDI "http://dev.csdn.net/article/12/12013.shtm
--------------------to Be Continued------------------
GDI Drawing in MFC (3)