在控件内部绘制比例三角形 [英] Draw proportional triangle inside control
问题描述
数学不是我的专长,因为这比我用C#标签标记的VB问题更是一个数学问题.
Maths are not my speciality and as this is more a math question than a VB question I tagged it with C# tag too.
我需要帮助在自定义用户控件的工作区域(客户端矩形)内绘制一个三角形,但是我无法设置正确的坐标(我无法通过Me.Left,Me.Right, Me.Top和Me.Bottom ...),下面是绘制矩形的相关代码:,但是无论如何,我不确定我是否在使用野兽方法(因为控件的客户矩形区域).
I need help to draw a triangle inside the working area (client rectangle) of a custom user control but I fail setting the right coordinates (I fail trying to calculate the right coordinates doing operations with Me.Left, Me.Right, Me.Top and Me.Bottom...), here is the relevant code to draw the rectangle:, but anyways I'm not sure whether I'm using the beast approach (because the control's client rectangle area).
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)
如果这是我的控制权
矩形结果应该是这样的,因为您会看到矩形尊重控件的比例/大小:
The rectangle result should be like this, as you'll see the rectangle respects the proportions/size of the control:
推荐答案
下面是一个如何绘制三角形的示例.请注意,您还需要将笔的宽度纳入方程式中.另外,您需要绘制path
,而不是线条.
Here's an example of how to draw a triangle. Note that you also need take the width of the pen into the equations. Also, you need to draw the path
, not the lines.
pt1:
顶部中央,pt2:
底部-右侧,pt3:
底部-左侧.
pt1:
top center, pt2:
bottom - right, pt3:
bottom - left.
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 Using
这篇关于在控件内部绘制比例三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!