讲述VB.NET实现拖动图片

  1. 环境:windows7 ,vb2010
  2. Public Class Form1
  3. Private m_MouseIsDown As Boolean
  4. Private Sub Form1_Load(ByVal senderAs System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
  5. PictureBox2.AllowDrop = True
  6. PictureBox1.AllowDrop = True
  7. End Sub
  8. Private Sub PictureBox1_MouseDown(ByVal senderAs Object,ByVal e As System.Windows.Forms.MouseEventArgs)Handles PictureBox1.MouseDown
  9. If Not PictureBox1.ImageIs NothingThen ' Set a flag to show that the mouse is down.
  10. m_MouseIsDown = True
  11. End If
  12. End Sub
  13. Private Sub PictureBox1_MouseMove(ByVal senderAs Object,ByVal e As System.Windows.Forms.MouseEventArgs)Handles PictureBox1.MouseMove
  14. If m_MouseIsDown Then ' Initiate dragging and allow either copy or move.
  15. PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or DragDropEffects.Move)
  16. End If
  17. m_MouseIsDown = False
  18. End Sub
  19. Private Sub PictureBox2_DragDrop(ByVal senderAs Object,ByVal e As System.Windows.Forms.DragEventArgs)Handles PictureBox2.DragDrop
  20. ' Assign the image to the PictureBox.
  21. PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
  22. ' If the CTRL key is not pressed, delete the source picture.
  23. If Not e.KeyState = 8Then
  24. PictureBox1.Image = Nothing
  25. End If
  26. End Sub
  27. Private Sub PictureBox2_DragEnter(ByVal senderAs Object,ByVal e As System.Windows.Forms.DragEventArgs)Handles PictureBox2.DragEnter
  28. If e.Data.GetDataPresent(DataFormats.Bitmap)Then ' Check for the CTRL key.
  29. If e.KeyState = 9 Then
  30. e.Effect = DragDropEffects.Copy
  31. Else
  32. e.Effect = DragDropEffects.Move
  33. End If
  34. Else
  35. e.Effect = DragDropEffects.None
  36. End If
  37. End Sub
  38. End Class
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值