程序中线的位置是(1080,1800)-(5040,1800),左边的小球是imgball,现在要求imgball只能在左右方向拖动,并且不超过线的范围,线的最左边的值是0,最右边是100,值显示在txtvalue中请求高手帮助~在线等~

解决方案 »

  1.   

    看不到图...
    区域长度范围左右两边都减去imgball.width/2
    以imgball的中心为坐标点取比例
      

  2.   

    还有,在线中的任意一个地方单击,球平滑的滑动过去,并给txtvalue赋值
      

  3.   

    假定水平线控件是 imgLine
    Option ExplicitPrivate m_OnDrag As BooleanPrivate m_Value As LongFriend Property Get Value() As Long
        Value = m_Value
    End Property
    Friend Property Let Value(ByVal RHS As Long)
        Dim X As Long
        
        If RHS < 0 Then
            m_Value = 0
        ElseIf RHS > 100 Then
            m_Value = 100
        Else
            m_Value = RHS
        End If
        Text1.Text = CStr(m_Value)
        
        X = 1080 + 39.6 * m_Value
        imgball.Move X - imgball.Width \ 2, 1800 - imgball.Height \ 2
    End PropertyFriend Sub DragTo(ByVal X As Long)
        Me.Value = (X - 1080) / 39.6
    End SubPrivate Sub Form_Load()
        Me.Value = 0
    End SubPrivate Sub imgball_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            m_OnDrag = True
            Me.DragTo imgball.Left + X
        End If
    End SubPrivate Sub imgball_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If m_OnDrag Then
            Me.DragTo imgball.Left + X
        End If
    End SubPrivate Sub imgball_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If m_OnDrag And (Button = vbLeftButton) Then
            m_OnDrag = False
        End If
    End SubPrivate Sub imgLine_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            Me.DragTo imgLine.Left + X
        End If
    End Sub
      

  4.   

    感谢上面的朋友,现在还有一个问题是:当单击imgball的时候,图片会瞬间移动到鼠标中心,能不能不要这样,直接随着鼠标拖动,还有在线上单击imgball不会移动哎!
      

  5.   

    1)在 imgball_MouseDown 不调用 DragTo,而是将 X 记录下来,作为以后 DragTo 的修正量。
    2)imgLine_MouseDown 中不是调用了 DragTo,移动过去了啊!如果要不放手继续拖动,在这里也打开 m_OnDrag 开关。