.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
新聞熱點
疑難解答
圖片精選