如何使用GDIplus实现和弦功能?

发布于 2024-09-01 01:58:07 字数 312 浏览 8 评论 0原文

以下是 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 函数(具有完全相同的原型)?

顺便说一句,我只是不明白为什么微软不提供它们。

谢谢。

Here are the GDI functions Chord() provided by MFC:

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

It seems to me that no such method is privided by GDI+ (the Graphics class), so how do I implement my own Chord function (with the exact same prototype) ?

By the way, I just don't understand why does MS just don't provide them.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-09-08 01:58:07
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文