在PictureBox上绘制矩形

时间:2018-01-27 07:35:22

标签: vb.net drawing mouseevent picturebox rectangles

下一个代码允许您使用鼠标clics在表单中绘制矩形。 为什么不,或者如何绘制PictureBox?

Public Class Form1
Dim SelectRect As Rectangle = New Rectangle()
Dim ps As Point = New Point()
Dim pe As Point = New Point()

这会抓住矩形的第一次点击,起点或角落

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    SelectRect.Width = 0
    SelectRect.Height = 0
    SelectRect.X = e.X
    SelectRect.Y = e.Y
    ps.X = e.X
    ps.Y = e.Y
    pe = ps
End Sub

此部分确定矩形的宽度和高度:

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    If (e.Button = MouseButtons.Left) Then
        ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
        SelectRect.Width = e.X - SelectRect.X
        SelectRect.Height = e.Y - SelectRect.Y
        ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
    End If

End Sub

此部分确定最后一个坐标,即矩形的第二个角:

Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    Dim g As Graphics = Me.CreateGraphics()
    Dim p As Pen = New Pen(Color.Blue, 2)
    ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
    g.DrawRectangle(p, SelectRect)
    g.Dispose()
End Sub
   End Class

1 个答案:

答案 0 :(得分:1)

您的代码使用控件(在本例中为Form)鼠标事件,以便在Control.DrawReversibleFrame()提供的指导帮助下绘制矩形图形。
你只需要定义一个不同的,可绘制的控件的相同事件 - 比如PictureBox - 并且或多或少地重复相同的过程(在清理之后)。

  

正如许多所述,此处和之前使用的是Graphics对象   Paint事件提供,以便您的绘图将持续存在。
  您从Control.CreateGraphics()获得的Graphics对象不是   持久性,当你不想要它时可以擦除/剪裁。
  只有在您真正拥有计划执行的情况下才能使用它   你知道的原因。

<小时/> 我已经加载了一个事件处理程序,用于检查是否按下了Control Key 如果按下Control,则添加一个矩形,否则只绘制一个矩形 作为一个例子,我还包括一行填充矩形的代码。我认为这很有意思,因为你必须控制无效区域的大小 注释掉这些代码行以仅绘制框架:

SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)


enter image description here

Dim SelectRect As Rectangle = New Rectangle()
Dim _pen As Pen = New Pen(Color.Green, 4)
Dim _brush As SolidBrush = New SolidBrush(Color.Orange)
Dim _ControlPressed As Boolean = False

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    _ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    _ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub


Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    SelectRect.Location = e.Location
    SelectRect.Size = New Size(0, 0)
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If (e.Button = MouseButtons.Left) Then
        ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
        SelectRect.Width = e.X - SelectRect.X
        SelectRect.Height = e.Y - SelectRect.Y
        ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
    End If
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp

    If (e.Y < SelectRect.Y) Then
        SelectRect.Location = If(SelectRect.Location.X > e.X,
                                 New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
        SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
    Else
        If SelectRect.Location.X > SelectRect.Right Then
            SelectRect.Location = New Point(e.X, SelectRect.Y)
            SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
        End If
    End If

    If _ControlPressed Then
        Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
        _InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
        PictureBox1.Invalidate(_InflatedRect)
    Else
        PictureBox1.Invalidate()
    End If

End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    'Draw the outer rectangle with the color of _pen
    e.Graphics.DrawRectangle(_pen, SelectRect)

    'Fill the rectangle with the color of _brush
    'It's half Pen.Width smaller so it doesn't erase the contour
    SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
    e.Graphics.FillRectangle(_brush, SelectRect)

End Sub