所以,我尝试通过在窗体中拖动鼠标来绘制矩形,并且成功了,但是当我尝试在图片框中以同样的方式进行操作时,不会创建矩形。
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
每@HansPassant:当您想要使图片框无效时,PictureBox1_MouseDown()
方法中的Invalidate()
调用会使表单无效。
该调用应改为:
PictureBox1.Invalidate()
此外,请确保以正确的方式拖动;这仅在从左上角到右下角时才有效。