数学不是我的专长,因为这更多的是一个数学问题,而不是一个VB问题,我也用C#标签标记它。
我需要帮助在自定义用户控件的工作区(客户端矩形)内绘制一个三角形,但我无法设置正确的坐标(我无法计算正确的坐标,使用Me.Left、Me.Right、Me.Top和Me.Bottom.),下面是绘制矩形的相关代码:但无论如何,我不确定是否使用了猛兽方法(因为控件的客户端矩形区域)。
    Dim ptsArray As PointF() =
        {
            New PointF(0, 0),
            New PointF(0, 0),
            New PointF(0, 0),
            New PointF(0, 0)
        }
    Dim gp As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
    gp.AddLines(ptsArray)
    gp.CloseFigure()
    e.Graphics.FillPath(Brushes.Red, gp)
    e.Graphics.DrawLines(Pens.Black, ptsArray)如果这是我的控制

矩形结果应该如下所示,因为您将看到矩形尊重控件的比例/大小:

发布于 2014-11-09 11:29:31
这是一个如何画三角形的例子。请注意,您还需要将笔的宽度放入方程中。另外,您需要绘制path,而不是线条。
pt1:顶部中心,pt2:右下角,pt3:左下角。
Using pen As New Pen(Brushes.Red, 10)
    Dim rect As Rectangle = Me.ClientRectangle
    Dim pt1 As New PointF(CSng(rect.Left + (rect.Width / 2)), (rect.Top + pen.Width))
    Dim pt2 As New PointF((rect.Right - pen.Width), (rect.Bottom - pen.Width))
    Dim pt3 As New PointF((rect.Left + pen.Width), (rect.Bottom - pen.Width))
    Using path As New Drawing2D.GraphicsPath(FillMode.Winding)
        path.AddLines({pt1, pt2, pt3, pt1})
        path.CloseFigure()
        e.Graphics.DrawPath(pen, path)
    End Using
End Usinghttps://stackoverflow.com/questions/26826538
复制相似问题