溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET中怎么實現(xiàn)拖動圖片功能

發(fā)布時間:2021-08-09 15:02:09 來源:億速云 閱讀:165 作者:Leah 欄目:編程語言

VB.NET中怎么實現(xiàn)拖動圖片功能,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。


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將引發(fā)一個意外。還應該注意到的是在DragEnter和DragDrop事件中代碼檢測CTRL鍵是否被按下,從而決定是否是復制還是VB.NET實現(xiàn)拖動圖片。

為什么值會不同呢?在 DragEnter事件中,當鼠標左鍵按下時,產(chǎn)生的值是1,在加上CTRL的值8,從而值為9。見KeyState枚舉列表DragEventArgs.KeyState Property到目前為止,這兩個例子處理的都是同一窗體不同控件間的拖放,然而在同一應用程序的不同窗體上同樣適用。

關于VB.NET中怎么實現(xiàn)拖動圖片功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI