Draw a Triangle using the GraphicsPath::AddLine Method
This example simply adds three lines to a GDI+ GraphicsPath object to create a triangle. There is no checking performed, at this level, to determine whether the lines do actually constitute a valid triangle.
// Create a graphics path to hold the triangle sides.
m_pPath = new GraphicsPath();
if(m_pPath != NULL)
{
// Insert the three lines
m_pPath->AddLine(m_lX1Pos, m_lY1Pos, m_lX2Pos, m_lY2Pos);
m_pPath->AddLine(m_lX2Pos, m_lY2Pos, m_lX3Pos, m_lY3Pos);
m_pPath->AddLine(m_lX3Pos, m_lY3Pos, m_lX1Pos, m_lY1Pos);
// Determine whether any scaling is required
Matrix* pScaleMatrix = new Matrix();
this->ScaleShape(hWnd, &pScaleMatrix);
m_pPath->Transform(pScaleMatrix);
// Create a region based on the path object
m_pRegion = new Region(m_pPath);
// Use the specified RGB values to draw the path
Color colorPen(m_ucAlpha, m_ucRed, m_ucGreen, m_ucBlue);
Pen drawPen(colorPen);
// Apply antialiasing to the drawing operations
pCanvas->SetSmoothingMode(SmoothingModeAntiAlias);
// Draw the triangle
pCanvas->DrawPath(&drawPen, m_pPath);
// Fill the triangle if it has been previously selected
if(m_bSelected)
{
ULONG ulARGBColour = Color::MakeARGB(m_ucAlpha, m_ucRed, m_ucGreen, m_ucBlue);
this->Fill(pCanvas, ulARGBColour);
}
// Clean up resources
if(pScaleMatrix != NULL)
{
delete pScaleMatrix;
pScaleMatrix = NULL;
}
}