Coordinate map instances
(1) Create a single-document MFC project DRAW:NEW->PROJECTS->MFC AppWizard (EXE)->single document.
(2) Locate CMainFrame::P the Recreatewindow function, where the default window size is set to pixel*300 pixel.
BOOL CMainFrame::P Recreatewindow (createstruct& CS)
{
if ( ! CFrameWnd::P Recreatewindow (CS))
return FALSE;
//Todo:modify the Window class or styles here by modifying
//The CREATESTRUCT CS
cs.cx=400;
cs.cy=300;
return TRUE;
}
(3) Add OnPaint Event
Resource Manager->classview-> Right-click CDrawView Select Add Windows Message Handler
->wm_paint-> ADD Handler
1. void cdrawview::onpaint ()
2. {
3. CPaintDC DC (this); //device context for painting
4. //Todo:add your message Handler code here
5. CRect CR; // Rectangular Structure
6. GetClientRect (&CR); // Get client Area window
7. int cx=cr.right; // Right
8. int cy=cr.bottom; // Bottom
9. DC. Setmapmode (Mm_isotropic); //x=y
DC. SetWindowExt (1000,1000); // Set Logical window, default window origin is (0, 0)
One. DC. SetViewportExt (Cx,-cy); // defines the output viewport, x right y is positive
A. DC. Setviewportorg (CX/2,CY/2); // define viewport origin as Customer Area center
DC. Ellipse ( -200,200,200,-200); // to draw an ellipse that is circumscribed by the client area
// draw a horizontal vertical four-bar radius
. DC. MoveTo (0,0); dc. LineTo (200,0);
. DC. MoveTo (0,0); dc. LineTo ( -200,0);
. DC. MoveTo (0,0); dc. LineTo (0,200);
DC. MoveTo (0,0); dc. LineTo (0,-200);
F5 // perform debug in the bottom Output window to observe the Clientrect
TRACE ("Clientrect.x =%d, Clientrect.y =%d\n", CX, CY);
21.}
Run Results 1 left. When the window size is changed, the shape of the circle in the figure is always the same.
<1> Change Line 9th of the above code to: DC. Setmapmode (Mm_anisotropic); //x!=y Run result 1 right.
Figure 1
We found that despite the above code, the 13th line of DC. Ellipse ( -200,200,200,-200); the ellipse that is defined in the bounding rectangle is logically square, but instead of a circle, the ellipse is not displayed.
When we change the size of the window, the ellipse is deformed and may even turn into a circle. The specific:
To keep the window width constant, decrease the height, the ellipse becomes more flat, keep the window height constant, reduce the width, the ellipse becomes more rounded, and when stretched to the customer area is a square, we find that the ellipse becomes a circle!
<2> Change the 9th line of the above code back to DC. Setmapmode (Mm_isotropic); //x=y, the 15th line is changed to DC. LineTo (500,0); The 18th line is changed to DC. LineTo (0,-500); Run Results 2 left.
Keep window height constant, reduce window width, make window width < window height, run result 2 right.
Figure 2
<3> changes the 9th line of code in <2> back to DC. Setmapmode (Mm_anisotropic); //x!=y,
Run result 3:
Figure 3
When we change the window size, the DC. LineTo (500,0); dc. LineTo (0,-500) is a straight line from the origin (Customer Area Center) to the center of the right end of the customer area and the center of the bottom.
<4> will be the original code in line 10th of DC. SetWindowExt (1000,1000); // Add a DC after setting the logical window . SetWindowOrg (100,100); Sets the origin of the logical window to (100,100). Observing the results of the operation, the graph in Figure 1 has moved 100 logical units to the left and downward respectively:
( -200,200,200,-200)--( -200-100,200-100,200-100,-200-100)
If you need to maintain the graphics in Figure 1, you need to add (100,100) Each of the points involved, namely:
DC. Ellipse ( -200+100,200+100,200+100,-200+100);
// draw a horizontal vertical four-bar radius
. DC. MoveTo (0+100,0+100); dc. LineTo (200+100,0+100);
. DC. MoveTo (0+100,0+100); dc. LineTo ( -200+100,0+100);
. DC. MoveTo (0+100,0+100); dc. LineTo (0+100,200+100);
DC. MoveTo (0+100,0+100); dc. LineTo (0+100,-200+100)
(4) Summarize the mapping method of logical window coordinates to device viewport coordinates:
<1> Logical window origin map to viewport origin
<2> logical window width and height map to viewport width and height
<3> when the mapping mode is Mm_isotropic, windowext.width=windowext.height, the effective drawing area is the smallest side of the viewport wide high school for the side Long Square area. The scale factor is:
Scalex=scaley=min{viewportext.width, Viewportext.height}/windowext.width
When the mapping method is Mm_anisotropic, the valid drawing area is the entire viewport (this is the customer area). The scale factor is:
Scalex=viewportext.width/windowext.width
Scaley=viewportext.height/windowext.height. See Figure 4.
<4> Equipment (viewport) coordinates = (logical coordinates – Logical window origin coordinates) x scale factor + viewport origin coordinates
Figure 4
The client area size in the following analysis is clientrect= (388,200), the logical window origin is windoworg=(100,100), based on the modified code in (3) <4>.
In 4 left, Nmapmode=mm_isotropic, the upper-left corner of the ellipse's bounding rectangle (-100,300) is mapped to the client area in pixel coordinates:
left_top_x= ( -100-100) X (200/1000) +388/2=154 pixel
left_top_y= (300-100) x (200/1000) +200/2=140 pixel
According to this method, the coordinates of the lower-right logical coordinates (300,-100) mapped to the client area are calculated in pixel units:
right_bottom_x=234 pixel;right_bottom_y=60 Pixel
If we are in line 9th DC. Setmapmode (Mm_isotropic); Add CreatePen (Ps_solid,2,rgb (255,0,0));d c before //x=y . Ellipse (154,140,234,60), it can be found that this 2-pixel-wide red brush (ellipse) is just the same as the (ellipse) circle that is drawn after setting the mapping mode. However, when we change the window size, we find that the drawn (elliptical) circle is stretched proportionally when the mapping mode is set, but the red circle is always in place and the size remains the same, which means that the default mapping method Mm_text is drawn in the direction of the x-axis in the direction of the right, the y-axis, and 1 pixel.
Similarly, we can analyze 4 right in the case of Nmapmode=mm_anisotropic, CRect (116,140,272,60), an equivalent ellipse for an external rectangle.
GDI Drawing in MFC (5)