2013-04-14 49 views
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) 

lsidewside和​​代表长边,边宽和对角线的一面。

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

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

我试过多种方法,像triangle.Dispose triangle.Restore triangle.Clear和更多。他们都没有工作。

为什么我不把它们画在你可能会问的picturebox中。那么,当我在一个图画框中绘制一条线时,这个图画框排在前面,使得这条线不可见。我不知道如何解决这个问题。

+0

不能使用的createGraphics。您必须在Paint事件处理程序中绘制它们。将要绘制的三角形存储在列表中。将笔放在三角形中也是根本错误的,请从图书馆或书店挑选一本关于winforms编程的书。这只是非常困难,你不能通过试错学习来猜测它。 –

回答

0

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

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 
-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 
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 
相关问题