如何在VB.NET中变换形状

时间:2019-05-10 07:54:35

标签: vb.net matrix graphics rotatetransform

我正在尝试先绘制一个形状(我已经做过),然后按照用户的选择进行变换,例如旋转到某个角度或缩放,以显示原始形状和新变换的形状形状。

在尝试旋转时,我尝试了以下方法:

Private Sub paint_box_Paint(sender As Object, e As PaintEventArgs) Handles paint_box.Paint
    Dim x As Integer = paint_box.Size.Width / 2
    Dim y As Integer = paint_box.Size.Height / 2

    Dim rect As New Rectangle(x, y, 80, 80)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)
    ' Create pen.
    Dim redPen As New Pen(Color.Red, 3)
    e.Graphics.DrawRectangle(blackPen, rect)
End Sub

我希望将打印在Form.Load上的形状旋转指定的角度30.0F

1 个答案:

答案 0 :(得分:2)

当我们想在控件的表面上绘画时,我们需要订阅控件的Paint event(并非所有控件都提供此事件:例如,TextBox控件不提供。我们可以派生一个Custom Control类从TextBox并覆盖OnPaint方法,尽管可能令人失望)。

要绘制和旋转形状,我们需要一些字段或属性来存储用于绘制的对象/值的引用。在这里,只有Rectangle对象提供了形状的度量,而single字段则存储了旋转角度。

Private drawingRect As Rectangle = New Rectangle(50, 50, 100, 100)
Private rotationAngle As Single = 0.0F

这些参考可以在需要时更改对象的形状和旋转角度。当这些值更改时,我们只需要调用控件的Invalidate()方法即可引发其Paint事件。

Form.Load()中(或在窗体的构造函数-Public Sub New()中),如果需要,我们可以定义形状的初始位置和大小:

Public Sub New()
    InitializeComponent()
    drawingRect = New Rectangle(50, 50, 100, 100)
End Sub

更改形状的尺寸以更新图形时,我们将调用控件的 Invalidate() 方法。在这里,绘图表面是由GroupBox控件提供的。
Button.Click()上,我们使用称为 GroupBox.Invalidate() 的新值更新形状。绘图将立即更新:

Private Sub btnRotate_Click(sender As Object, e As EventArgs) Handles btnRotate.Click
    ' Rotate the shape 45 degrees
    rotationAngle = 45
    GroupBox1.Invalidate()
End Sub

要旋转形状,我们可以使用两种简单的方法:Graphics.RotateTransform方法或Matrix.RotateAt()方法。
我正在使用后者:使用非常简单,它接受以度表示的角度,我们只需要提供旋转的坐标即可。
在这里,形状使用其中心点的坐标旋转:

Using m As Matrix = New Matrix
    m.RotateAt(rotationAngle, New PointF(drawingRect.X + (drawingRect.Width / 2.0F),
                                         drawingRect.Y + (drawingRect.Height / 2.0F)))
    e.Graphics.Transform = m
    e.Graphics.DrawRectangle(pen, drawingRect)
End Using

结果样本:

Graphis DrawRectangle Matrix RotateAt

用于绘制和旋转形状的所有代码:

Public Class Form1

    Private drawingRect As Rectangle = Rectangle.Empty
    Private rotationAngle As Single = 0.0F

    Public Sub New()
        InitializeComponent()
        drawingRect = New Rectangle(50, 50, 100, 100)
    End Sub

    Private Sub GroupBox1_Paint(sender As Object, e As PaintEventArgs) Handles GroupBox1.Paint
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        Using pen As Pen = New Pen(Color.LightGreen, 4)
            Using m As Matrix = New Matrix
                m.RotateAt(rotationAngle, New PointF(drawingRect.X + (drawingRect.Width / 2.0F),
                                                     drawingRect.Y + (drawingRect.Height / 2.0F)))
                e.Graphics.Transform = m
                e.Graphics.DrawRectangle(pen, drawingRect)
            End Using
        End Using
    End Sub

    Private Sub btnPaint_Click(sender As Object, e As EventArgs) Handles btnPaint.Click
        ' Redefine the shape and redraw it
        drawingRect.Location = New Point(100, 100)
        drawingRect.Size = New Size(200, 300)
        GroupBox1.Invalidate()
    End Sub

    Private Sub btnRotate_Click(sender As Object, e As EventArgs) Handles btnRotate.Click
        ' Rotate the shape 45 degrees
        rotationAngle = 45
        GroupBox1.Invalidate()
    End Sub
End Class