运行用户窗体时移动形状

时间:2018-07-09 19:10:37

标签: excel vba userform

我试图在已经运行的用户窗体上“解锁”固定图像,以便我可以自由移动它,然后再次单击它,使它再次处于固定位置。

1 个答案:

答案 0 :(得分:2)

认为这可以满足您的需求。

Dim imgOriginX As Double
Dim imgOriginY As Double
Dim clicked As Boolean

Private Sub UserForm_Activate()
    clicked = False
End Sub

Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     If clicked = True Then
        imgOriginX = X
        imgOriginY = Y
    End If
End Sub

Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If clicked = True Then
        clicked = False
    Else
        clicked = True
    End If
End Sub

Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     If clicked = True Then
        If Button And 1 Then
            Image1.Left = Image1.Left + (X - imgOriginX)
            Image1.Top = Image1.Top + (Y - imgOriginX)
        End If
    End If
End Sub