Rubberband like feature in a picture box

NEOLLE
03-08-2005, 01:33 AM
Hello Guys,
Im want to create a line by clicking two points on a picturebox. I would like to obtain a rubberbanding feature where a line is drawn tailing the mouse pointer after the first point is selected and until the second point is selected.

There are several lines already drawn in my picturebox. I saved the attributes of these lines via UDT.

My first move in obtaining this rubberbanding feature is I used the CLS method and loop through all the items in my UDT and draw them one by one during MouseMove event.

This works nicely, but the picture box flickers a lot while the loop is on going.

Is there a better way of obtaining a rubberband like feature without the flicker?
Is there a picture box property like a Redraw = False which eliminates flicker?

Thanks! :)

the-copy
03-08-2005, 04:26 AM
I am using this and it's not flickering.

Let's say that firstx is the first x-point and firsty is the y-point, and line contains wiether or not your going to draw the line


Dim line as Boolean

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

line = True
firstx = X
firsty = Y

End Sub


Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

If line = True Then

Picture1.Cls
Picture1.Line (firstx, firsty)-(X,Y)

End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

If line = True Then

Picture1.Cls
Picture1.Line (firstx, firsty)-(X,Y)
line = False

End Sub


Picture1.AutoRedraw = True


I dosen't know if that would help you but i hope so. GLHF ;)

BillSoo
03-08-2005, 04:57 AM
A simpler way would be to just draw your rubber band box using the NOT XOR operation. This eliminates the need to cls your picturebox since the NOT XOR operation can be used to erase the old box.

BillSoo
03-08-2005, 05:11 AM
Here is some example code with a picturebox on a form:

Option Explicit
Dim StartX As Single
Dim StartY As Single
Dim LastX As Single
Dim LastY As Single
Dim oldMode As Integer

Private Sub Form_Load()
Dim i As Integer
Dim mx As Integer
Dim my As Integer

Picture1.ScaleMode = vbPixels
Picture1.AutoRedraw = True
mx = Picture1.ScaleWidth
my = Picture1.ScaleHeight
For i = 1 To 30
Picture1.Line (Rnd() * mx, Rnd() * my)-(Rnd() * mx, Rnd() * my)
Next i
End Sub


Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
StartX = X
LastX = X
StartY = Y
LastY = Y
oldMode = Picture1.DrawMode
Picture1.DrawMode = 10 'not xor pen
Picture1.Line (StartX, StartY)-(LastX, LastY)
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button Then
Picture1.Line (StartX, StartY)-(LastX, LastY)
LastX = X
LastY = Y
Picture1.Line (StartX, StartY)-(LastX, LastY)
End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.Line (StartX, StartY)-(LastX, LastY)
Picture1.DrawMode = oldMode
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum