vb.net 如何删除在表单上绘制的线条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16004299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:15:17  来源:igfitidea点击:

How to remove a line which is drawn on a form

vb.netvisual-studio-2008

提问by CriticalRocket

I'm trying to draw a triangle like this:

我正在尝试绘制这样的三角形:

Dim triangle As Graphics
    Dim pen1 As New Pen(Color.LimeGreen, 2)
    Dim lside As Integer
    Dim wside As Integer
    Dim dside As Integer

triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)

lside, wsideand dsidestand for length side, width side and diagonal side.

lsidewsidedside代表长度侧,宽度侧和对角线侧。

I've got 4 textboxes, for the length, width, diagonal side and one for the angle. The purpose is to fill in 2 of the values, and then a rectangular triangle gets drawn following Pythagoras' theorem. I want to draw a line for Angle as well later on. But I first want to get this to work.

我有 4 个文本框,用于长度、宽度、对角边和一个用于角度。目的是填充其中的 2 个值,然后根据毕达哥拉斯定理绘制一个矩形三角形。稍后我也想为 Angle 画一条线。但我首先想让这个工作。

But every time I click the button to draw a new triangle, the previous one should get deleted. And that's the problem.

但是每次我点击按钮绘制一个新的三角形时,前一个应该被删除。这就是问题所在。

I've tried multiple methods, like triangle.Dispose triangle.Restore triangle.Clear and more. None of them work.

我已经尝试了多种方法,比如 triangle.Dispose triangle.Restore triangle.Clear 等等。他们都没有工作。

Why am I not drawing them in a picturebox you might ask. Well, when I drew a line in a picturebox, the picturebox sort of went in front of the line, making the line invisible. And I didn't know how to fix that.

为什么我不在你可能会问的图片框中绘制它们。好吧,当我在图片框中画一条线时,图片框有点走在线条的前面,使线条不可见。我不知道如何解决这个问题。

回答by Sam

Try using Me.Invalidate(), it basically clears, then draws the shape in the area you're painting on. Reference.

尝试使用 Me.Invalidate(),它基本上会清除,然后在您正在绘制的区域中绘制形状。参考

Private Sub ClearCanvas_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Me.Invalidate()
End Sub

Priavte DrawTriangle_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim triangle As Graphics
       Dim pen1 As New Pen(Color.LimeGreen, 2)
       Dim lside As Integer
       Dim wside As Integer
       Dim dside As Integer
       triangle = Me.CreateGraphics()
       triangle.DrawLine(pen1, wside, 420, 640, 420)
       triangle.DrawLine(pen1, 640, lside, 640, 420)
       triangle.DrawLine(pen1, dside, 420, 640, lside)
End Sub

回答by user6597605

‘Draw select delete multiple lines on Picturebox

Imports System
Imports System.Drawing

Imports System.Drawing.Drawing2D





Public Class Form1

    Dim drawrec, undo_delete As Boolean

    Dim index_arrary_line_tobe_deleted(10000) As Integer
    Dim ptA, ptB As Point                     ' starting and ending point
    Dim down As Boolean
    Dim k, Last_index_line_tobe_selected As Integer
    Private temp As line
    Dim List_of_line_tobe_deleted As New List(Of line)
    Dim List_of_line_to_Undo As New List(Of line)

    Private m_Lines As New List(Of line)
    Private m_Pt As Point
    Private m_Pt2 As Point
    Private m_tracking As Boolean
    Private Sub B2_index_arrary_line_tobe_deletedete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2_Delete.Click

        Try

            m_Lines.RemoveAll(AddressOf List_of_line_tobe_deleted.Contains)

        Catch ex As Exception

        End Try

        PictureBox1.Refresh()
    End Sub
    Private Sub B3_Undodelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3_Undodelete.Click

        undo_delete = True

        Try
            m_Lines.AddRange(List_of_line_tobe_deleted)
        Catch ex As Exception

        End Try
        PictureBox1.Refresh()

    End Sub
    Private Sub B1_Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1_Select.Click

        drawrec = True

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint

        Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))

        If m_tracking = True And drawrec = True Then

            k = -1
            For i As Integer = 0 To m_Lines.Count - 1
                If m_Lines(i).ContainsCompletely(r) = True Then
                    k = k + 1

                    index_arrary_line_tobe_deleted(i) = k
                    Debug.Print("Index of NOT selected lines  " + i.ToString + "Index of selected lines  " + Last_index_line_tobe_selected.ToString)  'to compare idex of two lists !!!!
                    index_arrary_line_tobe_deleted(k) = i
                    List_of_line_tobe_deleted.Add(m_Lines(i))
                End If

            Next
            Last_index_line_tobe_selected = k 'so far no use, just to know
            e.Graphics.DrawRectangle(Pens.Cyan, r)
        End If


        If undo_delete = False Then
            For i As Integer = 0 To m_Lines.Count - 1
                Me.m_Lines(i).Draw(e.Graphics, r)
                Debug.Print("Index of remaining lines  " + i.ToString)
            Next
        End If
        If undo_delete = True Then
            For i As Integer = 0 To m_Lines.Count - 1
                Me.m_Lines(i).Re_Draw(e.Graphics)
                Debug.Print("Index of remaining lines  " + i.ToString)
            Next
        End If


    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        drawrec = False
        down = False
        undo_delete = False
        For i As Integer = 0 To index_arrary_line_tobe_deleted.Length - 1
            index_arrary_line_tobe_deleted(0) = -1
        Next i
        k = -1
        Last_index_line_tobe_selected = -1
    End Sub




    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        down = True

        If down = True And drawrec = False Then

            ptA = e.Location
            temp = New line
            temp.StartPoint = e.Location

        End If


        If e.Button = MouseButtons.Left Then
            ResetSelected(Me.m_Lines)
            m_Pt = e.Location
        End If
    End Sub

    Private Sub ResetSelected(ByVal m_Lines As List(Of line))
        For i As Integer = 0 To m_Lines.Count - 1
            m_Lines(i).Selected = False
        Next
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
        If down = True Then

        End If

        If e.Button = MouseButtons.Left Then

            If down = True And drawrec = False Then
                ptB = e.Location
                temp.EndPoint = e.Location
            End If

            m_Pt2 = e.Location
            m_tracking = True
            Me.PictureBox1.Invalidate()
        End If
    End Sub

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

        If drawrec = False Then

            temp.EndPoint = e.Location

            m_Lines.Add(temp)
            temp = Nothing
            Me.PictureBox1.Invalidate()

        End If

        m_tracking = False
        Me.PictureBox1.Invalidate()
    End Sub



End Class

Public Class line


    Public StartPoint As Point
    Public EndPoint As Point



    Private m_selected As Boolean
    Public Property Selected() As Boolean
        Get
            Return m_selected
        End Get
        Set(ByVal value As Boolean)
            m_selected = value
        End Set
    End Property



    Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
        Dim myPen1 As New Pen(Color.Red, 1)
        g.SmoothingMode = SmoothingMode.AntiAlias
        If Me.ContainsCompletely(r) OrElse Me.Selected Then
            Me.Selected = True
            g.DrawLine(myPen1, Me.StartPoint, Me.EndPoint)
        Else
            Dim myPen2 As New Pen(Color.Blue, 1)
            g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
        End If

    End Sub
    Public Sub Re_Draw(ByVal g As Graphics)
        g.SmoothingMode = SmoothingMode.AntiAlias
        Dim myPen2 As New Pen(Color.Blue, 1)
        g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
    End Sub


    Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean

        If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
            Return True
        End If

        Return False
    End Function
End Class

回答by user6597256

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1

Dim drawrec As Boolean

Dim del As Integer
Dim ptA, ptB As Point                     ' starting and ending point
Dim down As Boolean
Private temp As line

Private m_Lines As New List(Of line)
Private m_rnd As New Random
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Delete.Click
    Try
        m_Lines.RemoveAt(del)

    Catch ex As Exception

    End Try
    PictureBox1.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    drawrec = False
    down = False
    del = -1

End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
    down = True

    If down = True And drawrec = False Then

        ptA = e.Location
        temp = New line
        temp.StartPoint = e.Location

    End If


    If e.Button = MouseButtons.Left Then
        ResetSelected(Me.m_Lines)
        m_Pt = e.Location
    End If
End Sub

Private Sub ResetSelected(ByVal m_Lines As List(Of line))
    For i As Integer = 0 To m_Lines.Count - 1
        m_Lines(i).Selected = False
    Next
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    If down = True Then

    End If

    If e.Button = MouseButtons.Left Then

        If down = True And drawrec = False Then
            ptB = e.Location
            temp.EndPoint = e.Location
        End If

        m_Pt2 = e.Location
        m_tracking = True
        Me.PictureBox1.Invalidate()
    End If
End Sub

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

    If drawrec = False Then

        temp.EndPoint = e.Location

        m_Lines.Add(temp)
        temp = Nothing
        Me.PictureBox1.Invalidate()


    End If

    m_tracking = False
    Me.PictureBox1.Invalidate()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint

    Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))

    If m_tracking And drawrec = True Then
        For i As Integer = 0 To m_Lines.Count - 1
            If m_Lines(i).ContainsCompletely(r) = True Then
                Debug.Print("KKKKKKKKKKKKKKK  " + i.ToString)
                del = i
            End If

        Next
        e.Graphics.DrawRectangle(Pens.Cyan, r)
    End If


    For i As Integer = 0 To m_Lines.Count - 1

        Me.m_Lines(i).Draw(e.Graphics, r)


    Next


End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Select.Click
    drawrec = True
End Sub


End Class

Public Class line

Public StartPoint As Point
Public EndPoint As Point
Public Filled As Boolean
Public ShapeColor As Color
Public PenWidth As Integer



Private m_selected As Boolean
Public Property Selected() As Boolean
    Get
        Return m_selected
    End Get
    Set(ByVal value As Boolean)
        m_selected = value
    End Set
End Property



Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
    g.SmoothingMode = SmoothingMode.AntiAlias
    If Me.ContainsCompletely(r) OrElse Me.Selected Then
        Me.Selected = True
        g.DrawLine(Pens.Red, Me.StartPoint, Me.EndPoint)
    Else

        g.DrawLine(Pens.Blue, Me.StartPoint, Me.EndPoint)
    End If

End Sub

Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean

    If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
        Return True
    End If

    Return False
End Function
End Class