Narysuj kilka linii na obrazie ruchu myszy

0

Pytanie

Chcę narysować kilka linii na obrazie Za pomocą myszy, przesuń w vb.net. Mój problem polega na tym, że gdy zaczynam malować następną linię, poprzednia linia znika! Czy ktoś może mi pomóc? Umieściłem swój kod wraz ze zdjęciem uruchomienia mojego projektu

Dim st, en As New Point
Dim p As Pen
Private mouseButtonPressed As Boolean = False

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    p = New Pen(Color.Black, 2)
    Dim g As Graphics = e.Graphics
    g.DrawLine(p, st, en)
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    mouseButtonPressed = False
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If mouseButtonPressed Then
        PictureBox1.Invalidate()
        en = e.Location
    End If
End Sub

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    If e.Button = MouseButtons.Left Then
        st = New Point(e.X, e.Y)
        mouseButtonPressed = True
    End If
End Sub

GIF of my run project

drawing vb.net
2021-11-23 06:07:57
1

Najlepsza odpowiedź

0

Możesz zapisać wiersza na liście, w miarę ich ukończenia, a następnie narysować wszystkie poprzednie wiersze, malować na żywo

Private st, en As New Point
Private mouseButtonPressed As Boolean = False

Private lines As New List(Of (st As Point, en As Point))

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim p = New Pen(Color.Black, 2)
    Dim g As Graphics = e.Graphics
    g.DrawLine(p, st, en)
    For Each line In lines
        g.DrawLine(p, line.st, line.en)
    Next
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    mouseButtonPressed = False
    lines.Add((st, en))
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If mouseButtonPressed Then
        PictureBox1.Invalidate()
        en = e.Location
    End If
End Sub

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    If e.Button = MouseButtons.Left Then
        st = New Point(e.X, e.Y)
        mouseButtonPressed = True
    End If
End Sub

Lista ta zawiera Tuple(of Point, Point)

2021-11-23 15:48:51

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................