欢迎光临
我们一直在努力

.net中PictureBox中图片的拖动-.NET教程,.NET Framework

建站超值云服务器,限时71元/月

 

.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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » .net中PictureBox中图片的拖动-.NET教程,.NET Framework
分享到: 更多 (0)