我正在开发一个简单的程序,该程序要求我能够选择一个图片框并通过用鼠标拖动它来将其移动到新位置。这是我目前提出的所有相关代码。但是,当我运行程序时,它会尝试移动到我希望它去的地方,然后它似乎又回到了以前的位置。
编辑:它在一个容器中。如果这有任何相关性。
变量
Dim startx As Integer
Dim starty As Integer
Dim endy As Integer
Dim endx As Integer
Dim finalx As Integer
Dim finaly As Integer
Dim mdown As Boolean
Dim valx As Boolean
Dim valy As Boolean
使图像移动的代码
Private Sub picbox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) 处理 picbox.MouseDown
startx = MousePosition.X
starty = MousePosition.Y
mdown = 真
valx = 假
值 = 假
结束子
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub picbox_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picbox.MouseMove
'Check if mouse=down
If mdown = True Then
endx = (MousePosition.X - Me.Left)
endy = (MousePosition.Y - Me.Top)
If valy = False Then
starty = endy - sender.top
valy = True
End If
If valx = False Then
startx = endx - sender.left
valx = True
End If
sender.left = endx - startx
sender.top = endy - starty
End If
End Sub
Private Sub picbox_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picbox.MouseUp
mdown = False
valx = False
valy = False
End Sub