ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Drag label position on userform at runtime - jerky movement (https://www.excelbanter.com/excel-programming/378817-drag-label-position-userform-runtime-jerky-movement.html)

[email protected]

Drag label position on userform at runtime - jerky movement
 
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


[email protected]

Drag label position on userform at runtime - jerky movement
 
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


wrote:
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




All times are GMT +1. The time now is 03:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com