2

MFC が提供する GDI 関数 Chord() を次に示します。

BOOL Chord( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );

BOOL Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

そのようなメソッドは GDI+ (Graphics クラス) によって提供されないように思われるので、独自の Chord 関数を (まったく同じプロトタイプで) 実装するにはどうすればよいですか?

ちなみに、なぜMSがそれらを提供しないのか理解できません。

ありがとう。

4

1 に答える 1

1
BOOL GDIplusChord( HDC hDC, INT x1, INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4 )
{
    Graphics graphics(hDC);
    CRect rectBound(x1, y1, x2, y2);
    rectBound.NormalizeRect();
    Rect ellipseRect(rectBound.left, rectBound.top, rectBound.Width(), rectBound.Height());

    ////////////////////////////////////////////////////////////////////////////////
    // For testing only.
    BYTE        byAlpha = 200;
    SolidBrush  fillBrush(Color(byAlpha, 0, 0, 255));
    Pen         redPen(Color(byAlpha, 255, 0, 0), 3);
    ////////////////////////////////////////////////////////////////////////////////
    Status ret = InvalidParameter;

    if ( x3 == x4 && y3 == y4 )
    {
        // If the starting point and ending point of the curve are the same, a complete ellipse should be drawn. 
        ret = graphics.FillEllipse(&fillBrush, ellipseRect);
        ret = graphics.DrawEllipse(&redPen, ellipseRect);
        return Gdiplus::Ok == ret;
    }

    CPoint ptCenter(rectBound.CenterPoint());

#define PI 3.1415926

    REAL startAngle = atan2(y3-ptCenter.y, x3-ptCenter.x ) * 180.0f / PI;
    REAL sweepAngle = (atan2(y4-ptCenter.y, x4-ptCenter.x ) * 180.0f / PI) - startAngle - 360.0f;
    if ( sweepAngle < -360.0f )
        sweepAngle += 360.0f;

    GraphicsPath path;
    path.StartFigure();
    path.AddArc(ellipseRect, startAngle, sweepAngle);
    path.CloseFigure();

    ret = graphics.FillPath(&fillBrush, &path);
    ret = graphics.DrawPath(&redPen, &path);

    return Gdiplus::Ok == ret;
}
于 2010-05-14T08:18:50.853 に答える