如何删除所选的新rect图形&通过使用类来扩展

时间:2015-07-18 16:28:36

标签: vb.net graphics graphics2d cad

单击此按钮时,它直接放在picturebox中,并从C#转换为VB.NET,我的问题如何删除他的rect 我的意思是选择rectangluer&如果我从rect做了更多,我希望知道

' *删除所选的rect&删除所有rect

' *缩放选择矩形&缩放所有矩形

   Public rect As UserRect
   Private userRects As New List(Of UserRect)
   'add them as you would to this collection
   Private Sub ToolStripButton1_Click(sender As System.Object, e As 
   System.EventArgs) Handles ToolStripButton1.Click

    cb.DataSource = userRects
    rect = New UserRect(New System.Drawing.Rectangle(10, 10, 100, 100))

    userRects.Add(rect)


    rect.SetPictureBox(Me.PictureBox1)
    ' rect.SetPictureBox(Print.PictureBox1)
    PictureBox1.Refresh()
    End Sub



     Private Sub cb_SelectedIndexChanged() Handles cb.SelectedIndexChanged

    Dim ur As UserRect = DirectCast(cb.SelectedItem, UserRect)
    cnt = cnt - 1
    userRects.Remove(ur)
    'then refresh the picturebox - it is gone
    End Sub

我正在使用这个课程

Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class UserRect

Public Shared mPictureBox As PictureBox
Private rect As Rectangle
Public allowDeformingDuringMovement As Boolean = False
Private mIsClick As Boolean = False
Private mMove As Boolean = False
Private oldX As Integer
Private oldY As Integer
Private sizeNodeRect As Integer = 5
Private mBmp As Bitmap = Nothing
Private nodeSelected As PosSizableRect = PosSizableRect.None
' Private angle As Integer = 30
Dim tool As New ToolTip()
Private linex As Integer
Private liney As Integer
Private linex0 As Integer
Private liney0 As Integer
Dim check As Boolean = False
'''skipping your code for just the part I want to show you
Public Property Name As String
'tells the combobox what to display when holding a class object
Public Overrides Function ToString() As String
  Return Me.Name
End Function

Private Enum PosSizableRect
    UpMiddle
    LeftMiddle
    LeftBottom
    LeftUp
    RightUp
    RightMiddle
    RightBottom
    BottomMiddle
    None

End Enum

Public Sub New(r As Rectangle)
    rect = r
    mIsClick = False
End Sub

Public Sub Draw(g As Graphics)


    g.DrawRectangle(New Pen(Color.Red), rect)

    If check = True Then
        For Each pos As PosSizableRect In [Enum].GetValues(GetType(PosSizableRect))
            ' g.DrawRectangle(New Pen(Color.Blue), GetRect(pos))
            g.FillRectangle(Brushes.Blue, GetRect(pos))
        Next
        g.DrawRectangle(New Pen(Color.Green), rect)
    End If

End Sub

Public Sub SetBitmapFile(filename As String)
    Me.mBmp = New Bitmap(filename)
End Sub

Public Sub SetBitmap(bmp As Bitmap)
    Me.mBmp = bmp
End Sub

Public Sub SetPictureBox(p As PictureBox)
    Me.mPictureBox = p

    AddHandler mPictureBox.MouseDown, AddressOf mPictureBox_MouseDown
    AddHandler mPictureBox.MouseUp, AddressOf mPictureBox_MouseUp
    AddHandler mPictureBox.MouseMove, AddressOf mPictureBox_MouseMove
    AddHandler mPictureBox.Paint, AddressOf mPictureBox_Paint
    '   AddHandler mPictureBox.MouseWheel, AddressOf mPictureBox_MouseWheel
    '   mPictureBox.MouseDown += New MouseEventHandler(AddressOf mPictureBox_MouseDown)
    '  mPictureBox.MouseUp += New MouseEventHandler(AddressOf mPictureBox_MouseUp)
    '  mPictureBox.MouseMove += New MouseEventHandler(AddressOf mPictureBox_MouseMove)
    '  mPictureBox.Paint += New PaintEventHandler(AddressOf mPictureBox_Paint)
End Sub

Private Sub mPictureBox_Paint(sender As Object, e As PaintEventArgs)

    Try
        Draw(e.Graphics)
    Catch exp As Exception
        System.Console.WriteLine(exp.Message)
    End Try

End Sub

Private Sub mPictureBox_MouseDown(sender As Object, e As MouseEventArgs)
    mIsClick = True

    nodeSelected = PosSizableRect.None
    nodeSelected = GetNodeSelectable(e.Location)

    If rect.Contains(New Point(e.X, e.Y)) Then
        mMove = True
        check = True
    Else
        check = False
    End If
    oldX = e.X
    oldY = e.Y
End Sub
Private Sub mPictureBox_MouseWheel(sender As Object, e As MouseEventArgs)

End Sub
Private Sub mPictureBox_MouseUp(sender As Object, e As MouseEventArgs)
    Print.PictureBox1.Refresh()
    mIsClick = False
    mMove = False

    Print.gr.DrawRectangle(New Pen(Color.Red), rect)

End Sub
Private choice As String
Private zoomfactor As Single = 1
Private Sub mPictureBox_MouseMove(sender As Object, e As MouseEventArgs)
    ChangeCursor(e.Location)
    If mIsClick = False Then
        Return
    End If

    Dim backupRect As Rectangle = rect

    Select Case nodeSelected
        Case PosSizableRect.LeftUp
            rect.X += e.X - oldX
            rect.Width -= e.X - oldX
            rect.Y += e.Y - oldY
            rect.Height -= e.Y - oldY
            Exit Select
        Case PosSizableRect.LeftMiddle
            rect.X += e.X - oldX
            rect.Width -= e.X - oldX
            Exit Select
        Case PosSizableRect.LeftBottom
            rect.Width -= e.X - oldX
            rect.X += e.X - oldX
            rect.Height += e.Y - oldY
            Exit Select
        Case PosSizableRect.BottomMiddle
            rect.Height += e.Y - oldY
            Exit Select
        Case PosSizableRect.RightUp
            rect.Width += e.X - oldX
            rect.Y += e.Y - oldY
            rect.Height -= e.Y - oldY
            Exit Select
        Case PosSizableRect.RightBottom
            rect.Width += e.X - oldX
            rect.Height += e.Y - oldY
            Exit Select
        Case PosSizableRect.RightMiddle
            rect.Width += e.X - oldX
            Exit Select

        Case PosSizableRect.UpMiddle
            rect.Y += e.Y - oldY
            rect.Height -= e.Y - oldY
            Exit Select
        Case Else

            If mMove = True Then
                rect.X = rect.X + e.X - oldX
                rect.Y = rect.Y + e.Y - oldY


                tool.ShowAlways = True
                tool.ToolTipIcon = ToolTipIcon.Info
                tool.ToolTipTitle = "Rect"
                tool.UseAnimation = False

                tool.SetToolTip(mPictureBox, "x =" & rect.X & vbCrLf & "y =" & rect.Y & vbCrLf & "width =" & rect.Width & vbCrLf & "height =" & rect.Height)
            ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
                rect.X = rect.X + e.X - oldX
                rect.Y = rect.Y + e.Y - oldY
            End If
            Exit Select
    End Select
    oldX = e.X
    oldY = e.Y

    If rect.Width < 5 OrElse rect.Height < 5 Then
        rect = backupRect
    End If

    TestIfRectInsideArea()
    '* zoomfactor
    mPictureBox.Invalidate()
End Sub

Private Sub TestIfRectInsideArea()
    ' Test if rectangle still inside the area.
    If rect.X < 0 Then
        rect.X = 0
    End If
    If rect.Y < 0 Then
        rect.Y = 0
    End If
    If rect.Width <= 0 Then
        rect.Width = 1
    End If
    If rect.Height <= 0 Then
        rect.Height = 1
    End If

    If rect.X + rect.Width > mPictureBox.Width Then
        rect.Width = mPictureBox.Width - rect.X - 1
        ' -1 to be still show 
        If allowDeformingDuringMovement = False Then
            mIsClick = False
        End If
    End If
    If rect.Y + rect.Height > mPictureBox.Height Then
        rect.Height = mPictureBox.Height - rect.Y - 1
        ' -1 to be still show 
        If allowDeformingDuringMovement = False Then
            mIsClick = False
        End If
    End If
End Sub

Private Function CreateRectSizableNode(x As Integer, y As Integer) As Rectangle
    Return New Rectangle(x - sizeNodeRect / 2, y - sizeNodeRect / 2, sizeNodeRect, sizeNodeRect)
End Function

Private Function GetRect(p As PosSizableRect) As Rectangle
    Select Case p
        Case PosSizableRect.LeftUp
            Return CreateRectSizableNode(rect.X, rect.Y)

        Case PosSizableRect.LeftMiddle
            Return CreateRectSizableNode(rect.X, rect.Y + +rect.Height / 2)

        Case PosSizableRect.LeftBottom
            Return CreateRectSizableNode(rect.X, rect.Y + rect.Height)

        Case PosSizableRect.BottomMiddle
            Return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y + rect.Height)

        Case PosSizableRect.RightUp
            Return CreateRectSizableNode(rect.X + rect.Width, rect.Y)

        Case PosSizableRect.RightBottom
            Return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height)

        Case PosSizableRect.RightMiddle
            Return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height / 2)

        Case PosSizableRect.UpMiddle
            Return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y)
        Case Else
            Return New Rectangle()
    End Select
End Function

Private Function GetNodeSelectable(p As Point) As PosSizableRect
    For Each r As PosSizableRect In [Enum].GetValues(GetType(PosSizableRect))
        If GetRect(r).Contains(p) Then
            Return r
        End If
    Next
    Return PosSizableRect.None
End Function






Private Sub ChangeCursor(p As Point)
    '    mPictureBox.Cursor = GetCursor(GetNodeSelectable(p))
End Sub

''' <summary>
''' Get cursor for the handle
''' </summary>
''' <param name="p"></param>
''' <returns></returns>
Private Function GetCursor(p As PosSizableRect) As Cursor
    Select Case p
        Case PosSizableRect.LeftUp
            Return Cursors.SizeNWSE

        Case PosSizableRect.LeftMiddle
            Return Cursors.SizeWE

        Case PosSizableRect.LeftBottom
            Return Cursors.SizeNESW

        Case PosSizableRect.BottomMiddle
            Return Cursors.SizeNS

        Case PosSizableRect.RightUp
            Return Cursors.SizeNESW

        Case PosSizableRect.RightBottom
            Return Cursors.SizeNWSE

        Case PosSizableRect.RightMiddle
            Return Cursors.SizeWE

        Case PosSizableRect.UpMiddle
            Return Cursors.SizeNS
        Case Else
            Return Cursors.[Default]
    End Select
End Function

End Class

1 个答案:

答案 0 :(得分:1)

您可以将每个UserRect存储在List(Of UserRect)中,并将该列表作为ComboBox或其他控件的dataSource提供。覆盖类的ToString函数,告诉控件要显示的内容。然后使用SelectedIndexChanged事件删除UserRect

Private userRects As New List(Of UserRect)
'add them as you would to this collection

'in some method after adding the objects to the list
'assign the list as the datasource
cb.DataSource = userRects

Private Sub cb_SelectedIndexChanged() Handles cb.SelectedIndexChanged
  Dim ur As UserRect = DirectCast(cb.SelectedItem, UserRect)
  userRects.Remove(ur)
  'then refresh the picturebox - it is gone
End Sub

UserRect类重写ToString函数:

Public class
 '''skipping your code for just the part I want to show you
  Public Property Name As String
  'tells the combobox what to display when holding a class object
  Public Overrides Function ToString() As String
    Return Me.Name
  End Function
End Class