First, the purpose of the experiment
1. Master the method of building MFC application;
2. Master the mapping mode.
Ii. contents of the experiment
1. In MFC to draw a ruler, rulers need to have a scale, similar to the daily use of transparent plastic ruler, need to establish four rulers, respectively, distributed in the screen customer area of the upper, lower, left and right four boundaries. Ruler needs to have the scale, that customer area upper ruler distance, should have centimeters, 5 millimeters, 1 millimeters scale, the scale with the vertical line display, the length is 7 mm, 6 mm, 5 mm respectively, the appearance resembles the student ruler, the right end leaves a centimeter, prevents 4 ruler to touch together.
2. Draw a coordinate system, give the x-coordinate change range, the y-coordinate range, draw the axis, and mark the scale on the axis, the origin, the axis is required to fill the customer area.
Third, the experimental steps
(i) draw a ruler in MFC.
1. Design ideas:
(1) Declaring customer area rectangle
(2) obtaining customer area coordinates
(3) Setting the mapping mode
(4) Draw a ruler in four directions and draw a ruler scale by circulation
Attention:
The mapping mode chooses Mm_lometric, which corresponds to the coordinate system feature: each logical coordinate is converted to 0.1 mm. Positive x right, positive y upward.
2. The code is as follows:
(1) Declaring the client area rectangle and setting the mapping mode
voidCrulerview::ondraw (cdc*PDC) {Crulerdoc* PDoc =getdocument (); Assert_valid (PDOC); //Todo:add Draw code for native data hereCRect rect;//declaring a customer area rectangleGetClientRect (&rect);//get customer Area coordinatesPdc->setmapmode (Mm_lometric);//set the mapping mode to: Mm_lometric, that is, each logical coordinate is converted to 0.1 MM. Positive x right, positive y upward. //set the length and width range of the actual client area window intHeight=rect. Height () *2.5; intWidth=rect. Width () *2.5; intI
(2) Draw the ruler at the top
//Top ruler ruler//Draw a straight line at the top, as the measure edge of the ruler, draw from the top (/left) down (/right)Pdc->moveto (0,0); PDC->lineto (width- -,0); COLORREF C=rgb (0,0,255); //start the cycle and draw the ruler's scale for(i=0; i<width- -; i+=Ten) { //ruler distance is 1 cm, scale vertical bar length is 7 mm if(i% -==0) {PDC->moveto (I,0); PDC->lineto (i,- -); } //ruler distance is 5 mm, scale vertical bar length is 6 mm Else if(i% -==0) {PDC->moveto (I,0); PDC->lineto (i,- -); } //ruler distance is 1 mm, scale vertical bar length is 5 mm Else{PDC->moveto (I,0); PDC->lineto (i,- -); } }
(3) Draw the ruler at the bottom
//Bottom ruler Draw the ruler at the bottom//draw a straight line at the bottom, as the measure edge of the ruler (draw from top/left down/right)Pdc->moveto (width,-height+ -); PDC->lineto ( -,-height+ -); //start the cycle and draw the ruler's scale for(i=0; i<width- -; i+=Ten) { //ruler distance is 1 cm, scale vertical bar length is 7 mm if(i% -==0) {PDC->moveto (width-i,-height+ -); PDC->lineto (width-i,-height+ -); } //ruler distance is 5 mm, scale vertical bar length is 6 mm Else if(i% -==0) {PDC->moveto (width-i,-height+ -); PDC->lineto (width-i,-height+ +); } //ruler distance is 1 mm, scale vertical bar length is 5 mm Else{PDC->moveto (width-i,-height+ -); PDC->lineto (width-i,-height+ -); } }
(4) Draw the ruler on the left
//left ruler draw the ruler//first draw a straight line at the left, as the measure edge of the ruler (draw from top/left down/right)Pdc->moveto (0,-height); PDC->lineto (0,- -); //start the cycle and draw the ruler's scale for(i=0; i -; i+=Ten) { //ruler distance is 1 cm, scale vertical bar length is 7 mm if(i% -==0) {PDC->moveto (0,-height+i); PDC->lineto ( -,-height+i); } //ruler distance is 5 mm, scale vertical bar length is 6 mm Else if(i% -==0) {PDC->moveto (0,-height+i); PDC->lineto ( -,-height+i); } //ruler distance is 1 mm, scale vertical bar length is 5 mm Else{PDC->moveto (0,-height+i); PDC->lineto ( -,-height+i); } }
(5) Draw the ruler on the right
//right ruler draw the ruler//draw a straight line on the far right, as the ruler's measure edge (draw from right to left)Pdc->moveto (width,0); PDC->lineto (width,-height+ Max); //start the cycle and draw the ruler's scale for(i=0; i Max; i+=Ten) { //ruler distance is 1 cm, scale vertical bar length is 7 mm if(i% -==0) {PDC->moveto (width,-i); PDC->lineto (width- -,-i); } //ruler distance is 5 mm, scale vertical bar length is 6 mm Else if(i% -==0) {PDC->moveto (width,-i); PDC->lineto (width- -,-i); } //ruler distance is 1 mm, scale vertical bar length is 5 mm Else{PDC->moveto (width,-i); PDC->lineto (width- -,-i); } }
2. Operation Result:
(b) Draw the coordinate system.
1. Design ideas:
(1) Declaring customer area rectangle
(2) obtaining customer area coordinates
(3) Setting the mapping mode
(4) Draw the x-axis and y-axis, and draw the coordinate change range through the loop
Attention:
The mapping mode chooses Mm_lometric, which corresponds to the coordinate system feature: each logical coordinate is converted to 0.1 mm. Positive x right, positive y upward.
2. The code is as follows:
(1) Declaring the client area rectangle and setting the mapping mode
voidCcoordinatesystemview::ondraw (cdc*PDC) {Ccoordinatesystemdoc* PDoc =getdocument (); Assert_valid (PDOC); //Todo:add Draw code for native data hereCRect rect;//declaring a customer area rectangleGetClientRect (&rect);//get customer Area coordinates intHeight= (int) Rect. Height () *2.5; intWidth= (int) Rect. Width () *2.5; inti; PDC->setmapmode (Mm_lometric);//set the mapping mode to: Mm_lometric, that is, each logical coordinate is converted to 0.1 MM. Positive x right, positive y upward. Pdc->setwindowext (Width,height);//Setup windowPdc->setviewportext (Width,-height);//x Axis horizontal right, Y axis vertical upward//pdc->setviewportorg (WIDTH/2,HEIGHT/2);//Customer Area Center is the coordinate system origin.
(2) Draw x-axis and y-axis
// Draw x Axis Pdc->moveto (0,-height/2); PDC->lineto (width,-height/2); // Draw Y-axis Pdc->moveto (width/2,0); PDC->lineto (width/2,-height);
(3) Draw the coordinate scale of x and Y axes
//Draw x axis negative half axis scale for(i=0; i<=width/2; i+= -) {PDC->moveto (i,-height/2); PDC->lineto (i,-height/2+ -); } //drawing x-axis positive half axis scale for(i=width/2; i<=width;i+= -) {PDC->moveto (i,-height/2); PDC->lineto (i,-height/2+ -); } //Draw y-axis positive half axis scale for(i=0; i<=height/2; i+= -) {PDC->moveto (width/2, i); PDC->lineto (width/2+ -, i); } //Draw x axis negative half axis scale for(i=-height/2; i<=height;i+= -) {PDC->moveto (width/2,-i); PDC->lineto (width/2+ -,-i); }
2. Operation Result:
Iv. Experimental Results and discussion
(a) the ruler drawn in MFC is as follows:
(b) The coordinate system plotted in MFC is as follows:
V. Summary
(a) The experiment was completed on time and in quantity.
(b) Through this experiment, the method of building MFC application is mastered.
(c) Through this experiment, I have mastered the mapping mode and its use.
(iv) Problems encountered during the experiment and points for attention:
1. Customer area The actual length and width of the setting range should be how to fit the screen?
2. It is important to note that the mapping mode chooses Mm_lometric, and its corresponding coordinate system features are: each logical coordinate is converted to 0.1 mm. Positive x right, positive y upward.
MFC graphics Drawing--drawing rulers and coordinate systems