为什么我的矩形没有绘制在图片框中? (拖动)
Why my rectangle is not drawing in a picturebox? (Dragging)
因此,我尝试通过在表单中拖动鼠标来绘制矩形,并且成功了,但是当我尝试在图片框中以相同的方式绘制时,没有创建矩形。
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
Per @HansPassant:当您想要使图片框无效时,PictureBox1_MouseDown()
方法中的 Invalidate()
调用会使表单无效。
那个调用应该是:
PictureBox1.Invalidate()
此外,请确保以正确的方式拖动;这仅在您从左上角转到右下角时有效。
因此,我尝试通过在表单中拖动鼠标来绘制矩形,并且成功了,但是当我尝试在图片框中以相同的方式绘制时,没有创建矩形。
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
Per @HansPassant:当您想要使图片框无效时,PictureBox1_MouseDown()
方法中的 Invalidate()
调用会使表单无效。
那个调用应该是:
PictureBox1.Invalidate()
此外,请确保以正确的方式拖动;这仅在您从左上角转到右下角时有效。