.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
新闻热点
疑难解答
图片精选