Available in the LEADTOOLS Imaging toolkit. |
Outlining, Dragging, and Pasting a Region (Access 2.0)
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 Integer 'The state after marking the region but before dragging
Dim Dragging As Integer '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.
Sub SelRgnBtn_Click ()
Me![LEAD1].Object.RgnMarkingMode = RGNMARK_FREEHAND
MsgBox "Draw a freehand region"
End Sub
6. Add the following code to the PasteFloaterBtn control's Click procedure.
Sub PasteFloaterBtn_Click ()
'Declare the variable for the Combine operation.
Dim MyOp As Long
'Declare variables for the client area coordinates.
Dim LCRgnX, LCRgnY , LCRgnWidth, LCRgnHeight
'Declare variables for the bitmap coordinates.
Dim LBRgnX, LBRgnY , LBRgnWidth, LBRgnHeight
'Do nothing if there is no floater.
If Me![LEAD1].Object.Floater = 0 Then Exit Sub
'Get the floater into another bitmap
Me![LEAD2].Object.ScaleMode = Me![LEAD1].Object.ScaleMode
Me![LEAD2].Object.Bitmap = Me![LEAD1].Object.Floater
'Get the floater's client coordinates into local variables.
LCRgnX = Me![LEAD1].Object.FloaterDstLeft
LCRgnY = Me![LEAD1].Object.FloaterDstTop
LCRgnWidth = Me![LEAD1].Object.FloaterDstWidth
LCRgnHeight = Me![LEAD1].Object.FloaterDstHeight
'Delete the floater.
Me![LEAD1].Object.FloaterVisible = False
Me![LEAD1].Object.Floater = 0
'Translate the client coordinates to bitmap coordinates.
LBRgnX = ((LCRgnX - Me![LEAD1].Object.DstLeft) / ZoomFactorX) + Me![LEAD1].Object.SrcLeft
LBRgnY = ((LCRgnY - Me![LEAD1].Object.DstTop) / ZoomFactorY) + Me![LEAD1].Object.SrcTop
LBRgnWidth = LCRgnWidth / ZoomFactorX
LBRgnHeight = LCRgnHeight / ZoomFactorY
'Reposition the region to use it as a mask for the Combine method.
Me![LEAD1].Object.OffsetRgn LBRgnX - Me![LEAD1].Object.RgnLeft, LBRgnY - Me![LEAD1].Object.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.
Me![LEAD1].Object.Combine LBRgnX, LBRgnY, LBRgnWidth, LBRgnHeight, Me![LEAD2].Object.Bitmap, 0, 0, MyOp
'Repaint the part of the client area that has changed.
Me![LEAD1].Object.RepaintRect LCRgnX, LCRgnY, LCRgnWidth, LCRgnHeight, False
'Free the region.
Me![LEAD1].Object.FreeRgn
End Sub
7. In the LEAD1 control's MouseDown procedure, add the following code.
Sub LEAD1_MouseDown (Button As Integer, Shift As Integer, X As Long, Y As Long)
'Declare local variables.
Dim BitmapX, BitmapY
Dim NewX, NewY, NewWidth, NewHeight
'Do nothing if we are drawing a region.
If Me![LEAD1].Object.RgnMarkingMode <> RGNMARK_NONE Then Exit Sub
'Make sure we are using Twips, so that we do not have to translate mouse coordinates.
Me![LEAD1].Object.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 = Me![LEAD1].Object.DstWidth / Me![LEAD1].Object.SrcWidth
ZoomFactorY = Me![LEAD1].Object.DstHeight / Me![LEAD1].Object.SrcHeight
BitmapX = (x / ZoomFactorX) - (Me![LEAD1].Object.DstLeft / ZoomFactorX) + Me![LEAD1].Object.SrcLeft
BitmapY = (y / ZoomFactorY) - (Me![LEAD1].Object.DstTop / ZoomFactorY) + Me![LEAD1].Object.SrcTop
'Continue to create the floater if the mouse is pointing to the region we marked.
If Me![LEAD1].Object.IsPtInRgn(BitmapX, BitmapY) Then
'Hide the region frame.
Me![LEAD1].Object.RgnFrameType = RGNFRAME_NONE
'Create the floater bitmap, which will be the part of the main bitmap that is
'in the region's bounding rectangle.
Me![LEAD1].Object.Floater = Me![LEAD1].Object.Bitmap
'Translate the bitmap region coordinates to client area coordinates.
NewY = ((Me![LEAD1].Object.RgnTop - Me![LEAD1].Object.SrcTop) * ZoomFactorY) + Me![LEAD1].Object.DstTop
NewX = ((Me![LEAD1].Object.RgnLeft - Me![LEAD1].Object.SrcLeft) * ZoomFactorX) + Me![LEAD1].Object.DstLeft
NewWidth = Me![LEAD1].Object.RgnWidth * ZoomFactorX
NewHeight = Me![LEAD1].Object.RgnHeight * ZoomFactorY
'Set the initial display position of the floater.
Me![LEAD1].Object.SetFloaterDstRect NewX, NewY, NewWidth, NewHeight
'Set form-level variables.
FloaterY = Me![LEAD1].Object.FloaterDstTop
FloaterX = Me![LEAD1].Object.FloaterDstLeft
Me![LEAD1].Object.FloaterVisible = True
Dragging = True
ReadyToDrag = False
End If
End If
End Sub
8. In the LEAD1 control's MouseMove procedure, add the following code.
Sub LEAD1_MouseMove (Button As Integer, Shift As Integer, X As Long, Y As Long)
'Declare variables for repositioning the floater.
Dim NewX, NewY, NewWidth, NewHeight, XDelta, YDelta
'Do nothing if we are drawing a region.
If Me![LEAD1].Object.RgnMarkingMode <> RGNMARK_NONE Then Exit Sub
'Reposition the floater if we are dragging it.
If Dragging = True And Button = 1 And Me![LEAD1].Object.Floater <> 0 Then
If Me![LEAD1].Object.IsPtInFloater(x, y) Then
'Update the position variables.
XDelta = x - StartX
YDelta = y - StartY
NewX = FloaterX + XDelta
NewY = FloaterY + YDelta
NewWidth = Me![LEAD1].Object.FloaterDstWidth
NewHeight = Me![LEAD1].Object.FloaterDstHeight
'Reposition the floater.
Me![LEAD1].Object.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.
Sub LEAD1_MouseUp (Button As Integer, Shift As Integer, X As Long, Y As Long)
' If we were drawing a region, set up for the next operation.
If Me![LEAD1].Object.RgnMarkingMode <> RGNMARK_NONE Then
Me![LEAD1].Object.RgnMarkingMode = RGNMARK_NONE
Me![LEAD1].Object.RgnFrameType = RGNFRAME_STATIC
ReadyToDrag = True
MsgBox "Now, drag the selection to another place."
End If
End Sub
10. Run your program to test it.