MFC has a powerful function and has a complete drawing function. On the Windows platform, the application's Graphics Device interface (INTERFACE,GDI) is abstracted into device CONTEXT,DC. In Microsoft's base Class library MFC, the CDC class is the base class that defines the device context object, and all the drawing functions are defined in the CDC base class. When you need to output text or graphics, you need to call the CDC class's member functions, which have the ability to output text and draw graphics. The routines explained in this article are all implemented in the void Ctestview::ondraw (cdc* pDC) function of the TestView.cpp file. 1. Draw a blue pixel point at the P0 (20,20) coordinate of the screen, then read out the color of the pixel, and horizontally pan 100 pixels to draw the P1 point; draw a rectangle. The code is as follows:
1 voidCmy00view::ondraw (cdc*PDC)2 {3cmy00doc* PDoc =getdocument ();4 assert_valid (PDOC);5 //Todo:add Draw code for native data here6 7 COLORREF CLR;8 intx= -, y= -;9Pdc->setpixelv (X,y,rgb (255,0,0));TenClr=pdc->GetPixel (x, y); OnePdc->setpixelv (x+ -, Y,CLR);//Pixel points APdc->rectangle ( -, $, $, -);//Rectangle -}
The effect is as follows:
2. Draw a blue line code of 1 pixels wide from start P0 (20,30) to end point P1 (200,300) as follows:
1 voidCmy00view::ondraw (cdc*PDC)2 {3cmy00doc* PDoc =getdocument ();4 assert_valid (PDOC);5 //Todo:add Draw code for native data here6 //draw a Blue Line 1 pixels wide from the beginning P0 (20,30) to the end point P1 (200,300),7CPen newpen,*P01dpen;8Newpen.createpen (Ps_solid,1, RGB (0,0,255));9P01dpen=pdc->selectobject (&newpen);TenPdc->moveto ( -, -); OnePdc->lineto ( $, -); APdc->SelectObject (p01dpen); - Newpen.deleteobject (); -}
The results are as follows: 3. Draw the rectangle with the upper-left corner of P0 (100,100) and the lower-right corner to P1 (600,300). The rectangular boundary line is 1 pixels wide blue, and the interior of the rectangle is filled with green code as follows:
1 voidCmy00view::ondraw (cdc*PDC)2 {3cmy00doc* PDoc =getdocument ();4 assert_valid (PDOC);5 //Todo:add Draw code for native data here6CPen newpen,*P01dpen;7Newpen.createpen (Ps_solid,1, RGB (0,0,255));8P01dpen=pdc->selectobject (&newpen);9CBrush newbrush,*P01dbrush;TenNewbrush.createsolidbrush (RGB (0,255,0)); OneP01dbrush=pdc->selectobject (&Newbrush); APdc->rectangle ( -, -, -, -); -Pdc->SelectObject (p01dbrush); - Newbrush.deleteobject (); the}
The effect is as follows:
Draw simple graphics with MFC's drawing tools from Microsoft vc++6.0