Drag label position on userform at runtime - jerky movement

H

hooksie2

I want to be able to drag a label (or other suitable control to act
like a box) on a userform at runtime.

I have written the following to do this but have these problems:
1) the movement is quite jerky - at times the label can even jump
backwards although I am only moving the mouse fowards
2) the mouse gets ahead of the label

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Static sngXOld As Single
Dim sngDelta As Single

If m_blnMouseIsDown Then
sngDelta = (X - sngXOld)
Me.Label1.Left = Me.Label1.Left + sngDelta
Me.Label2.Left = Me.Label2.Left + sngDelta
DoEvents
sngXOld = X
End If
End Sub

I can improve things a little bit by only moving the label once the
mouse is moved a certain minimum distance, ie. If Abs(sngDelta) > 5,
but it's still not perfect and the mouse still gets ahead of the label.

Can anyone suggest an alternative approach - or have I simply
overlooked something here?

Thanks a lot,
Andrew
 
H

hooksie2

Solved it.

I am measuring the cursor position relative to the label position - but
at the same time I am moving the label so if the mouse movement slows
down (for a fraction of a second) the cursor position relative to the
label can move in the opposite direction - it is this that causes the
flicker.

To overcome this I am now using the GetCursorPos API (together with
conversion from pixels to point count) and use that to track the
relative mouse movement. Works perfectly :)

Best regards,
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top