This is my recent "Computer graphics" course practical programming class introduction of the relevant knowledge, mainly want to use MFC C + + drawing, let students realize the graphics related to the programming and simple graphic drawing, at the same time very admire the students imagination, they do really good. I hope this basic article will be of some help to you. In particular, students with this course or programming enthusiasts, if there are errors or deficiencies in the article, please Haihan.
Reference books: Konglingde • Basic course of computer graphics (Visual C + + Edition)
Students draw the graphics are very innovative, expressed very satisfied, ha ha ~
I. MFC graphics basics CDC class
PS: This part mainly introduces the knowledge of Konglingde teachers, this article is mainly based on the following programming. VC + + has a powerful drawing function, although based on the application of dialog box I recommend you use C # WinForm program, but the basic knowledge of computer graphics and images, or strongly recommend the use of VC + + MFC implementation. This will help you to understand the graphic transformation, image processing and other knowledge.
Under the Windows platform, the GDI (Graphics Device Interface) graphics device interface is abstracted as a context CDC class (Device CONTEXT,DC). Instead of hardware devices such as monitors and printers, the Windows platform directly receives graphic data information, but CDC objects. In MFC, the CDC class defines the base class for the device context object, encapsulates the required member functions, invokes the CDC class's member functions, and draws and prints graphics and text.
CDC class derives CClientDC class, CMetaFileDC class, CPaintDC class and CWINDOWDC class, ask reader to study by oneself, also recommend reading original book.
MFC commonly used CPoint, CRect, CSize and other data types.
(1) CPoint class: storage point coordinates (X,Y);
(2) CRect class: The coordinates of the upper-left and lower-right vertices of the rectangle (top, left, right-hand, bottom), where (Top,left) is the upper-right corner of the rectangle, and (Right,bottom) is the lower right-hand corner of the rectangle;
(3) Cszie class: Coordinates (CX,CY) that hold the width and height of the rectangle, where CX is the width of the rectangle, and Cy is the height of the rectangle.
The MFC drawing tools classes include CGdiObject, CBitmap, CBrush, CFont, Cpallette, CPen, and CRgn. Commonly used include:
(1) CBitmap: Encapsulates a GDI bitmap that provides an interface for bitmap manipulation;
(2) CFont: Encapsulates the GDI font, which can be selected as the current font in the device context;
(3) CBrush: Encapsulates a GDI brush, selected as the current brush for the device context, and brushes are used to fill the interior of the graphic;
(4) CPen: Encapsulates the GDI brush, is selected as the current brush for the device context, and the brush is used to draw the graph boundary line;
(5) Cpallette: Encapsulates the GDI palette, providing the color interface between the application and the monitor;
(6) Cgdiobject:gdi drawing tools of the base class, generally can not be used directly.
Two. MFC Single document programming introduction
First create the MFC project, select the "MFC AppWizard" project, named "test01."
Then select the single document format, where dialog box is the dialog dialog project.
After the project is created, the work range includes: ClassView (Class View), ResourceView (Resource View), and FileView (File view). Class views consist primarily of classes, and file views include source files. cpp and header files. H.
Open the Resource ResourceView view looks like this:
MFC write code usually finds the OnDraw () function in "Test01View.cpp" under the XxxView.cpp file for drawing:
//Ctest01view drawing
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
}
The MFC basic drawing functions are described in detail below.
Three. MFC drawing functions and extensions
1.MFC Drawing function
(1) Draw a straight line
Cdc::moveto (int x, int y)
Moves the brush to the current position, at coordinates (x, y), and does not draw a line.
cdc::lineto (int x, int y)
The brush draws a line from its current position to (x, y), but does not contain (x, y) points.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Pdc->moveto (100,150);
Pdc->lineto (300,400);
Todo:add draw code for native data here
}
The drawing figure is as follows, and coordinates (100, 150) represent 100 to the left and 150 to the top.
Note: Drawing graphics mainly calls the cdc* PDC method implementation, MFC can complement the prompt function.
(2) Set Brush
You can usually set the color and line properties of the drawing graph, which is:
cpen::createpen (int npenstyle, int nwidth, COLORREF color)
The first parameter is the style of the brush, implementation, dashed line, and so on, the second parameter is the brush thickness, the third parameter makes the color of the brush, using RGB (255, 255, 255) assigned value.
Cpen::selectobject (CPen *pen)
Call CPen to select the Brush object pointer, to point the brush to the new brush, and to point to the pointer.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Draw Straight line
Pdc->moveto (100,150);
Pdc->lineto (300,400);
Defines the brush drawing line
cpen pen (ps_dash, 4, RGB (255,0,0));//dashed thick 4 red
pdc->selectobject (&pen);
Pdc->moveto (100,150);
Pdc->lineto (400,300);
Method Two CreatePen defines the brush
cpen pen2;
Pen2. CreatePen (Ps_dashdotdot, 1, RGB (0,255,0)); Double point draw line coarse 2 green
pdc->selectobject (&pen2);
Pdc->moveto (100,150);
Pdc->lineto (500,200);
}
The results of the run are shown in the following illustration, note that you need to select a brush SelectObject () after defining the brush to use it.
(3) Draw a rectangle
Cdc::rectangle (int x1, int y1, int x2, int y2)
The parameter X1 and Y1 represent the upper-left corner coordinate of the rectangle, and the parameter X2 and y2 represent the lower right corner coordinates of the rectangle.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Defines the brush drawing rectangle
CPen Pen (ps_dash, 2, RGB (0,0,255));//dashed thick 2 blue
pdc->selectobject (&pen);
Defines the coordinate point
cpoint point1 (100,150);
CPoint Point2 (400,300);
Draw rectangular
pdc->rectangle (point1.x, Point1.y, point2.x, point2.y);
The results of the operation are shown in the following figure, while the definition point is cpoint, and you can call Point.x and point.y to get the coordinates.
(4) Set Paint brush Fill Color
Cbrush::createsolidbrush (colorref crcolor)
parameter is a brush color and is used primarily for filling graphics.
Cbrush::selectobject (cbrush* pbrush)
Select the brush, fill the color, and pbrush the parameter as a pointer to the selected CBrush object.
CGdiObject::D electobject ()
Remove the brush from the system memory with the Free State, this function is the same as the delete brush function.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Defines the brush drawing rectangle
CPen Pen (ps_dash, 2, RGB (0,0,255));//dashed thick 2 blue
pdc->selectobject (&pen);
CBrush Bush;
Bush. CreateSolidBrush (RGB (255,0,0));
Pdc->selectobject (Bush);
Defines the coordinate point
cpoint point1 (100,150);
CPoint Point2 (400,300);
Draw rectangular
pdc->rectangle (point1.x, Point1.y, point2.x, point2.y);
Select the brush to fill in the following figure:
(5) Clear brushes and brushes
In real operations, brushes and brushes that have become free are removed from the system's memory when the brushes and brushes are used.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Defines the brush drawing rectangle
cpen mypen, *oldpen;
Mypen.createpen (Ps_dash, 2, RGB (0,0,255)); Dashed thick 2 blue
Oldpen = Pdc->selectobject (&mypen); Old Brush assigned
//Brush
CBrush mybrush, *oldbrush;
Mybrush.createsolidbrush (RGB (255,0,0));
Oldbrush = Pdc->selectobject (&mybrush);
Defines the coordinate point
cpoint point1 (100,150);
CPoint Point2 (400,300);
Draw rectangular
pdc->rectangle (point1.x, Point1.y, point2.x, point2.y);
Clear
Pdc->selectobject (oldpen);
Mypen.deleteobject ();
Pdc->selectobject (Oldbrush);
Mybrush.deleteobject ();
}
(6) Draw an ellipse function
Cdc::ellipse (int x1, int y1, int x2, int y2)
Parameter x1, y1 is the coordinates that draw the upper-left corner of the ellipse's outer rectangle, x2, y2 is the lower-right corner coordinate of the outer rectangle. When you draw an external rectangle with the same length and width, you draw a circle.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Define brush
CPen mypen, *oldpen;
Mypen.createpen (Ps_dash, 2, RGB (0,0,255)); Dashed thick 2 blue
Oldpen = Pdc->selectobject (&mypen); Old Brush assigned
//Brush
CBrush mybrush, *oldbrush;
Mybrush.createsolidbrush (RGB (255,0,0));
Oldbrush = Pdc->selectobject (&mybrush);
Defines the coordinate point
cpoint point1 (100,150);
CPoint Point2 (400,300);
Draw Elliptical
Pdc->ellipse (point1.x, Point1.y, point2.x, point2.y);
Draw round
pdc->ellipse (0, 0, m);
Clear
Pdc->selectobject (oldpen);
Mypen.deleteobject ();
Pdc->selectobject (Oldbrush);
Mybrush.deleteobject ();
}
The output is shown in the following illustration:
Note: There are also some methods, including drawing arcs, drawing polygons, the method is similar, just note the parameters can be.
(7) Draw text
Cdc::textout (int x, int y, const CString & str)
The parameter x and y are the starting coordinates of the text, and the parameter str is the CString object, the text content. You can also control string output variables in conjunction with the format format.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Define brush
CPen mypen, *oldpen;
Mypen.createpen (Ps_dash, 2, RGB (0,0,255)); Dashed thick 2 blue
Oldpen = Pdc->selectobject (&mypen); Old Brush assigned
//Brush
CBrush mybrush, *oldbrush;
Mybrush.createsolidbrush (RGB (255,0,0));
Oldbrush = Pdc->selectobject (&mybrush);
Defines the coordinate point
cpoint point1 (100,150);
CPoint Point2 (400,300);
Draw Elliptical
Pdc->ellipse (point1.x, Point1.y, point2.x, point2.y);
Pdc->textout (405,305,_t ("Drawing Ellipse"));
Draw round
pdc->ellipse (0, 0, m);
Use format to write text
CString str1 = "Draw circle radius =";
int r = m;
CString data;
Data. Format ("%s%d", str1,r);
Pdc->textout (105,105,data);
Clear
Pdc->selectobject (oldpen);
Mypen.deleteobject ();
Pdc->selectobject (Oldbrush);
Mybrush.deleteobject ();
}
The output is shown in the following illustration:
2. Expanding knowledge
The first extension is that, in the process of drawing a line, if you loop around a circle, you get a nice, rounded line.
#include <math.h>
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Define the brush and select
cpen pen (Ps_solid,4,rgb (120,32,240));
Pdc->selectobject (&pen);
Define the point to draw a vertical straight line
cpoint p0 (300,300);
CPoint P1 (300,550);
Pdc->moveto (p0);
Pdc->lineto (p1);
Radius and pi
int R =;
float pi = 3.14f;
Define 150 point loops in accordance with the circular drawing
cpoint p[100];
for (int i=0;i<100;i++)
{
p[i].x = Int (P0.x+r*sin (pi+i+1)/20)); The x coordinate sin involves mathematical knowledge
p[i].y = Int (P0.y+r*cos (pi+i+1)/20)); Y coordinate
//move first to the circular P0 (300,300) and then draw a straight line
Pdc->moveto (p0);
Pdc->lineto (P[i]);
}
The output is shown in the following illustration:
The content is common graphics rotation knowledge, the core content: The calculation of new coordinates, through sin and cos math knowledge, while the circle is unchanged, each cycle first moveto (p0) can be.
The second piece of code involves a graphic translation, which is to draw a rectangular graphic translation operation.
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Define the brush and select
cpen pen (Ps_solid,4,rgb (120,32,240));
Pdc->selectobject (&pen);
CBrush Brush (RGB (250,12,30));
Pdc->selectobject (&brush);
Loop Draw Rectangle
int x1=100,y1=100,x2=300,y2=400;
for (int j=0; j<100; j=j+3)
{
pdc->rectangle (x1+j, Y1+j, X2+j, y2+j);
}
The output looks like this:
four. Student Achievement and Innovation
The following code is better to do the students, the feeling is still good.
Xia KH classmate:
#include <math.h>
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native the data here
int d,k,x1,x2,y1,y2;
float pi,a,e;
CPen pen;
Pen. CreatePen (Ps_solid,1,rgb (155,0,0));
CPen *poldpen = Pdc->selectobject (&pen);
PI = 3.1415926f;
d =;
for (a = 0; a<=2 * PI; a+= pi/360)
{
E = d * (1+0.25*sin (4*a));
E = e * (1 + sin (8*a));
x1 = Int (320+e*cos (a));
x2 = Int (320+e*cos (A + PI/8));
y1 = Int (200+e*sin (a));
y2 = Int (200+e*sin (A + PI/8));
Pdc->moveto (x1,y1);
Pdc->lineto (x2,y2);
}
The output looks like this:
Li N Classmate:
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native the data here
//definition brush
cpen pen (Ps_solid,2,rgb (0,255,0));
Pdc->selectobject (&pen);
CPoint P0 (400,200);
int r=200;
CPoint P1 (400,600);
Pdc->moveto (p0);
Pdc->lineto (p1);
float pi=3.14f;
CPoint p[126];
for (int i=0;i<126;i++)
{
p[i].x=int (p0.x + r*sin ((pi+i+1)/20));
P[i].y=int (P0.y + r*cos ((pi+i+1)/20));
Pdc->moveto (p0);
Pdc->lineto (P[i]);
CBrush Bush (RGB (255,255,0));
Pdc->selectobject (&bush);
int x1=700,y1=100,x2=900,y2=400;
for (int j=0;j<50;j++)
{
pdc->rectangle (x1+j,y1+j, X2+j, y2+j);
}
The output looks like this:
Yang J Classmate:
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
cpen pen (Ps_solid,1,rgb (255,0,0));
Pdc->selectobject (&pen);
CPoint P0 (400,300);
int r=200;
CPoint P1;
p1.x=p0.x;
P1.y=p0.y+r;
Pdc->moveto (p0);
Pdc->lineto (p1);
CPoint P2;
float pi=3.14f;
P2.x=int (P0.x+r*sin (PI/20));
P2.y=int (P0.y+r*cos (PI/20));
Pdc->moveto (p0);
Pdc->lineto (p2);
CPoint p[1000];
for (int i=0;i<1000;i++) {
p[i].x=int (P0.x+r*sin (pi+i+1)/20));
P[i].y=int (P0.y+r*cos (pi+i+1)/20));
Pdc->moveto (p0);
Pdc->lineto (P[i]);
CBrush Bush (RGB (0,255,255));
Pdc->selectobject (&bush);
for (int j=0;j<200;j=j+3)
{
pdc->rectangle (600+j,100+j,800+j,400+j);
}
}
The output is shown in the following illustration:
Zhang J Classmate:
#include <math.h> void Ctest01view::ondraw (cdc* pDC) {ctest01doc* PDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here CPen pen (Ps_solid,1,rgb (255,80,0));
Pdc->selectobject (&pen);
CPoint P0 (200,200);
CPoint P1;
int r=200,p=100;
p1.x=p0.x;
P1.y=p0.y+r;
Pdc->moveto (P0);
Pdc->lineto (p1);
float pi=3.14;
CPoint p[200];
for (int i=0,h=1;i<200,i<200;i=i+2,h=h+2) {p[i].x=int (R*sin (pi+i)/20) +p0.x);
P[i].y=int (R*cos (pi+i)/20) +p0.y);
P[h].x=int (P*sin (pi+h)/20) +p0.x);
P[h].y=int (P*cos (pi+h)/20) +p0.y);
Pdc->moveto (P0);
Pdc->lineto (P[i]);
Pdc->moveto (P0);
Pdc->lineto (P[h]);
} CBrush Bush (RGB (255,190,0));
Pdc->selectobject (&bush);
int x1=500,y1=100,x2=600,y2=300;
for (int j=0;j<100;j++) {pdc->rectangle (500-j,100+j,600+j,300-j);
int x3=700,y3=100,x4=800,y4=300;
for (int k=0;k<100;k++) {pdc->ellipse (700-k,100+k,800+k,300-k); } int X5=900,y5=100,x6=1000,y6=300;
for (int l=0;l<100;l++) {pdc->rectangle (900+l,100+l,1000+l,300+l); }
}
The output is shown in the following illustration:
Zheng DD Classmate:
#include <math.h>
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
cpen pen (Ps_solid,1,rgb (0,255,0,));
Pdc->selectobject (&pen);
CPoint p1,p2;
int r=200;
p1.x=200;
p1.y=300;
p2.x=100;
P2.y=p1.y+r;
Pdc->moveto (p1);
Pdc->moveto (p2);
float pi=3.14f;
CPoint p[400];
for (int i=0;i<400;i++)
{
p[i].x = Int (R*sin (pi+i+1)/3) +p1.x);
p[i].y = Int (R*cos (pi+i+1)/6) +p1.y);
Pdc->moveto (p1);
Pdc->lineto (P[i]);
Pdc->textout (190,280,_t ("wow")), RGB (255,0,0);
float ph=3.14f;
for (int h=0;h<400;h++)
{
p[h].x = Int (R*sin (ph+h+1)/6) +p1.x);
p[h].y = Int (R*cos (ph+h+1)/3) +p1.y);
Pdc->moveto (p1);
Pdc->lineto (P[i]);
}
The output is shown in the following illustration:
Zhao BL classmate:
#include <math.h>
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native the data here
//definition brush
cpen pen (Ps_solid,2,rgb (255,32,150));
Pdc->selectobject (&pen);
Define Point
cpoint p0 (300,400);
CPoint P1 (300,600);
Pdc->moveto (p0);
Pdc->lineto (p1);
float pi=3.14f;
int r=200;
CPoint p[249];
for (int i=0; i<249; i++) {
p[i].x = Int (p0.x + r*cos ((pi+i+1)/20));
p[i].y = Int (p0.y + R*tan ((pi+i+1)/20));
Pdc->moveto (p0);
Pdc->lineto (P[i]);
CBrush Bush (RGB (255,166,123));
Pdc->selectobject (Bush);
int x=600,y=100;
int x2=900,y2=300;
Pdc->rectangle (x,y,x2,y2);
for (int j=0;j<100;j=j+4) {
pdc->rectangle (x+j,y+j,x2+j,y2+j);
}
}
The output is shown in the following illustration:
Chen Y classmate:
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
cpen pen (Ps_solid,8,rgb (132,255,255));
Pdc->selectobject (&pen);
CPoint P0 (220,300);
int r=200;
CPoint P1;
p1.x=p0.x;
P1.y=p0.y+r;
Pdc->moveto (p0);
Pdc->lineto (p1);
CPoint P2;
float pie=3.14f;
CPoint p[45];
for (int i=0;i<60;i++) {
p2.x=int (P0.x+r*sin (pie-i)/10));
P2.y=int (P0.y+r*tan (pie-i)/10));
Pdc->moveto (p0);
Pdc->lineto (p2);
}
The output is shown in the following illustration:
Text FB classmate Code:
#include <math.h>
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
cpen pen (ps_solid, 3, RGB (0, 0, 255));
Pdc->selectobject (&pen);
CPoint P0 (in);
int r =;
CPoint P1 (+ + R);
Pdc->moveto (p0);
Pdc->lineto (p1);
CPoint P2;
float PI = 3.14f;
p2.x = Int (p0.x + r*sin (PI/10));
p2.y = Int (p0.y + r*cos (PI/10));
Pdc->moveto (p0);
Pdc->lineto (p2);
CPoint p[1000];
for (int i = 0; I <300; i++)
{
p[i].x = Int (p0.x + r*sin (PI + i + 1));
p[i].y = Int (p0.y + r*cos (PI + i + 1));
r--;
Pdc->moveto (p0);
Pdc->lineto (P[i]);
}
The output is shown in the following illustration:
Liu JL classmate code as follows:
void Ctest01view::ondraw (cdc* PDC)
{
ctest01doc* pDoc = GetDocument ();
Assert_valid (PDOC);
Todo:add draw code for native data here
cpen pen (Ps_solid,2,rgb (255,0,0));
Pdc->selectobject (&pen);
CPoint P0 (300,200);
int r=170;
CPoint P1 (350,400);
Pdc->moveto (p0);
Pdc->lineto (p1);
float pi=3.14f;
CPoint p[45];
for (int i=0;i<45;i++)
{
p[i].x=int (P0.x*sin (pi+i)/20));
P[i].y=int (P0.y*cos (pi+i)/20) +r);
Pdc->moveto (p0);
Pdc->lineto (P[i]);
}
The output is shown in the following illustration:
Hope that the article for you to help, the class also need to continue to explore the content, this article mainly describes the basic knowledge of MFC drawing graphics, and then combined with computer graphics rotation and transfer applications to expand. The original course can be so, very interesting, at the same time the students ' imagination is really strong, but also very good. Next week the experts came, really busy, but again busy, write a blog when they are the most relaxed most enjoy the time, class is the same. Come on!