怎么样实现拖动一个按钮,并且不允许它被拖到FORM窗体的外面?

解决方案 »

  1.   

    简单写个例子给,自己完善吧Option Explicit
    Private OnDrag As Boolean, cx As Long, cy As Long
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 And OnDrag = False Then
            Command1.Drag vbBeginDrag
            cx = X
            cy = Y
            OnDrag = True
        End If
    End SubPrivate Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
        If Source Is Command1 Then
            Command1.Drag vbEndDrag
            Command1.Move X - cx, Y - cy
            OnDrag = False
        End If
    End Sub
      

  2.   

    比较喜欢这样写~ ^ ^Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim SMode As Integer
        Static OldX As Single, OldY As Single
        
        SMode = Me.ScaleMode
        Me.ScaleMode = vbTwips    If Button = vbLeftButton Then
            With Command1
                .Move .Left + X - OldX, .Top + Y - OldY
            End With
        Else
            OldX = X
            OldY = Y
        End If    Me.ScaleMode = SMode
    End Sub
      

  3.   

    一个按钮能拖到FORM外面吗,没见过,告诉我怎么实现