From: Larry Serflaten on

"Webbiz" <nospam(a)forme.thanks.com> wrote

> Now I know that I can draw a line using the picbox.line method. But as
> I move the mouse, I'll need to delete the previous line so it makes it
> look like the vertical line is moving. How would I do this, and do it
> without erasing anything that the line has drawn over (whatever was
> underneath at the time the line was drawn over it)?
>
> With the line control, that was not a problem. Just give the
> coordinates and it would move there without hassle.
>
> Suggestions?

You can use the XOR drawmode to both draw and erase a line. Unlike
the Line control however, XOR mode allows you to see the line no matter
what the underlying picture color is. On black areas the XOR line is white,
and on white areas the XOR line is black, etc...

Add a picturebox to a new form and try out the code below. Hold the
left mouse button down to see the lines track where the mouse goes.

LFS


Option Explicit
Private XX&, YY&

Private Sub Form_Load()
Picture1.AutoRedraw = True
Picture1.Move 90, 90, 3000, 3000
Picture1.PaintPicture Me.Icon, 0, 0, 3000, 3000
Picture1.Line (30, 30)-Step(300, 300), vbBlack, BF
Picture1.Line (330, 30)-Step(300, 300), vbRed, BF
Picture1.Line (630, 30)-Step(300, 300), vbBlue, BF
Picture1.Line (930, 30)-Step(300, 300), vbGreen, BF
Picture1.Line (1230, 30)-Step(300, 300), vbWhite, BF
Picture1.DrawMode = vbXorPen
Picture1.ForeColor = vbWhite
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawLine Button, X, Y
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawLine Button, XX, YY
DrawLine Button, X, Y
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawLine Button, XX, YY
End Sub

Sub DrawLine(ByVal B&, ByVal X&, ByVal Y&)
If B = vbLeftButton Then
Picture1.Line (X, 0)-Step(0, Picture1.ScaleHeight)
Picture1.Line (0, Y)-Step(Picture1.ScaleWidth, 0)
XX = X
YY = Y
End If
End Sub





From: Larry Serflaten on

"Larry Serflaten" <serflaten(a)usinternet.com> wrote

> You can use the XOR drawmode to both draw and erase a line. Unlike
> the Line control however, XOR mode allows you to see the line no matter
> what the underlying picture color is. On black areas the XOR line is white,
> and on white areas the XOR line is black, etc...

I stand corrected. On some shades of gray, the XOR value is also gray,
making the line hard to see. Use as you see fit...

LFS


From: Webbiz on
On Tue, 3 Nov 2009 18:35:01 -0500, "Larry Serflaten"
<serflaten(a)usinternet.com> wrote:

>
>"Webbiz" <nospam(a)forme.thanks.com> wrote
>
>> Now I know that I can draw a line using the picbox.line method. But as
>> I move the mouse, I'll need to delete the previous line so it makes it
>> look like the vertical line is moving. How would I do this, and do it
>> without erasing anything that the line has drawn over (whatever was
>> underneath at the time the line was drawn over it)?
>>
>> With the line control, that was not a problem. Just give the
>> coordinates and it would move there without hassle.
>>
>> Suggestions?
>
>You can use the XOR drawmode to both draw and erase a line. Unlike
>the Line control however, XOR mode allows you to see the line no matter
>what the underlying picture color is. On black areas the XOR line is white,
>and on white areas the XOR line is black, etc...
>
>Add a picturebox to a new form and try out the code below. Hold the
>left mouse button down to see the lines track where the mouse goes.
>
>LFS
>
>
>Option Explicit
>Private XX&, YY&
>
>Private Sub Form_Load()
> Picture1.AutoRedraw = True
> Picture1.Move 90, 90, 3000, 3000
> Picture1.PaintPicture Me.Icon, 0, 0, 3000, 3000
> Picture1.Line (30, 30)-Step(300, 300), vbBlack, BF
> Picture1.Line (330, 30)-Step(300, 300), vbRed, BF
> Picture1.Line (630, 30)-Step(300, 300), vbBlue, BF
> Picture1.Line (930, 30)-Step(300, 300), vbGreen, BF
> Picture1.Line (1230, 30)-Step(300, 300), vbWhite, BF
> Picture1.DrawMode = vbXorPen
> Picture1.ForeColor = vbWhite
>End Sub
>
>Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
> DrawLine Button, X, Y
>End Sub
>
>Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
> DrawLine Button, XX, YY
> DrawLine Button, X, Y
>End Sub
>
>Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
> DrawLine Button, XX, YY
>End Sub
>
>Sub DrawLine(ByVal B&, ByVal X&, ByVal Y&)
> If B = vbLeftButton Then
> Picture1.Line (X, 0)-Step(0, Picture1.ScaleHeight)
> Picture1.Line (0, Y)-Step(Picture1.ScaleWidth, 0)
> XX = X
> YY = Y
> End If
>End Sub
>

That was illuminating.

Appreciate the code example. Seeing teaches a lot.

Webbiz
From: Saga on
>"...you should Unload both of them, unloading the Line first and then the
>PictureBox."

Is it prudent to set the Container property of the pic box
to Nothing after unloading the Line or simply not necessary?
Thanks, Saga


From: Mike Williams on
"Saga" <antiSpam(a)nowhere.com> wrote in message
news:%23VWlHBWXKHA.1232(a)TK2MSFTNGP05.phx.gbl...
> >"...you should Unload both of them, unloading the Line first and then the
> >PictureBox."
>
> Is it prudent to set the Container property of the pic box
> to Nothing after unloading the Line or simply not necessary?
> Thanks, Saga

It's not necessary. In fact it's not possible. You cannot set a control's
container to Nothing. All you need to do before Unloading it from the array
is to make sure it is not itself acting as the container for anything.

Mike