在 vb.net 中创建自定义多边形 Class
Creating a Custom Polygon Class in vb.net
我想自定义 polygon shape
class,我可以随意将其拖放到我的表单中(就像 OvalShape
和 RectangleShape
VS 工具箱中的对象)。我检查了 site1, site2 and site3,其中一个特别指出我的表单的 OnPaint
事件应该被覆盖。有什么方法可以在创建自定义多边形形状时实现相同的效果,并且仍然让我的多边形出现在工具箱中?
编辑:
@Jens:我希望控件使用以下经过测试的代码生成其代码:
Me.ClientSize = New Point(24, 24)
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
Dim pts(9) As Point
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
这给了我一颗有 5 个尖峰的星星。我如何将它们存储在创建的 Points 变量中,
或
将点存储为一个区域,这样每当我更改 forecolor
时,它就会用选定的颜色填充该区域(即 polygn)。我还想防止绘制 backcolor
。请查看下面的链接,找到我真正想要的 c# 解决方案,但我不擅长将 c# 转换为 vb。
非常感谢
我不完全确定这就是您想要的。您始终可以从 Control
派生一个新的 class 并使用它的 Paint
事件来绘制任何您喜欢的东西。在你的例子中是一个多边形。
因此,该控件包含 Points
属性,它只是定义多边形边缘的 PointF
值的数组。通过使用 DesignerSerializationVisible.Content
属性,您可以直接通过设计器编辑这些值。代码如下所示:
Public Class PolygonControl
Inherits Control
Private _Points(2) As PointF
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)>
Public Property Points As PointF()
Get
Return _Points
End Get
Set(value As PointF())
_Points = value
End Set
End Property
Public Property LineColor As Color = Color.Black
Public Property LineWidth As Integer = 2
Private Sub PolygonControl_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.Clear(Me.BackColor)
If Points IsNot Nothing AndAlso Points.Count > 1 Then
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Using b As New SolidBrush(ForeColor)
Using p As New Pen(b, LineWidth)
e.Graphics.DrawPolygon(p, Points)
End Using
End Using
End If
End Sub
End Class
我还添加了颜色和宽度 属性。这只是为了给你一个大概的想法。注意 Points 属性.
上方的属性
在 "action" 中看起来像这样:
添加更好的设计师支持当然是可能的,但我对此没有任何经验。但这是一个开始。
编辑
因为你总是绘制相同的形状,所以你可以在控件的构造函数中预先计算点,然后在 paint 事件中绘制形状:
Public Class StarControl
Inherits Control
'Storage for the shape's points
Private pts(9) As Point
'Constructor
Public Sub New()
Me.ClientSize = New Size(24, 24)
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
'Precalculate the shape
Dim cx = CInt(Me.ClientSize.Width / 2)
Dim cy = CInt(Me.ClientSize.Height / 2)
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
ReDim pts(9)
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
End Sub
Public Property LineColor As Color = Color.Black
Public Property FillColor As Color = Color.Gold
Public Property LineWidth As Integer = 1
Public Sub PaintMe(sender As Object, e As PaintEventArgs) Handles Me.Paint
'Draw the precalculated shape
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Using b As New SolidBrush(FillColor)
e.Graphics.FillPolygon(b, pts)
End Using
Using b As New SolidBrush(LineColor)
Using p As New Pen(b, LineWidth)
e.Graphics.DrawPolygon(p, pts)
End Using
End Using
End Sub
End Class
要获得更酷的效果,请将形状计算移动到 Paint 事件处理程序中,以便形状根据控件的大小自行调整大小。这允许您绘制任意大小的星星。
我想自定义 polygon shape
class,我可以随意将其拖放到我的表单中(就像 OvalShape
和 RectangleShape
VS 工具箱中的对象)。我检查了 site1, site2 and site3,其中一个特别指出我的表单的 OnPaint
事件应该被覆盖。有什么方法可以在创建自定义多边形形状时实现相同的效果,并且仍然让我的多边形出现在工具箱中?
编辑: @Jens:我希望控件使用以下经过测试的代码生成其代码:
Me.ClientSize = New Point(24, 24)
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
Dim pts(9) As Point
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
这给了我一颗有 5 个尖峰的星星。我如何将它们存储在创建的 Points 变量中,
或
将点存储为一个区域,这样每当我更改 forecolor
时,它就会用选定的颜色填充该区域(即 polygn)。我还想防止绘制 backcolor
。请查看下面的链接,找到我真正想要的 c# 解决方案,但我不擅长将 c# 转换为 vb。
非常感谢
我不完全确定这就是您想要的。您始终可以从 Control
派生一个新的 class 并使用它的 Paint
事件来绘制任何您喜欢的东西。在你的例子中是一个多边形。
因此,该控件包含 Points
属性,它只是定义多边形边缘的 PointF
值的数组。通过使用 DesignerSerializationVisible.Content
属性,您可以直接通过设计器编辑这些值。代码如下所示:
Public Class PolygonControl
Inherits Control
Private _Points(2) As PointF
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)>
Public Property Points As PointF()
Get
Return _Points
End Get
Set(value As PointF())
_Points = value
End Set
End Property
Public Property LineColor As Color = Color.Black
Public Property LineWidth As Integer = 2
Private Sub PolygonControl_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.Clear(Me.BackColor)
If Points IsNot Nothing AndAlso Points.Count > 1 Then
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Using b As New SolidBrush(ForeColor)
Using p As New Pen(b, LineWidth)
e.Graphics.DrawPolygon(p, Points)
End Using
End Using
End If
End Sub
End Class
我还添加了颜色和宽度 属性。这只是为了给你一个大概的想法。注意 Points 属性.
上方的属性在 "action" 中看起来像这样:
添加更好的设计师支持当然是可能的,但我对此没有任何经验。但这是一个开始。
编辑
因为你总是绘制相同的形状,所以你可以在控件的构造函数中预先计算点,然后在 paint 事件中绘制形状:
Public Class StarControl
Inherits Control
'Storage for the shape's points
Private pts(9) As Point
'Constructor
Public Sub New()
Me.ClientSize = New Size(24, 24)
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
'Precalculate the shape
Dim cx = CInt(Me.ClientSize.Width / 2)
Dim cy = CInt(Me.ClientSize.Height / 2)
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
ReDim pts(9)
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
End Sub
Public Property LineColor As Color = Color.Black
Public Property FillColor As Color = Color.Gold
Public Property LineWidth As Integer = 1
Public Sub PaintMe(sender As Object, e As PaintEventArgs) Handles Me.Paint
'Draw the precalculated shape
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Using b As New SolidBrush(FillColor)
e.Graphics.FillPolygon(b, pts)
End Using
Using b As New SolidBrush(LineColor)
Using p As New Pen(b, LineWidth)
e.Graphics.DrawPolygon(p, pts)
End Using
End Using
End Sub
End Class
要获得更酷的效果,请将形状计算移动到 Paint 事件处理程序中,以便形状根据控件的大小自行调整大小。这允许您绘制任意大小的星星。