分享:鼠标拖动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
...全文
689 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
skidtren 2012-02-06
  • 打赏
  • 举报
回复
学习了
tomoonvivi 2010-04-12
  • 打赏
  • 举报
回复
m_Leftx - 152 是什么意思?为什么要减152?
wooyoncs 2009-09-13
  • 打赏
  • 举报
回复
不错,学习了
vv0147 2009-09-13
  • 打赏
  • 举报
回复
学习了
谢谢
zrl05 2009-09-12
  • 打赏
  • 举报
回复
恩,慎重啊,仅供参考
我是菜鸟,地球人都知道...


[Quote=引用 3 楼 cgz2568 的回复:]
谢谢楼主的共享。。
[/Quote]
cgz2568 2009-09-12
  • 打赏
  • 举报
回复
谢谢楼主的共享。。
zrl05 2009-09-12
  • 打赏
  • 举报
回复
赫赫,感谢您的帮助。 
不知道这是不是解决此问题的正道阿?
别又走了歪路...


[Quote=引用 1 楼 dylike 的回复:]
恭喜解决问题.
赞扬共享精神.
[/Quote]
dylike 2009-09-12
  • 打赏
  • 举报
回复
恭喜解决问题.
赞扬共享精神.

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧