Available in the LEADTOOLS Imaging toolkit. |
Outlining, Dragging, and Pasting a Region (Visual Basic)
Take the following steps to add code that lets you outline an area with a mouse, drag a copy of the selected area, and paste the copy into another part of the bitmap:
1. Start with the project that you created in Loading and Displaying an Image.
2. Add a second LEAD control, name it Lead2, and set its Visible property to False.
3. Add the following form-level variables to the declarations procedure of the general object in your main form. In online help, you can use the Edit pull-down menu to copy the block of code.
Dim ReadyToDrag As Boolean 'The state after marking the region but before dragging
Dim Dragging As Boolean 'The state when the mouse is used for dragging the floater
Dim StartX As Single 'Starting X position in screen twips
Dim StartY As Single 'Starting Y position in screen twips
Dim FloaterX As Single 'Floater X position in screen twips
Dim FloaterY As Single 'Floater Y position in screen twips
Dim ZoomFactorX As Single 'Used for translating positioning information
Dim ZoomFactorY As Single 'Used for translating positioning information
4. At the top of your main form, add two command buttons and name them as follows:
Name |
Caption |
SelRgnBtn |
Select Region |
PasteFloaterBtn |
Paste Floater |
5. Add the following code to the SelRgnBtn control's Click procedure. In online help, you can use the Edit pull-down menu to copy the block of code.
Private Sub SelRgnBtn_Click()
Lead1.RgnMarkingMode = RGNMARK_FREEHAND
MsgBox "Draw a freehand region"
End Sub
6. Add the following code to the PasteFloaterBtn control's Click procedure:
Private Sub PasteFloaterBtn_Click()
'Do nothing if there is no floater.
If Lead1.Floater = 0 Then Exit Sub
'Get the floater into another bitmap
Lead2.ScaleMode = Lead1.ScaleMode
Lead2.Bitmap = Lead1.Floater
'Get the floater's client coordinates into local variables.
LCRgnX = Lead1.FloaterDstLeft
LCRgnY = Lead1.FloaterDstTop
LCRgnWidth = Lead1.FloaterDstWidth
LCRgnHeight = Lead1.FloaterDstHeight
'Delete the floater.
Lead1.FloaterVisible = False
Lead1.Floater = 0
'Translate the client coordinates to bitmap coordinates.
LBRgnX = ((LCRgnX - Lead1.DstLeft) / ZoomFactorX) + Lead1.SrcLeft
LBRgnY = ((LCRgnY - Lead1.DstTop) / ZoomFactorY) + Lead1.SrcTop
LBRgnWidth = LCRgnWidth / ZoomFactorX
LBRgnHeight = LCRgnHeight / ZoomFactorY
'Reposition the region to use as a mask for the Combine method.
Lead1.OffsetRgn LBRgnX - Lead1.RgnLeft, LBRgnY - Lead1.RgnTop
'Use the Combine method to paste the Lead2 bitmap into the Lead1 bitmap.
MyOp = CB_OP_ADD + CB_DST_0 'Operation flags for a simple paste.
Lead1.Combine LBRgnX, LBRgnY, LBRgnWidth, LBRgnHeight, Lead2.Bitmap, 0, 0, MyOp
'Repaint the part of the client area that has changed.
Lead1.RepaintRect LCRgnX, LCRgnY, LCRgnWidth, LCRgnHeight, False
'Free the region.
Lead1.FreeRgn
End Sub
7. In the LEAD1 control's MouseDown procedure, add the following code:
Private Sub Lead1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'Do nothing if we are drawing a region.
If Lead1.RgnMarkingMode <> RGNMARK_NONE Then Exit Sub
'Make sure we are using Twips, so that we do not have to translate mouse coordinates.
Lead1.ScaleMode = 1
' Save the starting position, in case we need it.
StartY = y
StartX = x
'If we are ready to drag the selection, get a floater.
If ReadyToDrag = True Then
'Translate the current mouse coordinates.
'These coordinates account for the zoom factor and offset.
ZoomFactorX = Lead1.DstWidth / Lead1.SrcWidth
ZoomFactorY = Lead1.DstHeight / Lead1.SrcHeight
BitmapX = (x / ZoomFactorX) - (Lead1.DstLeft / ZoomFactorX) + Lead1.SrcLeft
BitmapY = (y / ZoomFactorY) - (Lead1.DstTop / ZoomFactorY) + Lead1.SrcTop
'Continue to create the floater if the mouse is pointing to the region we marked.
If Lead1.IsPtInRgn(BitmapX, BitmapY) Then
'Hide the region frame.
Lead1.RgnFrameType = RGNFRAME_NONE
'Create the floater bitmap, which will be the part of the main bitmap that is
'in the region's bounding rectangle.
Lead1.Floater = Lead1.Bitmap
'Translate the bitmap region coordinates to client area coordinates.
NewY = ((Lead1.RgnTop - Lead1.SrcTop) * ZoomFactorY) + Lead1.DstTop
NewX = ((Lead1.RgnLeft - Lead1.SrcLeft) * ZoomFactorX) + Lead1.DstLeft
NewWidth = Lead1.RgnWidth * ZoomFactorX
NewHeight = Lead1.RgnHeight * ZoomFactorY
'Set the initial display position of the floater.
Lead1.SetFloaterDstRect NewX, NewY, NewWidth, NewHeight
'Set form-level variables.
FloaterY = Lead1.FloaterDstTop
FloaterX = Lead1.FloaterDstLeft
Lead1.FloaterVisible = True
Dragging = True
ReadyToDrag = False
End If
End If
End Sub
8. In the LEAD1 control's MouseMove procedure, add the following code.
Private Sub Lead1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
'Do nothing if we are drawing a region.
If Lead1.RgnMarkingMode <> RGNMARK_NONE Then Exit Sub
'Reposition the floater if we are dragging it.
If Dragging = True And Button = 1 And Lead1.Floater <> 0 Then
If Lead1.IsPtInFloater(x, y) Then
'Update the position variables.
XDelta = x - StartX
YDelta = y - StartY
NewX = FloaterX + XDelta
NewY = FloaterY + YDelta
NewWidth = Lead1.FloaterDstWidth
NewHeight = Lead1.FloaterDstHeight
'Reposition the floater.
Lead1.SetFloaterDstRect NewX, NewY, NewWidth, NewHeight
'Save the form-level position variables.
FloaterY = NewY
FloaterX = NewX
StartY = y
StartX = x
End If
End If
End Sub
9. In the LEAD1 control's MouseUp procedure, add the following code.
Private Sub Lead1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
' If we were drawing a region, set up for the next operation.
If Lead1.RgnMarkingMode <> RGNMARK_NONE Then
Lead1.RgnMarkingMode = RGNMARK_NONE
Lead1.RgnFrameType = RGNFRAME_STATIC
ReadyToDrag = True
MsgBox "Now, drag the selection to another place."
End If
End Sub
10. Run your program to test it.