发新话题
打印

VB中如何使用MOVE事件来使一张图片移动到鼠标指定位置

VB中如何使用MOVE事件来使一张图片移动到鼠标指定位置

我的意思是让那张图片慢慢移动过去不是瞬间完成。。
若用MOVE事件可以先获得鼠标被点击位置
然后让图片判断自己位置慢慢移动到鼠标点击位置。。(用time来控制)
下边是我的想法。。但是实现不了啊。。。
Option Explicit

Dim Ix As Integer, Iy As Integer



Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Ix = X
  Iy = Y
  Timer1.Enabled = Not Timer1.Enabled
End Sub


Private Sub Timer1_Timer()
Dim m As Long, n As Long, p As Long, q As Long
    m = Image1.Left + (Image1.Left - Ix) / Sqr((Image1.Left - Ix) * (Image1.Left - Ix) + (Image1.Top - Iy) * (Image1.Top - Iy))
    n = Image1.Top + (Image1.Top - Iy) / Sqr((Image1.Top - Iy) * (Image1.Top - Iy) + (Image1.Left - Ix) * (Image1.Left - Ix))
    If m = Ix Or n = Iy Then
       Timer1.Enabled = False
    End If
Image1.Move m, n
End Sub

TOP

老是提示益出。。。

TOP

是你算法的问题,你自己仔细研究一下,应当是除数的问题。

TOP

定义有问题吧
我觉得是这样(虽然没试过)
Dim m As Varient, n As Varient(加根号后数是带许多带小数的)
还有我觉得这样好
  m = Image1.Left + (Image1.Left - Ix) / (image1.left-lx+image2.top)*Sqr((Image1.Left - Ix) * (Image1.Left - Ix) + (Image1.Top - Iy) * (Image1.Top - Iy))
那么移动的距离会相等。

TOP

Dim Ix As Variant, Iy As Variant

Private Sub Form_Activate()
Timer1.Enabled = False
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Ix = X
  Iy = Y
  Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Timer1_Timer()
Dim m As Variant, n As Variant
     m = Image1.Left - (Image1.Left - Ix) / Sqr((Image1.Left - Ix) * (Image1.Left - Ix) + (Image1.Top - Iy) * (Image1.Top - Iy))
    n = Image1.Top - (Image1.Top - Iy) / Sqr((Image1.Top - Iy) * (Image1.Top - Iy) + (Image1.Left - Ix) * (Image1.Left - Ix))
    If m = Ix And n = Iy Then
       Timer1.Enabled = False
    End If
Image1.Move m, n
End Sub
这次我试过了,成功了。

TOP

发新话题