【问题标题】:GraphicsPath AddArcGraphicsPath AddArc
【发布时间】:2014-08-02 15:05:14
【问题描述】:

我正在尝试创建一个带有圆形矩形和底部的弹出气泡,我在下面的图像中用绿色圈出:

但正如你猜到的那样,这不是我想要的样子,而是:

我想出了如何按我应该的方式绘制它,但在这种情况下,当我在路径周围添加边框时,它会将那个小三角形和圆角矩形分开。

这是我正在使用的代码:

public static GraphicsPath CreateBubblePath(Rectangle rect, int radius)
{
    GraphicsPath p = new GraphicsPath();
    GraphicsPath p2 = new GraphicsPath();
    p.StartFigure();
    p2.StartFigure();

    int pointHeigt = 3 * radius;

    //Top Left Corner
    p.AddArc(rect.X, rect.Y, 2 * radius, 2 * radius, 180, 90);

    //Top Edge
    p.AddLine(rect.X + radius, rect.Y, rect.X + rect.Width - radius, rect.Y);

    //Top Right Corner
    p.AddArc(rect.X + rect.Width - 2 * radius, rect.Y, 2 * radius, 2 * radius, 270, 90);


    //Right Edge
    p.AddLine(rect.X + rect.Width, rect.Y + radius, rect.X + rect.Width, rect.Y + rect.Height - radius - pointHeigt);

    //Bottom Right Corner
    p.AddArc(rect.X + rect.Width - (2 * radius), rect.Y + rect.Height - radius - pointHeigt, 2 * radius, 2 * radius, 0, 90);

    //Bottom Edge 1/2
    //METHOD 1
    p.AddLine(rect.X + rect.Width - radius, rect.Y + rect.Height - 2 * radius, rect.X + (rect.Width / 2) + pointHeigt, rect.Y + rect.Height - 2 * radius);

    p.AddArc(rect.X + (rect.Width / 2), rect.Y + rect.Height - 2 * radius , pointHeigt, pointHeigt, 180, 90);
    p.AddArc(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 270, 90);

    p.AddLine(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, rect.X + radius, rect.Y + rect.Height - 2 * radius);

    //METHOD 2///////////////////////////////////////
    //p.AddLine(rect.X + rect.Width - radius, rect.Y + rect.Height - 2 * radius, rect.X + radius, rect.Y + rect.Height - 2 * radius);
    //p2.AddArc(rect.X + (rect.Width / 2), rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 180, 90);
    //p2.AddArc(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 270, 90);
    //p2.CloseFigure();
    ////////////////////////////////

    //Bottom Left Corner
    p.AddArc(rect.X, rect.Y + rect.Height - radius - pointHeigt, 2 * radius, 2 * radius, 90, 90);

    //Left Edge
    p.AddLine(rect.X, rect.Y + rect.Height - radius - pointHeigt, rect.X, rect.Y + radius);

    p.CloseFigure();
    //METHOD 2
    //p.AddPath(p2, true);
    return p;
}

【问题讨论】:

    标签: c# winforms shapes graphicspath


    【解决方案1】:

    我认为弧的角度是问题。

    我把它们改成了这样:

    p.AddArc(rect.X + (rect.Width / 2), rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 270, -90);
    p.AddArc(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 0, -90);
    

    矩形底部的点现在看起来是正确的。

    有关圆弧角度的信息可以在MSDN page for GraphicsPath.AddArc 上找到,特别是在备注部分:

    弧线沿椭圆的周长绘制,由 指定的矩形。弧的起点由下式确定 从椭圆的 x 轴顺时针测量(在 0 度 角度)按起始角度的度数。端点是 通过从起点顺时针测量类似地定位 扫角的度数。如果扫掠角为 大于 360 度或小于 -360 度,弧被扫过 分别正好 360 度或 -360 度。

    【讨论】:

      【解决方案2】:

      好的,所以我不知道我可以在sweepAngle 中添加一个负数,所以我只更改了以下两行:

      来自

      p.AddArc(rect.X + (rect.Width / 2), rect.Y + rect.Height - 2 * radius , pointHeigt, pointHeigt, 180, 90);
      p.AddArc(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 270, 90);
      

      到:

          p.AddArc(rect.X + (rect.Width / 2), rect.Y + rect.Height - 2 * radius , pointHeigt, pointHeigt, 270, -90);
          p.AddArc(rect.X + (rect.Width / 2) - pointHeigt, rect.Y + rect.Height - 2 * radius, pointHeigt, pointHeigt, 0, -90);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 2010-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多