Windows Drawing tools: Brush CPen brush CBrush palette cpalette brushes typically have a width style and color 3 in the property constructor 1. CPen (); 2.CPen (int npenstyle, int nwidth, COLORREF crcolor), 3.CPen (int npenstyle, int nwidth, const logbrush* Plogbrush , int nstylecount = 0, const dword* lpstyle = NULL), and when the first constructor is used, you still have to call the function Cpen::createpen BOOL CreatePen (int npenstyle, int nwidth, COLORREF crcolor); BOOL CreatePen (int npenstyle, int nwidth, const logbrush* plogbrush, int nstylecount = 0, const dword* lpstyle = NULL); B OOL Createpenindirect (Lplogpen lplogpen); the involved structure typedef struct TAGLOGPEN {/* LGPN * UINT Lopnstyle; Point Lopnwidth; COLORREF Lopncolor;} logpen;typedef struct Taglogbrush {UINT lbstyle; COLORREF Lbcolor; LONG Lbhatch; } Logbrush, *plogbrush; typedef struct TAGPOINT {LONG x; LONG y;} Point; Example 1. CPen Pen;pen. CreatePen (Ps_solid,1,rgb (0,0,225)); 2.CPen *pen1=new CPen (PS_SOLID,1,RGB) brush usually has a fill color Fill pattern and fill style 3 attribute Constructors CBrush (); CBrush (Colorref crcolor); CBrush (int nIndex, COLORREF crcolor); CBrush ( cbitmap* pbitmap); If you use the constructor with no arguments, you must initialize the resulting CBrush object with Cre Atesolidbrush, Createhatchbrush, CreateBrushIndirect, CreatePatternBrush, or CreateDIBPatternBrush. Example 1.CBrush brush1 ; Must INITIALIZE!BRUSH1. CreateSolidBrush (RGB (0, 0, 255)); Blue Brush.2.cbrush Brush3 (Hs_diagcross, RGB (0, 255, 0)); CBrush Brush3 (Hs_diagcross, RGB (0, 255, 0)); 3.CBitmap bmp;bmp. LoadBitmap (Idb_brush); CBrush Brush4 (&bmp);------------------------Point------------------------------------Draw some cdc::setpixel colorref SetPixel (int x, int y, colorref crcolor); COLORREF SetPixel (Point point, colorref crcolor); Cdc::setpixelvbool Setpixelv (int x, int y, colorref crcolor); BOOL Setpixelv (Point point, Colorref crcolor),------------------------line------------------------------------draw line CDC:: MoveTo (Gets the current point cdc::getcurrentposition) CPoint MoveTo (int x, int y); CPoint MoveTo (point point); Moves the current position to the point specified by X and Y). Cdc::lineto BOOL LineTo (int x, int y); BOOL LineTo (Point point);D Raws a line from the current position up to, but not including, the point specified by X and Y (or point). The line was drawn with the selected pen. The current position are set to X, Y or to point.------------------------Ellipse and Arc------------------------------------draw ellipse CDC:: Ellipse BOOL Ellipse (int x1, int y1, int x2, int y2); BOOL Ellipse (lpcrect lpRect);D raws an Ellipse. The center of the ellipse is the center of the bounding rectangle specified by x1, y1, x2, and Y2, or lpRect. The ellipse is drawn with the current pen, and its interior are filled with the current brush. Draw elliptical Arcs Cdc::arc BOOL arc (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); BOOL ARC (Lpcrect lpRect, point Ptstart, point ptend); Since an arc isn't a closed figure, it's not filled. Elliptical arcs with chords Cdc::chord BOOL Chord (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); BOOL Chord (Lpcrect lpRect, point Ptstart, point ptend); THe chord is drawn by using the selected pen and filled by using the selected brush. Draw an elliptical arc and two endpoints of the arc connected to the center line CDC::P ie BOOL Pie (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); BOOL Pie (Lpcrect lpRect, point Ptstart, point ptend);D raws a pie-shaped wedge by drawing an elliptical arc whose center And endpoints is joined by lines. The pie-shaped area was filled with the current Brush------------------------Segments------------------------------------draw contiguous segments (starting from the first point in the array) CDC::P olyline BOOL Polyline (lppoint lpPoints, int ncount); Return Valuenonzero If the function is successful; Otherwise 0.ParameterslpPointsPoints to a array of point structures or CPoint objects to be connected.ncountspecifies the Number of points in the array. This value must is at least 2. Draw continuous segments (starting from the current point) CDC::P olylinetobool polylineto (const point* lpPoints, int ncount); A line was drawn from the current position to the first point specified by the lpPoints parameter by using the current pen. Draw Bézier curves CDC::P olybEzierbool polybezier (const point* lpPoints, int ncount); CDC::P olybeziertobool polybezierto (const point* lpPoints, int ncount); The first spline is drawn from the current position to the third point by using the first of the points as control points. Draw multiple groups Connected segments CDC::P olypolylinebool polypolyline (const point* lpPoints, const dword* lppolypoints, int ncount); Parameter lppolypointspoints to a array of variables specifying the number of points in the lpPoints array for the corresponding Polygon. Each entry must is greater than or equal to 2. Draw freeform CDC::P olygon BOOL Polygon (lppoint lpPoints, int ncount);----------- -------------rectangle------------------------------------Filled Rectangle cdc::rectangle BOOL Rectangle (int x1, int y1, int x2, int y2); BOOL Rectangle (lpcrect lpRect);D raws a Rectangle using the current pen. The interior of the rectangle is filled using the current brush. Draw a rectangle with rounded corners cdc::roundrect BOOL roundrect (int x1, int y1, int x2, int y2, int x3, int y3); BOOL RoundRect (Lpcrect lpRect, point Point); fills the rectangle with the specified color cdc::fillsolidrectvoid fillsolidrect (lpcrect lpRect, COLORREF CLR), void Fillsolidrect (int x, int y, int c x, int cy, COLORREF CLR); Remarkscall This member function to fill the given rectangle with the specified solid color. Fillsolidrect is very similar to cdc::fillrect; However, Fillsolidrect uses only solid colors (indicated by the COLORREF parameter), while FillRect takes a brush and ther Efore can used to fill a rectangle with a solid color, a dithered color, hatched brushes, or a pattern. Fillsolidrect usually is faster than FillRect. Fills the rectangle with the specified paint brush cdc::fillrectvoid FillRect (lpcrect lpRect, cbrush* Pbrush); Remarkscall This member function to fill a given rectangle using the specified brush. The function fills the complete rectangle, including the left and top borders, but it does not fill the right and bottom B Orders. Fills the rectangle with the specified brush, you can specify the style Cdc::extfloodfill BOOL extfloodfill (int x, int y, colorref crcolor, UINT nfilltype); Remarksfills an area of the display surface WITH the current brush. This member function offers more flexibility than FloodFill because you can specify a fill type in Nfilltype. Fills the display area with the current paint brush Cdc::floodfill BOOL floodfill (int x, int y, colorref crcolor); Remarksfills an area of the display surface with the current brush. The area was assumed to being bounded as specified by Crcolor. The FloodFill function begins at the point specified by X and Y and continues in all directions to the color boundary. Only memory-device contexts and devices, Raster-display technology support the FloodFill member function. The following is an example of void Cpicview::ondraw (cdc* pDC) {cpicdoc* PDoc = GetDocument (); Assert_valid (PDOC); if (!pdoc) return;//TODO: Add drawing code here for native data/*///use paint brush//Cbrush::cbrush.cbrush BRUSH1; Must INITIALIZE!BRUSH1. CreateSolidBrush (RGB (0, 0, 255)); Blue Brush. cbrush* Ptempbrush = NULL; CBrush Origbrush; CRect RC; GetClientRect (&RC); ScreenToClient (&RC),///0pTempBrush = (cbrush*) pdc->selectobject (BRUSH1);//Save original brush. OrIgbrush.fromhandle ((Hbrush) ptempbrush);//Paint upper left corner with blue brush.pdc->rectangle (0, 0, RC. Width ()/2, RC. Height ()/2);//These constructors throw resource exceptions.try{//Cbrush::cbrush (colorref crcolor) CBrush Brush2 (RGB ( 255, 0, 0)); Solid Red brush.//cbrush::cbrush (int nIndex, COLORREF crcolor)//hatched green brush. CBrush Brush3 (Hs_diagcross, RGB (0, 255, 0));//Cbrush::cbrush (cbitmap* pbitmap) CBitmap bmp;//Load a resource bitmap.bmp. LoadBitmap (Idb_brush); CBrush Brush4 (&bmp)//* 1pTempBrush = (cbrush*) pdc->selectobject (BRUSH2);//Paint upper right corner with red Brush.pdc->rectangle (RC. Width ()/2, 0, RC. Width (), RC. Height ()/2);//* 2pTempBrush = (cbrush*) pdc->selectobject (BRUSH3);//Paint lower left corner with green hatched brush. Pdc->rectangle (0, RC. Height ()/2, RC. Width ()/2,RC. Height ());//* 3pTempBrush = (cbrush*) pdc->selectobject (BRUSH4);//Paint lower right corner with resource brush.pdc-& Gt Rectangle (RC. Width ()/2, RC. Height()/2,RC. Width (), RC. Height ());} catch (cresourceexception* e) {e->reporterror (); E->delete ();} Reselect original brush into device context.pdc->selectobject (&origbrush); *//*//fill//Create and select a solid b Lue Brushcbrush brushblue (RGB (0, 0, 255)); cbrush* Poldbrush = Pdc->selectobject (&brushblue);//Create and select a thick, black pencpen penblack;penblack.cr Eatepen (Ps_solid, ten, RGB (255, 0, 0)); cpen* Poldpen = Pdc->selectobject (&penblack);//Get our client rectanglecrect rect; GetClientRect (rect);//Shrink our rect-pixels in each directionrect. Deflaterect ()//Draw a thick black rectangle filled with bluepdc->rectangle (rect);//Put back the old OBJECTSPDC ->selectobject (Poldbrush);pD c->selectobject (poldpen); *//*//Draw line CPen Penblack;penblack.createpen (PS_SOLID, 10 , RGB (255, 0, 0)); cpen* Poldpen = Pdc->selectobject (&penblack);pD C->moveto (20,20);pD C->lineto (;pD) c-> SelectObject (Poldpen); *//*//Draw ellipse CBrush brushblue (RGB (0, 0, 255)); cbrush* Poldbrush = Pdc->selectobject (&brushblue);//Create and select a thick, black pencpen penblack;penblack.cr Eatepen (Ps_solid, ten, RGB (255, 0, 0)); cpen* Poldpen = Pdc->selectobject (&penblack); //crect rectdeflate (1, 2, 3, 4), CRect Rect2 (Ten, ten, +);//rect2. Deflaterect (&rectdeflate);pD c->ellipse (rect2);pD c->selectobject (Poldbrush);pD C->selectobject ( Poldpen); *//*//Draw elliptical arc CRect rectclient; GetClientRect (rectclient);//Make a couple of pens. CPen Penblue; CPen penred; cpen* Poldpen;penblue.createpen (Ps_solid | Ps_cosmetic, 5, RGB (0, 0, 255));p Enred.createpen (Ps_solid | Ps_cosmetic, 5, RGB (255, 0, 0));//Draw from 3 o ' clock to 6 o ' clock, counterclockwise,//in a blue Pen.poldpen = Pdc->s Electobject (&penblue);pD C->arc (Rectclient,cpoint (Rectclient.right, Rectclient.centerpoint (). Y), CPoint ( Rectclient.centerpoint (). x, Rectclient.right));//-------------------------------------------------------------- -//Draw from 6 o ' clock to 3 o ' clock, counterclockwise,//in a red pen.pdc->selectobject (penred);//Keep the same parameters, but reverse start//and end Points.pdc->ar C (Rectclient,cpoint (Rectclient.centerpoint (). x, Rectclient.right), CPoint (Rectclient.right, Rectclient.centerpoint (). y));//Restore the previous pen.pdc->selectobject (Poldpen); *//*//draw elliptical arcs with chords//Get the client Area. CRect rectclient; GetClientRect (rectclient);//Make a couple of pens and similar brushes. CPen Penblue, penred; CBrush Brushblue, brushred; cbrush* Poldbrush; cpen* Poldpen;brushblue.createsolidbrush (RGB (0, 0, 255)), Brushred.createhatchbrush (hs_fdiagonal, RGB (255, 0, 0)); Penblue.createpen (Ps_solid | Ps_cosmetic, 1, RGB (0, 0, 255));p Enred.createpen (Ps_solid | Ps_cosmetic, 5, RGB (255, 0, 0));//Draw from 3 o ' clock to 6 o ' clock, counterclockwise,//in a blue pen with a solid blue f Ill.poldpen = Pdc->selectobject (&penblue);p Oldbrush = Pdc->selectobject (&brushblue);pD C->chord ( Rectclient,cpoint (Rectclient.right, Rectclient.centerpoint (). Y), CPoint (recTclient.centerpoint (). x, rectclient.right);//Draw the remaining quarter chord from 6 o ' clock//to 3 o ' clock, countercloc Kwise, in a red pen//with the hatched brush.pdc->selectobject (&penred);pD c->selectobject (&brushred);// Keep the same parameters, but reverse start and//end Points.pdc->chord (Rectclient,cpoint (Rectclient.centerpoint (). x , Rectclient.right), CPoint (Rectclient.right, Rectclient.centerpoint (). y));//Restore the previous pen.pdc-> SelectObject (Poldpen); *//*//draws an elliptical arc and two endpoints of the arc connected to the center of the circle//Get the client area. CRect rectclient; GetClientRect (rectclient);//Make a couple of pens and similar brushes. CPen Penblue, penred; CBrush Brushblue, brushred; cbrush* Poldbrush; cpen* Poldpen;brushblue.createsolidbrush (RGB (0, 0, 255)), Brushred.createhatchbrush (hs_fdiagonal, RGB (255, 0, 0)); Penblue.createpen (Ps_solid | Ps_cosmetic, 1, RGB (0, 0, 255));p Enred.createpen (Ps_solid | Ps_cosmetic, 5, RGB (255, 0, 0));//Draw from 3 o ' clock to 6 o ' clock, counterclockwise,//in a blue pen wiTh a solid blue Fill.poldpen = Pdc->selectobject (&penblue);p Oldbrush = Pdc->selectobject (&brushblue); Pdc->pie (Rectclient,cpoint (Rectclient.right, Rectclient.centerpoint (). Y), CPoint (Rectclient.centerpoint (). x, Rectclient.right)//Draw The remaining quarter slice from 6 o ' clock//to 3 o ' clock, counterclockwise, in a red pen with/ /The hatched brush.pdc->selectobject (&penred);pD c->selectobject (&brushred);//Same parameters, but Reverse Start and End Points.pdc->pie (Rectclient,cpoint (Rectclient.centerpoint (). x, Rectclient.right), CPoint ( Rectclient.right, Rectclient.centerpoint (). y));//Restore the previous pen.pdc->selectobject (Poldpen); *//*// Draw a rectangle with rounded corners//create and select a solid blue Brushcbrush brushblue (RGB (0, 0, 255)); cbrush* Poldbrush = Pdc->selectobject (&brushblue);//Create and select a thick, black pencpen penblack;penblack.cr Eatepen (Ps_solid, 3, RGB (0, 0, 0)); cpen* Poldpen = Pdc->selectobject (&penblack);//Get our client RectanglEcrect rect; GetClientRect (rect);//Shrink our rect-pixels in each directionrect. Deflaterect ()//Draw a thick black rectangle filled with blue//corners rounded at a 17-unit radius. Note that//A radius of three or less are noticable because//the pen is three units wide.pdc->roundrect (rect, Cpoin T (());//Put back the old Objectspdc->selectobject (Poldbrush);pD c->selectobject (poldpen); *//*//Draw continuous segments CPen Penblack;penblack.createpen (Ps_solid, 5, RGB (0, 255, 0)); cpen* Poldpen = Pdc->selectobject (&penblack); Point Pt[4] = {{4, +}, {4, $}, {+, +]};pD c->polyline (PT,);pD;pD C ->selectobject (Poldpen); *//*//draw freeform//Find the client areacrect rect; GetClientRect (rect);//draw with a thick blue pencpen penblue (Ps_solid, 5, RGB (0, 0, 255)); cpen* Poldpen = Pdc->selectobject (&penblue),//and a solid red Brushcbrush brushred (RGB (255, 0, 0)); cbrush* Poldbrush = Pdc->selectobject (&brushred);//Find theMidpoints of the top, right, left, and bottom//of the client area. They'll is the vertices of our polygon. CPoint pts[4];p ts[0].x = rect.left + rect. Width ()/2;pts[0].y = rect.top;pts[1].x = Rect.right;pts[1].y = Rect.top + rect. Height ()/2;pts[2].x = Pts[0].x;pts[2].y = rect.bottom;pts[3].x = Rect.left;pts[3].y = pts[1].y;//Calling Polygon () on t Hat array would draw three lines//between the points, as well as a additional line to//close the Shape--from the last PO int to the first point//we Specified.pdc->polygon (pts, 4);//Put back the old Objects.pdc->selectobject (Poldpen);pD C->selectobject (Poldbrush); *//*pdc->fillsolidrect (0, 0, N, N, RGB (0, 255, 0)); */}
VC + + Drawing function