分享:鼠标拖动Picturebox中的图片移动(VB2008中运行通过)
zrl05 2009-09-12 05:19:01 自己菜鸟一个,在大家帮助下总算实现了这个功能。贴上来供大家参考,如果要更好的方法,请指正。
1. 功能描述
Picturebox1中图片太大时,要么使SizeMode方式为Stretchimage,要么使用拖动方式。本程序实现了鼠标拖动图片移动功能。
2. 程序界面情况
在VB2008中运行通过,建立窗体Form1,引进控件Picturebox(名字为Picturebox1),载入图片。
参数均采用默认值----当然,为了看的清楚的话,可把Picturebox1中的BorderStyle改为Fixed3D
3.源代码
Imports System.Drawing
Public Class Form1
Private m_Leftx As Integer = 152
Private m_Lefty As Integer = 0
Dim m_MousePosX As Integer
Dim m_MousePosY As Integer
Dim m_DeltaX As Integer
Dim m_DeltaY As Integer
Dim m_StrecthX As Double
Dim m_StrecthY As Double
'@#处理鼠标按键抬起的事件,根据鼠标按下时保存的鼠标位置,和当前鼠标的位置,
'计算鼠标移动偏移量,借此调用移动图片的函数,移动图片
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
m_DeltaX = m_MousePosX - e.X
m_DeltaY = m_MousePosY - e.Y
m_Leftx = m_Leftx - m_DeltaX
m_Lefty = m_Lefty - m_DeltaY
picturemove(sender, e)
Me.Cursor = System.Windows.Forms.Cursors.Arrow
End Sub
'@#当鼠标按下时,将鼠标变成手形,并且记录下当前鼠标的位置
Private Sub PictureBox1_MouseDown(ByVal sender As 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 picturemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
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()
End Sub
End Class