|
Tipp 0100
|
Figuren zeichnen und rotieren (GDI+)
|
|
|
Autor/Einsender: Datum: |
|
Michael Werner 29.12.2005 |
|
Entwicklungsumgebung: |
|
VB.Net 2003 |
Framework: |
|
1.1 |
|
|
Mit den Methoden der Klasse GraphicsPath im Namespace System.Drawing.Drawing2D lassen sich beliebige
Figuren zeichnen und um einen Achspunkt drehen. Mit den Mausereignissen MouseDown, MouseMove und MouseUp wird per
Mausbewegung die Figur gezeichnet. Über eine Funktion wird das Zentrum der gezeichneten Figur ermittelt und mit der Rotate-Methode
eines Matrix-Objektes, das dem GraphicsPath-Objekt über die Methode Transform zugewiesen wird, die Rotation ausgeführt.
|
|
|
Imports System.Drawing.Drawing2D
Dim path As New GraphicsPath
Dim prevPt As New Point(100, 100)
#Region " Freihandzeichnen "
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawPath(Pens.Black, path)
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
prevPt.X = e.X
prevPt.Y = e.Y
path.StartFigure()
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = MouseButtons.Left Then
Dim g As Graphics = Me.CreateGraphics
Dim currPt As Point = New Point(e.X, e.Y)
g.DrawLine(Pens.Black, prevPt, currPt)
g.Dispose()
path.AddLine(prevPt, currPt)
prevPt = currPt
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
If CheckBox1.Checked Then
If e.Button = MouseButtons.Left Then
path.CloseFigure()
Invalidate()
End If
End If
End Sub
#End Region
#Region " Rotation "
Private Function GetCentreOfPath(ByVal p As GraphicsPath) _
As PointF
Dim x As Single = 0.0F
Dim y As Single = 0.0F
For Each pt As PointF In path.PathPoints
x += pt.X
y += pt.Y
Next
Return New PointF(x / path.PointCount, y / path.PointCount)
End Function
Private Sub Rotate()
Dim m As Matrix = New Matrix
Dim centrPt As PointF = GetCentreOfPath(path)
m.Translate(-centrPt.X, -centrPt.Y)
m.Rotate(10.0F, MatrixOrder.Append)
m.Translate(centrPt.X, centrPt.Y, MatrixOrder.Append)
path.Transform(m)
Invalidate()
End Sub
#End Region
|
|
|
|
|
Windows-Version |
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
|
Download (6,6
kB)
|
Downloads bisher: [ 1294 ]
|
|
|