为什么我的矩形未在图片框中绘制? (拖动)

时间:2019-09-13 14:00:23

标签: vb.net picturebox rectangles drawrectangle

因此,我尝试通过以某种形式拖动鼠标来绘制矩形,但是我成功了,但是当我尝试在图片框中进行相同的操作时,没有创建矩形。

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

        If fGMouseIsDown And Not PictureBox1.Image Is Nothing Then

            rect.Width = e.X - rect.X
            rect.Height = e.Y - rect.Y
             Invalidate()


        End If
    End Sub

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown

        fGMouseIsDown = True
        rect.Location = e.Location
        rect.Width = 0
        rect.Height = 0
        Invalidate()
    End Sub 

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

        e.Graphics.DrawRectangle(Pens.Blue, rect)

End Sub

1 个答案:

答案 0 :(得分:2)

每个@HansPassant:Invalidate()方法中的PictureBox1_MouseDown()调用使表单无效,而您想使图片框无效。

该通话应改为:

PictureBox1.Invalidate()

此外,请确保以正确的方式拖动;仅当您从左上角转到右下角时,此功能才有效。