Graphics applications sometimes require users to select a rectangular region of a picture or drawing visually. You need to provide a resizing box manipulated by the pointer at run time that only interacts temporarily with the graphics displayed already:
Private Type TRect
Left As Single
Top As Single
Right As Single
Bottom As Single
End Type
Private mbDragging As Boolean
Private mRect As TRect
Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
mbDragging = True
Picture1.DrawMode = vbInvert
With mRect
.Left = X
.Top = Y
.Right = X
.Bottom = Y
End With
End Sub
Private Sub Picture1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Not mbDragging Then Exit Sub
With mRect
' erase box
Picture1.Line (.Left, .Top)-(.Right, _
.Bottom), , B
.Right = X
.Bottom = Y
' draw box
Picture1.Line (.Left, .Top)-(X, Y), , B
End With
End Sub
Private Sub Picture1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
mbDragging = False
' erase box by redrawing
Picture1.Line (mRect.Left, _
mRect.Top)-(mRect.Right, mRect.Bottom), , B
Picture1.DrawMode = vbCopyPen
End Sub
By assigning vbInvert to the PictureBox DrawMode property before selection dragging, you can restore the background graphics by redrawing the same rectangle. Once the selection dragging completes, mRect contains the selected rectangle coordinates. You can use the same technique to select a circular region or create the "rubber band" effect.