温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET中怎么实现拖动图片功能

发布时间:2021-08-09 15:02:09 来源:亿速云 阅读:156 作者:Leah 栏目:编程语言

VB.NET中怎么实现拖动图片功能,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。


1、 在Form中添加两个PictureBox控件。
2、 在代码窗体中添加如下代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _  System.EventArgs) Handles MyBase.Load  ' Enable dropping.  PictureBox2.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_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   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

注意到上面的例子中第二个PictureBox控件的AllowDrop属性是在Form1_load事件中设置的,这是因为设计时PictureBox并没有AllowDrop属性。在MouseDown事件中,代码首先检测是否有要赋给PictureBox的图片;如果没有的话,当你移动图片后,接下来的click将引发一个意外。还应该注意到的是在DragEnter和DragDrop事件中代码检测CTRL键是否被按下,从而决定是否是复制还是VB.NET实现拖动图片。

为什么值会不同呢?在 DragEnter事件中,当鼠标左键按下时,产生的值是1,在加上CTRL的值8,从而值为9。见KeyState枚举列表DragEventArgs.KeyState Property到目前为止,这两个例子处理的都是同一窗体不同控件间的拖放,然而在同一应用程序的不同窗体上同样适用。

关于VB.NET中怎么实现拖动图片功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI