.net中PictureBox中圖片的拖動(dòng)
發(fā)表時(shí)間:2024-06-15 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要].net中PictureBox中圖片的拖動(dòng)首先在Form窗體上放一個(gè)PictureBox,并指定一個(gè)圖片顯示定義一系列變量處理圖片拖動(dòng) '處理圖片拖動(dòng) Private m_Leftx As Integer Private m_Lefty As Integer ...
.net中PictureBox中圖片的拖動(dòng)
首先在Form窗體上放一個(gè)PictureBox,并指定一個(gè)圖片顯示
定義一系列變量處理圖片拖動(dòng)
'處理圖片拖動(dòng)
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初始化時(shí)做
Me.m_Leftx = Me.PictureBox1.Location.X
Me.m_Lefty = Me.PictureBox1.Location.Y
定義處理鼠標(biāo)按下的事件
'當(dāng)鼠標(biāo)按下時(shí),將鼠標(biāo)變成手形,并且記錄下當(dāng)前鼠標(biāo)的位置
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
定義處理鼠標(biāo)抬起的事件
'處理鼠標(biāo)按鍵抬起的事件,根據(jù)鼠標(biāo)按下時(shí)保存的鼠標(biāo)位置,和當(dāng)前鼠標(biāo)的位置,計(jì)算鼠標(biāo)移動(dòng)偏移量,借此調(diào)用移動(dòng)圖片的函數(shù),移動(dòng)圖片
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
'根據(jù)偏移量計(jì)算出的圖片位置,重畫圖片
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