.net中PictureBox中圖片的拖動
發(fā)表時間:2024-02-12 來源:明輝站整理相關軟件相關文章人氣:
[摘要].net中PictureBox中圖片的拖動首先在Form窗體上放一個PictureBox,并指定一個圖片顯示定義一系列變量處理圖片拖動 '處理圖片拖動 Private m_Leftx As Integer Private m_Lefty As Integer ...
.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
定義處理鼠標抬起的事件
'處理鼠標按鍵抬起的事件,根據(jù)鼠標按下時保存的鼠標位置,和當前鼠標的位置,計算鼠標移動偏移量,借此調(diào)用移動圖片的函數(shù),移動圖片
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ù)偏移量計算出的圖片位置,重畫圖片
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