Got a problem with a mousemove event in my PTZ camera controller created with VB6.

It contains a picturebox as seen in the image below, which is used as a virtual joystick.



I have created a boolean flag called IsMouseDown so that any mouse move event while the mouse is down will cause the camera to move in a different direction and at different speeds depending on the position and distance from the centre of the picturebox. Apart from setting the flag the mousedown event also moves the camera.

It works great . . .just like a joystick. The problem is that if the mouse moves outside the picturebox while the mouse is down, the camera still follows the mouse movement. This only happens if the mousedown event is initially triggered in the picturebox. So if the mousedown event only applies when the mouse is over the picturebox as it should be, why is the mousemove event still active when it strays outside the picturebox because they are both related to the picturebox component.

----------------------------------------------------------------------------------------------------------------------
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

IsMouseDown = True 'Sets boolean flag IsMouseDown to true

PanPosition = (x)
TiltPosition = (y)

If PanPosition < 0 Then 'Selects the pan direction. Center of Picturbox1 is 0. Left of center selects pan left and vice versa.
CamData(3) = &H4
ElseIf PanPosition > 0 Then
CamData(3) = &H2
Else: CamData(3) = 0
End If

If TiltPosition < 0 Then 'Selects the tilt direction. Center of Picturbox1 is 0. Above center selects tilt up and vice versa.
CamData(3) = CamData(3) + &H10
ElseIf TiltPosition > 0 Then
CamData(3) = CamData(3) + &H8
Else: CamData(3) = CamData(3) + 0
End If

CamData(4) = Abs(PanPosition) 'Removes the minus sign and sends x and y mouse position values to the CamData array-
CamData(5) = Abs(TiltPosition) 'and uses this data to select camera pan/tilt speed.

SendOutput

End Sub

----------------------------------------------------------------------------------------------

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)

If IsMouseDown = True Then 'Checks IsMouseDown boolean flag

PanPosition = (x)
TiltPosition = (y)

If PanPosition < 0 Then 'Selects the pan direction. Center of Picturbox1 is 0. Left of center selects pan left and vice versa.
CamData(3) = &H4
ElseIf PanPosition > 0 Then
CamData(3) = &H2
Else: CamData(3) = 0
End If

If TiltPosition < 0 Then 'Selects the tilt direction. Center of Picturbox1 is 0. Above center selects tilt up and vice versa.
CamData(3) = CamData(3) + &H10
ElseIf TiltPosition > 0 Then
CamData(3) = CamData(3) + &H8
Else: CamData(3) = CamData(3) + 0
End If

CamData(4) = Abs(PanPosition) 'Removes the minus sign and sends x and y mouse position values to the CamData array-
CamData(5) = Abs(TiltPosition) 'and uses this data to select camera pan/tilt speed.

SendOutput

Else

StopAllMovement

End If

End Sub

--------------------------------------------------------------------------------------------

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

IsMouseDown = False 'Sets boolean flag IsMouseDown to false

End Sub

-------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

Tried adding this below so that if the mouse moves over the form or the picturebox lost the focus it should stop all movement but it makes no difference

--------------------------------------------------------------------------------------------

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

StopAllMovement

End Sub

----------------------------------------------------------------------------------------------

Private Sub Picture1_LostFocus()

StopAllMovement

End Sub

-------------------------------------------------------------------------------------------------

Not really part of the above problem but don't fully understand why I had these problems below either:

I have had to repeat much of the same code for the mousedown and mousemove event. When I tried to create it as a seperate sub and called from each event it wouldn't work at all (no camera movement).

Also tried the mousemove as a 'Do While IsMouseDown' loop but could only manage to get it to work using the 'If IsMouseDown Then' loop even though Do While seems to make more sense. If I use a Do While loop the camera doesn't stop when the mouse is released even though I wrote it as Do While ISMouseDown = True. It seemed to get stuck within the loop and didnt see the boolean flag change with the mouseup event.

Hope I've explained it ok. (">