【发布时间】:2020-01-19 17:48:50
【问题描述】:
我想绘制第一个旋转的图形和第二个未旋转的图形。当我运行下面的代码时,它不显示第一个图,但第二个图被旋转。重置不起作用。我需要保持数字的顺序不变。
Private Sub Form17_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim x, y As Integer
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
'Figure 1
Dim Mx As Matrix = New Matrix()
x = 59 : y = 25
Mx.RotateAt(90, New Point(x, y), MatrixOrder.Append)
e.Graphics.Transform = Mx
e.Graphics.DrawEllipse(New Pen(Color.Green, 1), New Rectangle(x + 3, y - 5, 10, 10))
e.Graphics.DrawLine(New Pen(Color.Green, 1), x, y - 2, x + 13, y - 2)
e.Graphics.DrawLine(New Pen(Color.Green, 1), x, y + 2, x + 13, y + 2)
e.Graphics.DrawLine(New Pen(Color.Green, 1), x + 8, y - 5, x + 8, y + 5)
Mx.Reset()
'Figura 2
e.Graphics.DrawEllipse(New Pen(Color.Blue, 1), New Rectangle(x + 3, y - 5, 10, 10))
e.Graphics.DrawLine(New Pen(Color.Blue, 1), x, y - 2, x + 13, y - 2)
e.Graphics.DrawLine(New Pen(Color.Blue, 1), x, y + 2, x + 13, y + 2)
e.Graphics.DrawLine(New Pen(Color.Blue, 1), x + 8, y - 5, x + 8, y + 5)
End Sub
怎么了?
【问题讨论】:
-
不知道第一个缺席的人物(可能是因为旋转而不在屏幕上?)。对于第二个,旋转是正常的:Graphics.Transform 采用copy of the matrix。因此,如果您重置 Mx,旧副本仍在 Graphics 中。重置后你需要另一个
e.Graphics.Transform = Mx。 -
e.Graphics.ResetTransform()。如果您使用固定笔(相同颜色,大小为 1),请使用库存对象(Pens.Green、Pens.Blue)。否则,您必须显式(调用 Dispose())或隐式(使用Using语句声明它们)处置这些 Pen(或在 Paint 事件之外创建它们并在 Form 关闭时处置它们。)。跨度>