From: Bee on
I would like to add a freehand lasso to my VB6 app.
Currently I have a box lasso that works OK but would like to be able to
allow the user to freehand draw a lasso to use to cut and paste a image.
Can someone show me how to do this or point me at some smple code or let me
know the correct terminology so I can look for that.

From: Mike Williams on

"Bee" <Bee(a)discussions.microsoft.com> wrote in message
news:61855907-2F51-4A35-B5DB-F4C4F1FEF1EC(a)microsoft.com...

> I would like to add a freehand lasso to my VB6 app.
> Currently I have a box lasso that works OK but would
> like to be able to allow the user to freehand draw a lasso
> to use to cut and paste a image.

Sounds like an interesting little task. Thinking about it a little there
appear to be several different ways of doing it. One method which springs to
mind is to use a fairly standard MouseDown / MouseMove method of drawing the
user's input by drawing connection lines between the points using the PicBox
Line method as he moves the mouse, typically using an XOR drawing operation,
and at the same time to save the coordinates in an array of POINTAPI. When
the user releases the mouse button you close the shape by drawing a
connecting line to the start position. At this point you have a closed shape
drawn on the picture and you also have an array of coordinates describing
the drawn shape.

Then when the user selects your "Cut" menu you firstly erase the drawn
outline, which is most easily achieved by passing the array of coordiantes
in a single call to the Polygon API function whilst the XOR drawing method
is still selected. At this point in time the picture is exactly as it was
before the user drew the outline. Then you pass the same array of
coordinates to the CreatePolygonRgn API in order to create a region
describing the shape. Once you have done that you can use the PaintRgn API
function to paint the shape whatever colour you wish (typically white for a
"Cut" operation).

There are all sorts of different ways of tackling the rest of the job (the
"Paste" operation) depending on exactly what it is you want to do. Once
you've got the desired region of the main picture as a standard Windows
region you can do all sorts of tricks with it, for example you can offeset
it to a new position (either in the same pictureBox or in another) or use it
to copy the appropriate part of the main image to another hidden DC ot
PictureBox, using part of your original image as a brush to paint with,
setting up a clipping region with it and drawing into that region in another
DC and then transparently blitting it, all sorts of different things.

It's an interesting little project, but in the limited time I've currently
got available the best I can do is set you off on the path outlined above.
Here is some code which takes you up to the point where you have allowed the
user to draw the outline of the shape and where you have created the region
and performed the "Cut" operation. Maybe you can take it on from there
yourself. Anyway, paste the following code into a new VB project (just one
Form with a pictureBox on it). Change the hard coded picture path to a
picture that exists on your own system. When you run the project the Picture
will be painted into a PictureBox on the Form. Click anywhere on it and draw
the shape whilst holding down the left mouse button. When you let go of the
left mouse button the shape will be closed and you will be able to see it's
outline (the outline you have drawn). Then click the right mouse button to
perform the "Cut" operation. The code at the moment is just "rough and
ready" testbed code, but it should be a good start for you.

Mike

Option Explicit
Private Declare Function Polygon Lib "gdi32" _
(ByVal hdc As Long, lpPoint As POINTAPI, _
ByVal nCount As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" _
(lpPoint As POINTAPI, ByVal nCount As Long, _
ByVal nPolyFillMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
Private Declare Function PaintRgn Lib "gdi32" _
(ByVal hdc As Long, ByVal hRgn As Long) As Long
Private Const ALTERNATE = 1
Private Const WINDING = 2
Private Type POINTAPI
x As Long
y As Long
End Type
Private Drawing As Boolean, LastOutlineUsed As Boolean
Private d1() As POINTAPI
Private lastCoord As Long, Rgn1 As Long

Private Sub Form_Load()
Dim s1 As String
s1 = "c:\temp\jessica1.jpg"
Picture1.AutoRedraw = True
Picture1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
Picture1.ScaleMode = vbPixels
Picture1.PaintPicture LoadPicture(s1), 0, 0, _
Picture1.ScaleWidth, Picture1.ScaleHeight
End Sub

Private Sub Form_Unload(Cancel As Integer)
DeleteObject Rgn1
End Sub

Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, x As Single, y As Single)
Select Case Button
Case vbLeftButton
If lastCoord > 0 And Not LastOutlineUsed Then
Picture1.DrawMode = vbXorPen
Picture1.FillStyle = vbFSTransparent
Polygon Picture1.hdc, d1(0), lastCoord + 1
End If
Drawing = True
LastOutlineUsed = False
Picture1.ForeColor = vbWhite
Picture1.DrawMode = vbNop
Picture1.PSet (x, y)
Picture1.DrawMode = vbXorPen
ReDim d1(0 To 100)
lastCoord = 0
d1(lastCoord).x = x
d1(lastCoord).y = y
Case vbRightButton
LastOutlineUsed = True
If lastCoord > 0 Then
' erase vbXorPen drawn polygon by redrawing
Polygon Picture1.hdc, d1(0), lastCoord + 1
' create a region using the polygon data
Rgn1 = CreatePolygonRgn(d1(0), lastCoord + 1, WINDING)
' paint the region white
Picture1.FillColor = vbWhite
Picture1.FillStyle = vbFSSolid
Picture1.DrawMode = vbCopyPen
PaintRgn Picture1.hdc, Rgn1
Picture1.Refresh
' add code here to do whatever else you
' wish to do with the region . . .
End If
End Select
End Sub

Private Sub Picture1_MouseMove(Button As Integer, _
Shift As Integer, x As Single, y As Single)
If Not Drawing Then Exit Sub
Picture1.Line -(x, y)
lastCoord = lastCoord + 1
If lastCoord > UBound(d1) Then
ReDim Preserve d1(0 To lastCoord + 100)
End If
d1(lastCoord).x = x
d1(lastCoord).y = y
End Sub

Private Sub Picture1_MouseUp(Button As Integer, _
Shift As Integer, x As Single, y As Single)
Drawing = False
If Button <> vbRightButton Then
Picture1.Line -(d1(0).x, d1(0).y)
End If
End Sub




From: Nobody on
"Bee" <Bee(a)discussions.microsoft.com> wrote in message
news:61855907-2F51-4A35-B5DB-F4C4F1FEF1EC(a)microsoft.com...
>I would like to add a freehand lasso to my VB6 app.
> Currently I have a box lasso that works OK but would like to be able to
> allow the user to freehand draw a lasso to use to cut and paste a image.
> Can someone show me how to do this or point me at some smple code or let
> me
> know the correct terminology so I can look for that.

Not sure if this is what you want:

How to Create Rubber-Band Lines/Boxes in Visual Basic
http://support.microsoft.com/kb/71488


From: Bee on
Thank you both for some good insights.
Unfortuantely I am not that gifted to take it further.
Perhaps there is sample code or books that might take me to the next step.
Suggestions?
I need to be able to copy the selected area to the clipboard and then paste
the selected area only on to a picturebox pixture with a moveable paste image.
I have figured out how to do this with a simple lasso picturebox using
BitBlt and StretchBlt and standard VB6 calls but doing the polygon thing is
not in my vocabulary (yet).
Still learning.
My VB Graphics book only covers such things as drawing 2D and 3D using lines
and arrays ect but has nothing on this type of graphics manipulations.


"Bee" wrote:

> I would like to add a freehand lasso to my VB6 app.
> Currently I have a box lasso that works OK but would like to be able to
> allow the user to freehand draw a lasso to use to cut and paste a image.
> Can someone show me how to do this or point me at some smple code or let me
> know the correct terminology so I can look for that.
>
From: Mike Williams on
"Bee" <Bee(a)discussions.microsoft.com> wrote in message
news:B2CBE975-4A53-46B7-AC1A-557C2876893F(a)microsoft.com...

> I need to be able to copy the selected area to the clipboard
> and then paste the selected area only on to a picturebox
> pixture with a moveable paste image.

Why the clipboard? I thought the copy and the paste operation was for use
within your own VB program?

Mike