使用GDI绘制带箭头的直线
int DrawArrowLine(HDC hDC, LPPOINT lpPointStart, LPPOINT lpPointEnd, double nArrowBorderLen, double xAngleInRadians, COLORREF cr)
{
const double PI = 3.1415926;
double xAngleLine = 0;
if (lpPointStart->x == lpPointEnd->x)
xAngleLine = PI / 2;
else
{
xAngleLine = atan((double(lpPointEnd->y) - double(lpPointStart->y)) / (double(lpPointEnd->x) - double(lpPointStart->x)));
}
CPoint ptLineLeft;
if (lpPointStart->x > lpPointEnd->x)
{
ptLineLeft.x = long(lpPointEnd->x + cos(xAngleLine + xAngleInRadians) * nArrowBorderLen);
ptLineLeft.y = long(lpPointEnd->y + sin(xAngleLine + xAngleInRadians) * nArrowBorderLen);
}
else if (lpPointStart->x == lpPointEnd->x)
{
if (lpPointStart->y > lpPointEnd->y)
{
ptLineLeft.x = long(lpPointEnd->x + cos(xAngleLine + xAngleInRadians) * nArrowBorderLen);
ptLineLeft.y = long(lpPointEnd->y + sin(xAngleLine + xAngleInRadians) * nArrowBorderLen);
}
else if (lpPointStart->y == lpPointEnd->y)
{
return 0;
}
else
{
ptLineLeft.x = long(lpPointEnd->x - cos(xAngleLine + xAngleInRadians) * nArrowBorderLen);
ptLineLeft.y = long(lpPointEnd->y - sin(xAngleLine + xAngleInRadians) * nArrowBorderLen);
}
}
else
{
ptLineLeft.x = long(lpPointEnd->x - cos(xAngleLine + xAngleInRadians) * nArrowBorderLen);
ptLineLeft.y = long(lpPointEnd->y - sin(xAngleLine + xAngleInRadians) * nArrowBorderLen);
}
CPoint ptLineRight;
if (lpPointStart->x > lpPointEnd->x)
{
ptLineRight.x = long(lpPointEnd->x + cos(xAngleLine - xAngleInRadians) * nArrowBorderLen);
ptLineRight.y = long(lpPointEnd->y + sin(xAngleLine - xAngleInRadians) * nArrowBorderLen);
}
else if (lpPointStart->x == lpPointEnd->x)
{
if (lpPointStart->y > lpPointEnd->y)
{
ptLineRight.x = long(lpPointEnd->x + cos(xAngleLine - xAngleInRadians) * nArrowBorderLen);
ptLineRight.y = long(lpPointEnd->y + sin(xAngleLine - xAngleInRadians) * nArrowBorderLen);
}
else if (lpPointStart->y > lpPointEnd->y)
{
return 0;
}
else
{
ptLineRight.x = long(lpPointEnd->x - cos(xAngleLine - xAngleInRadians) * nArrowBorderLen);
ptLineRight.y = long(lpPointEnd->y - sin(xAngleLine - xAngleInRadians) * nArrowBorderLen);
}
}
else
{
ptLineRight.x = long(lpPointEnd->x - cos(xAngleLine - xAngleInRadians) * nArrowBorderLen);
ptLineRight.y = long(lpPointEnd->y - sin(xAngleLine - xAngleInRadians) * nArrowBorderLen);
}
HPEN hPen = ::CreatePen(PS_SOLID, 1, cr);
LOGBRUSH lb;
lb.lbStyle = BS_SOLID;
lb.lbColor = cr;
HBRUSH hBrush = ::CreateBrushIndirect(&lb);
HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
// Draw line
::MoveToEx(hDC, lpPointStart->x, lpPointStart->y, NULL);
::LineTo(hDC, lpPointEnd->x, lpPointEnd->y);
// Draw arrow
HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, hBrush);
POINT ptPolygon[3] = {lpPointEnd->x, lpPointEnd->y, ptLineLeft.x, ptLineLeft.y, ptLineRight.x, ptLineRight.y};
::Polygon(hDC, ptPolygon, 3);
::SelectObject(hDC, hOldBrush);
::SelectObject(hDC, hOldPen);
::DeleteObject(hBrush);
::DeleteObject(hPen);
return 0;
}