在PictureBox上绘制矩形 [英] Draw Rectangle over PictureBox

查看:174
本文介绍了在PictureBox上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下一个代码使您可以使用鼠标单击在窗体中绘制矩形. 为什么不呢,或者如何在PictureBox上绘制呢?

The next code lets you draw Rectangles in the Form with mouse clics. Why not, or how can be draw over a PictureBox?

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

这会捕获矩形的第一个点击,起点或角点

This catch the first click, starting point or corner of the rectangle

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

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

This part determine the width and height of the rectangle:

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

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

This part determine the last coordinate, the second corner of the rectangle:

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

推荐答案

您的代码在Control.DrawReversibleFrame()提供的准则的帮助下,使用控件(在这种情况下为Form)鼠标事件来启用矩形形状的绘制. .
您只需要定义一个不同的可绘制控件(例如PictureBox)的相同事件,并(在清理之后)或多或少地重复相同的过程即可.

Your code uses a control (a Form in this case) mouse events to enable the drawing of rectangular shapes, with the help of guidelines, provided by Control.DrawReversibleFrame().
You just have to define the same events of a different, drawable, control - like a PictureBox - and repeat, more or less, the same procedure (after a cleanup).

许多所述,在此处和之前,请使用Graphics对象 Paint事件,请提供,以便您的绘图得以保留.
您从Control.CreateGraphics()获得的Graphics对象不是 持久性,不需要时可以擦除/剪切.
仅当确实是您计划要做的 您知道的原因.

As many have stated, here and before, use the Graphics object that the Paint event kindly offers, so that your drawing will persist.
The Graphics object you get from Control.CreateGraphics() is not persistent, and it can be erase/clipped when you don't want to.
Use it only if that is really what you have planned to do for the reasons you know.


我已经添加了一个事件处理程序,用于检查是否按下了Control Key.
如果按下Control,则添加一个矩形,否则,将仅绘制一个矩形.
作为示例,我还包含了填充矩形的一行代码.我认为这很有趣,因为您必须控制无效Region的大小.
注释掉以下代码行以仅绘制框架:


I've adden an event handler that checks if Control Key is pressed.
If Control is pressed, you add a rectangle, if not, only one rectangle is drawn.
I've also included, as an example, a line of code that fills the rectangle. I think it's interesting, because you have to control the size of the invalidated Region.
Comment out these lines of code to draw just the frame:

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


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

这篇关于在PictureBox上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆