编程教程
您现在的位置: 中国个人站长站 >> 网络编程 >> ASP.NET教程 >> 教程正文 .net中PictureBox中图片的拖动
推荐位

.net中PictureBox中图片的拖动

中国个人站长站 ASP.NET教程 点击数: 更新时间:2007-7-8 1:34:00

.net中PictureBox中图片的拖动
首先在Form窗体上放一个PictureBox,并指定一个图片显示

定义一系列变量处理图片拖动
 '处理图片拖动
        Private m_Leftx As Integer
        Private m_Lefty As Integer
        Dim m_MousePosX As Integer
        Dim m_MousePosY As Integer
        Dim m_DriftX As Integer
        Dim m_DriftY As Integer
并给赋初值,可以在Form初始化时做
  Me.m_Leftx = Me.PictureBox1.Location.X
        Me.m_Lefty = Me.PictureBox1.Location.Y

定义处理鼠标按下的事件

 '当鼠标按下时,将鼠标变成手形,并且记录下当前鼠标的位置
  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 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 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

教程录入:swh    责任编辑:swh 
个人站长站与你风雨同舟!
本站所提供的资源均来源于互联网,如有侵权行为,请与本站管理员联系,我们会第一时间删除!
·如果您发现《.net中PictureBox中图片的拖动》文章有错误,也请通知我们修改!
联系邮箱chinageren#126.com,谢谢支持!
站内搜索:
广告服务 | 友情链接 | 联系我们 | 免责声明 | 用户留言 | 网站导航
版权所有:中国个人站长站 2007-2008 未经授权禁止复制或建立镜像 客服QQ号:112731235
copyright © 2007-2008 www.ChinaGeRen.com online services. all rights reserved. 苏ICP备05000059号