VB2010如何编程实现图片移动
在VB2010中,用鼠标点击窗体上图形控件,移动到指定位置,最好有动画效果?
基本思路,先记下按下鼠标时的坐标
然后再得到放开鼠标时的坐标
计算得出,鼠标在纵向y,横向x的位移分别是多少,最后利用PicGrh.DrawImageUnscaled 重画图片
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'赋初值,图片初始值
Me.m_Leftx = Me.PictureBox1.Location.X
Me.m_Lefty = Me.PictureBox1.Location.Y
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'按下鼠标位置
Me.Cursor = System.Windows.Forms.Cursors.Hand
m_MousePosX = e.X
m_MousePosY = e.Y
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
'放开鼠标,计算移动位置,重定位图片位置
m_DriftX = m_MousePosX - e.X
m_DriftY = m_MousePosY - e.Y
m_Leftx = m_Leftx - m_DriftX
m_Lefty = m_Lefty - m_DriftY
picturemove(sender, e)
Me.Cursor = System.Windows.Forms.Cursors.Arrow
End Sub
Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
Static m As Integer
If m > 1 And m <= 100 And e.Delta > 0 Then
m = m + 5
ElseIf m > 1 And m <= 100 And e.Delta < 0 Then
m = m - 5
Else
m = 100
End If
Me.Opacity = m * 0.01
' TextBox1.Text = m
Me.TopMost = True
End Sub
Private Sub picturemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Try
Dim myBit As New System.Drawing.Bitmap(PictureBox1.Image)
Dim myPicGrh As System.Drawing.Graphics = Me.PictureBox1.CreateGraphics
myPicGrh.Clear(Me.PictureBox1.BackColor)
myPicGrh.DrawImageUnscaled(myBit, m_Leftx - 152, m_Lefty)
myBit.Dispose()
myPicGrh.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub