Drawing 2.2 with GDI
2.2.1 drawing lines and curves
DC CPaintDC (the this); // draw a line, (0,0) for the top left corner of the screen dc.MoveTo (0,0); // starting point dc.LineTo (0,100); // end // a rectangle POINT aPoint [ . 5] = {0,0, 0,100, 100,100, 100,0, 0,0}; dc.Polyline (a aPoint,. 5); dc.MoveTo (0,0); the POINT a aPoint [. 4] = {0,100, 100,100, 100,0, 0,0}; dc.PolylineTo (a aPoint,. 4); // Videos arcuate CRect RECT (0,0,200,100); a CPoint Point1 (0,100); a CPoint Point2 (200,100); dc.Arc (RECT, Point1 , Point2); // draw a Bezier curve the POINT aPoint1 [. 4] = {120,100, 120,200, 250,150, 500,40}; dc.PolyBezier (aPoint1,4);
2.2.2 painted oval / polygon / shape other
// painted oval dc.Ellipse (0,0,100,100); CRect RECT (0,0,100,100); dc.Ellipse (RECT); // draw a rectangle dc.Rectangle (0,0,800,400);
2.2.3 GDI brush and CPen class
The default is one pixel wide pen solid black line, if you need to create a GDI pen must CDC :: SelectObject () which will be selected into the device context
PEN a CPen (the PS_SOLID,. 1, the RGB (255,0,0)); // pen style: the PS_SOLID PS_DASH PS_DOT PS_DASHDOT PS_DASHDOTDOT PS_NULL PS_INSIDEFRAME a CPen PEN; pen.CreatePen (the PS_SOLID,. 1, the RGB (255,0,0)); // RGB primary colors black (0,0,0) white (255,255,255) red (192,0,0) yellow (192,192,0) green (0,192,0) cyan (0,192,192) blue (0,0,192)
2.2.4 GDI brush and CBrush class
The default closed figure shown is filled with white pixels, by creating a GDI brush and it is selected into the device context, you can change the fill color pattern
CBrush brush(RGB(255,0,0)); CBrush brush; brush.CreateSolidBrush(RGB(255,0,0)); //画刷样式:HS_BDIAGONAL HS_FDIAGONAL HS_CROSS HS_HORIZONTAL HS_DIAGCROSS HS_VERTICAL CBrush brush(HS_DIAGCROSS,RGB(255,255,255));
2.2.5 text painting
// draw text in the middle of the screen CRect RECT; GetClientRect (& RECT); dc.DrawText (_T ( "the Hello, MFC"), - 1, & RECT, DT_SINGLELINE | DT_CENTER | DT_VCENTER); dc.TextOut (0, 0, CString ( _T ( "the Hello the MFC"))); // change the text attributes dc.SetTextAlign (TA_RIGHT); dc.SetTextColor () / dc.SetBkColor ()
2.2.6 GDI font type and CFont
CFont font; font.CreatePointFont(120,_T("Times New Roman")); CPaintDC dc(this); dc.SelectObject(&font);
2.2.7 raster fonts and TrueType fonts
TrueType fonts can be scaled to any size: Times New Roman / Arial / Courier New / Symbol
2.2.8 rotated fonts
2.2.9 backup objects
There are some ready-made spare interior windows objects (such as the pen / brush / fonts, etc.), so do not create our own
2.2.10 delete GDI objects
And GDI objects associated CGdiObject by calling CGdiObject :: DeleteObject explicitly deleted
// source .cpp // add a first row #ifdef _DEBUG #define new new DEBUG_NEW #undef THIS_FILE static char THIS_FILE [] = __FILE__; #endif // added at the end of the function _CrtDumpMemoryLeaks (); // check VS debugging information, which will The first few lines of the file has a memory leak
2.2.11 deselecting GDI object
We want to delete GDI objects, it is best to select an object from the GDI DC environment inside, in order to avoid a crash, the best way is to use a spare to replace the object we need to remove the objects
// spare target audiences to replace a CPen PEN (the PS_SOLID,. 1, the RGB (255,0,0)); dc.SelectObject (& PEN); the CBrush Brush (the RGB (0,0,255)); dc.SelectObject (& Brush) ; ... dc.SelectStockObject (BLACK_PEN); dc.SelectStockObject (WHITE_BRUSH); the Delete PEN; the Delete Brush;