| |
VB.NET - Ein- und UmsteigerMausbewegung (Linien) auf PictureBox zeichnen | | | Autor: keco | Datum: 19.04.09 14:15 |
| Hallo,
ihr kennt das bestimmt alle: In Grafikprogrammen wählt man einen Stift aus und malt damit eine verbundene Linie, entlang der Mausposition. Das wollte ich eben für eine PictureBox machen als kleine Zusatzfunktion, habe dabei allerdings ein Problem. Bisher kann der Benutzer mit gedrückter Maus über die PictureBox fahren, wobei auch die Linie gezeichnet wird, allerdings erst, wenn er die Maus wieder los lässt. Allerdings sieht er dabei nicht die Linie sofort.
Hier mal mein bisheriger Code:
Public Class Form1
Dim pPoints As New List(Of Point)
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
pPoints.Clear()
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
pPoints.Add(e.Location)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Dim g As Graphics = PictureBox1.CreateGraphics
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.DrawLines(Pens.Black, pPoints.ToArray)
g.Dispose()
End Sub
End Class Jetzt hätte ich aber gerne noch, dass der Benutzer den aktuellen Zustand der Linie sehen kann. Ich hatte das anfangs im MouseMove-Event gemacht, indem ich an aktueller Stelle ein Rechteck zeichnen ließ, was bei schnellen Bewegungen aber keine Linie mehr ergibt. Ich hätte es auch gerne so, dass die Linie anschließend gerendert wird, also mit AntiAliasing zum Beispiel. Kennt jemand eine Möglichkeit? | |
Re: Mausbewegung (Linien) auf PictureBox zeichnen | | | Autor: Helmuth | Datum: 20.04.09 12:33 |
| Hallo keco!
Ich hab das mal so gelöst!
Hat recht gut funktioniert!
Public Class Form1
Dim g As Graphics
Dim bDraw As Boolean
Private Structure myPaintStruct
Dim ptStart As Point
Dim ptEnd As Point
End Structure
Dim myPaintList As New List(Of myPaintStruct)
Dim colmyPaint As myPaintStruct
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
colmyPaint.ptStart = e.Location
myPaintList.Add(colmyPaint)
bDraw = True
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If bDraw = True Then
colmyPaint = myPaintList.Item(myPaintList.Count - 1)
colmyPaint.ptEnd = e.Location
myPaintList.Item(myPaintList.Count - 1) = colmyPaint
g = Me.CreateGraphics
g.Clear(Color.LightBlue)
For i As Integer = 0 To myPaintList.Count - 1
g.DrawLine(Pens.Black, myPaintList.Item(i).ptStart.X, _
myPaintList.Item(i).ptStart.Y, myPaintList.Item(i).ptEnd.X, _
myPaintList.Item(i).ptEnd.Y)
Next
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
bDraw = False
End Sub
End Class Kurze Beschreibung:
Ich erstelle ein Struktur aus Anfang's und Endpunkt.
Bei Mousdown add ich den Startpunkt und erlaube im das Zeichnen! (bDraw = True sonst zeichnet er bei jeder Mausbewegung)
Bei MousMove werden die alten Linien gelöscht und danach wieder neu gezeichnet!
MousUp wieder bDraw = False also ab jetzt auf das MouseMove Event nicht mehr zeichnen!
Hoff es hilft!
Gruß Helmuth | |
Re: Mausbewegung (Linien) auf PictureBox zeichnen | | | Autor: keco | Datum: 20.04.09 17:11 |
| Danke für den Programmcode, leider nicht ganz das, was ich wollte. Das ganze kann ich aber auch gut gebrauchen.
Ich wollte eigentlich, dass eine Linie entlang der Mausbewegung gezeichnet wird, also nicht nur eine gerade Linie. Ich habe das so in etwa gemeint:
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
pDraw = True
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If pDraw Then
Using g As Graphics = PictureBox1.CreateGraphics
g.DrawRectangle(Pens.Black, New Rectangle(e.Location.X, _
e.Location.Y, 1, 1))
End Using
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
pDraw = False
End Sub Aber mit diesem Code sieht das ganze wirklich sehr daneben aus, vorallem bei schnelleren Mausbewegungen. Das ganze soll eben durchgehend sein (siehe MSPaint -> Pinsel-Werkzeug). Zumal das ganze mit einem Rechteck zu zeichnen zu breit ist, wäre schön, wenn man die Breite in Pixel angeben könnte, beginnend bei 1 Pixel. | |
Re: Mausbewegung (Linien) auf PictureBox zeichnen | | | Autor: Snof | Datum: 20.04.09 18:03 |
| probiers mal so: Private lastPoint As Point
Private pdraw As Boolean
Private cl As Color = color.Red
Private w As Integer = 4
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
pdraw = True
lastPoint = e.Location
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If pdraw AndAlso e.Location <> lastPoint Then
Using g As Graphics = PictureBox1.CreateGraphics
g.FillEllipse(New SolidBrush(cl), lastPoint.X - w \ 2, _
lastPoint.Y - w \ 2, w, w)
g.DrawLine(New Pen(cl, w), lastPoint, e.Location)
lastPoint = e.Location
End Using
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
pdraw = False
End Sub | |
Re: Mausbewegung (Linien) auf PictureBox zeichnen | | | Autor: keco | Datum: 20.04.09 18:19 |
| Das funktioniert prima, dankesehr. Ich habe das ganze noch in Kombination mit meinem ersten Beitrag geschrieben:
Private pDrawingPoints As New List(Of Point)
Private pLastPoint As Point
Private pAllowDraw As Boolean
Private pLineColor As Color = Color.Red
Private pLineWidth As Integer = 2
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
pAllowDraw = True
pLastPoint = e.Location
pDrawingPoints.Clear()
pDrawingPoints.Add(e.Location)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If pAllowDraw AndAlso e.Location <> pLastPoint Then
Using g As Graphics = PictureBox1.CreateGraphics
g.FillEllipse(New SolidBrush(pLineColor), pLastPoint.X - pLineWidth _
\ 2, pLastPoint.Y - pLineWidth \ 2, pLineWidth, pLineWidth)
g.DrawLine(New Pen(pLineColor, pLineWidth), pLastPoint, e.Location)
pLastPoint = e.Location
pDrawingPoints.Add(e.Location)
End Using
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
pAllowDraw = False
Using g As Graphics = PictureBox1.CreateGraphics
g.Clear(Color.White)
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.DrawLines(New Pen(pLineColor, pLineWidth), pDrawingPoints.ToArray)
End Using
End Sub Damit wäre das Problem gelöst und muss nur noch in das UserControl mit den anderen Sachen. Danke an beide! | |
| Sie sind nicht angemeldet! Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.
Einloggen | Neu registrieren |
|
|
Neu! sevCommand 4.0
Professionelle Schaltflächen im modernen Design!
Mit nur wenigen Mausklicks statten auch Sie Ihre Anwendungen ab sofort mit grafischen Schaltflächen im modernen Look & Feel aus (WinXP, Office, Vista oder auch Windows 8), inkl. große Symbolbibliothek. Weitere InfosTipp des Monats Access-Tools Vol.1
Über 400 MByte Inhalt
Mehr als 250 Access-Beispiele, 25 Add-Ins und ActiveX-Komponenten, 16 VB-Projekt inkl. Source, mehr als 320 Tipps & Tricks für Access und VB
Nur 24,95 EURWeitere Infos
|