1

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

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,wside分别dside代表长边、宽边和对角边。

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

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

我尝试了多种方法,例如 triangle.Dispose triangle.Restore triangle.Clear 等等。它们都不起作用。

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

4

3 回答 3

0

尝试使用 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
于 2013-04-14T21:15:16.517 回答
0
‘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
于 2016-07-18T09:52:06.020 回答
-1
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
于 2016-07-16T09:55:49.910 回答