The process of displaying a graphic in a screen coordinate system is called mapping, and the mapping pattern is divided into logical coordinates and device coordinates. Logical coordinate units are metric scales, and device coordinate units are pixels. By default, the device coordinate system is used, a device coordinate is equal to one pixel, the physical size of the pixel varies depending on the device, the device coordinate system origin is located in the upper-left corner of the customer area, the x-axis is horizontal to the right, the y-axis is vertically downward, and the unit coordinates a pixel.
The concept of "window" and "viewport". The window is understood as a rectangular area under a logical coordinate system, and the viewport is a rectangular area under the device coordinate system. Scale factor in X-direction and Y-direction: X-directional scale factor = viewport CX/window cx, y-directional scale factor = viewport CY/window CY. If you set SetWindowExt (100,100), SetViewportExt (200,200), the scale factor for both the X and Y directions is 2, indicating that a logical coordinate of the window is mapped to two pixels of the viewport.
void Ctestview::ondraw (cdc* pDC) {
Ctestdoc * PDoc = GetDocument ();
CRect rect; Declaring a CRect class rectangle object
GetClientRect (&rect); Use the member function of the CWnd class GetClientRect (LPRECT LPRECT) to get the client area size
Pdc->setmapmode (Mm_anisotropic); Set mapping mode to Mm_anisotropic
Pdc->setwindowext (rect. Width (), Rect. Height ()); Setup window
Pdc->setviewportext (rect. Width (),-rect. Height ()); Set viewport: X-axis horizontal to right, y-axis vertically downward
Pdc->setviewportorg (Rect.width ()/2,rect. Heigth ()/2); Customer Area Center is the coordinate system origin.
}
To draw a straight segment function:
MoveTo () and LineTo () functions
Each line segment is drawn with the current position as the starting point, and after the line segment is drawn, the end of the straight segment becomes the current position.
Set Current position function: Cdc::moveto
Prototype:
CPoint MoveTo (int x,int y);
CPoint MoveTo (point point);
Draw line segment function: Cdc::lineto
Prototype:
BOOL LineTo (int x,int y);
BOOL LineTo (point point);
Example:
void Ctestview::ondraw (CDC * PDC)
{
CTESTCOC * PDoc = GetDocument ();
CRect rect;
GetClientRect (&rect);
Pdc->setmapmode (Mm_anisotropic); Set mapping mode to Mm_anisotropic
Pdc->setwindowext (rect. Width (), Rect. Height ()); Setup window
Pdc->setviewportext (rect. Width (),-rect. Height ()); Set viewport: X-axis horizontal to right, y-axis vertically downward
Pdc->setviewportorg (Rect.width ()/2,rect. Heigth ()/2); Customer Area Center is the coordinate system origin.
CPoint P0 ( -100,-50), p1 (100,50);
Cpen Newpen,*poldpen;
Newpen.createpen (Ps_solid,1,rgb (0,0,255));
Poldpen = Pdc->selectobject (&newpen);
Pdc->moveto (P0);
Pdc->lineto (p1);
Pdc->selectobject (Poldpen);
}
Output graphics and text in the MFC framework, if not output in the OnDraw () function, you first need to get the device context before you can call the member function of the corresponding CDC class to vomit. Get up to 5 device contexts at any time, so you should release the acquired device context when the drawing is complete.
cdc* GetDC ();
If the call succeeds, returns the device context pointer to the current screen client, otherwise, returns null
int ReleaseDC (cdc* PDC);
The PDC is a device context pointer that is disposed, and if the call succeeds, returns "not 0", otherwise, returns "0".
Computer graphics--mapping mode