Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 7: GDI Accessories and Tools


Practical Learning: Drawing Polylines


To draw a polyline, change the event as follows:
void CView1View::OnPaint()
{
CPaintDC dc(this); // device context for painting


// TODO: Add your message handler code here
CPoint PtLine[] = { CPoint( 50, 50), CPoint(670, 50),
CPoint(670, 310), CPoint(490, 310),
CPoint(490, 390), CPoint(220, 390),
CPoint(220, 310), CPoint( 50, 310),
CPoint( 50, 50) };
CPoint PlLine[] = { CPoint( 55, 55), CPoint(665, 55),
CPoint(665, 305), CPoint(485, 305),
CPoint(485, 385), CPoint(225, 385),
CPoint(225, 305), CPoint( 55, 305),
CPoint(55, 55) };


dc.MoveTo(PtLine[0]);
dc.LineTo(PtLine[1]);
dc.LineTo(PtLine[2]);
dc.LineTo(PtLine[3]);
dc.LineTo(PtLine[4]);
dc.LineTo(PtLine[5]);
dc.LineTo(PtLine[6]);
dc.LineTo(PtLine[7]);
dc.LineTo(PtLine[8]);


dc.Polyline(PlLine, 9);
// Do not call CView::OnPaint() for painting messages
}


Test the application


6.3.3 Multiple Polylines................................................................................


The above polylines were used each as a single entity. That is, a polyline is a combination
of lines. If you want to draw various polylines in one step, you can use the
CDC::PolyPolyline() method. By definition, the PolyPolyline() member function is used
to draw a series of polylines. Its syntax is:

BOOL PolyPolyline(const POINT* lpPoints, const DWORD* lpPolyPoints, int nCount);

Like the above Polyline() method, the lpPoints argument is an array of POINT or CPoint
values. The PolyPolyline() method needs to know how many polylines you would be
drawing. Each polyline will use the points of the lpPoints value but when creating the
array of points, the values must be incremental. This means that PolyPolyline() will not
access their values at random. It will retrieve the first point, followed by the second,
followed by the third, etc. Therefore, your first responsibility is to decide where one
polyline starts and where it ends. The good news (of course depending on how you see it)
Free download pdf