讲述VB.NET实现拖动图片

讲述VB.NET实现拖动图片

环境:windows7,vb2010


Public Class Form1
    Private m_MouseIsDown As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox2.AllowDrop = True
        PictureBox1.AllowDrop = True
    End Sub

   
    Private Sub PictureBox1_MouseDown(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If Not PictureBox1.Image Is Nothing Then  ' Set a flag to show that the mouse is down. 
            m_MouseIsDown = True
        End If

    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If m_MouseIsDown Then  ' Initiate dragging and allow either copy or move.
            PictureBox1.DoDragDrop(PictureBox1.Image,DragDropEffects.Copy Or DragDropEffects.Move)
        End If
        m_MouseIsDown = False
    End Sub

    Private Sub PictureBox2_DragDrop(ByVal sender As Object,ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
        ' If the CTRL key is not pressed,delete the source picture. 
        If Not e.KeyState = 8 Then
            PictureBox1.Image = Nothing
        End If
    End Sub

    Private Sub PictureBox2_DragEnter(ByVal sender As Object,ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then  ' Check for the CTRL key. 
            If e.KeyState = 9 Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
End Class

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...