Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


// 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) };


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]);
// Do not call CView::OnPaint() for painting messages
}


Test the application


6.3.2 Polylines................................................................................................


A polyline is a series of connected lines. The lines are stored in an array of POINT or
CPoint values. To draw a polyline, you use the CDC::Polyline() method. Its syntax is:

BOOL Polyline(LPPOINT lpPoints, int nCount);

The lpPoints argument is an array of points that can be of POINT or CPoint types. The
nCount argument specifies the number of members of the array. When executing, the
compiler moves the starting point to lpPoints[0]. The first line is drawn from lpPoints[0]
to lpPoints[1] as in:

void CExoView::OnDraw(CDC* pDC)
{
CPoint Pt[] = { CPoint(60, 20), CPoint(60, 122) };
pDC->MoveTo(Pt[0]);
pDC->LineTo(Pt[1]);
}

To draw a polyline, you must have at least two points. If you define more than two
points, each line after the first would be drawn from the previous point to the next point
until all points have been included. Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
CPoint Pt[7];
Pt[0] = CPoint(20, 50);
Pt[1] = CPoint(180, 50);
Pt[2] = CPoint(180, 20);
Pt[3] = CPoint(230, 70);
Pt[4] = CPoint(180, 120);
Pt[5] = CPoint(180, 90);
Pt[6] = CPoint(20, 90);
Free download pdf