我正在处理一个有十个图片框的表单。儿童应该能够将图片从一个图片框拖到任何其他图片框中。我读了一篇很好的微软文章,解释了在两个图片框之间设置拖放的步骤。有五个子程序可以将一种方式拖放到第二个图片框中。
我担心的是,如果表单有十个图片框,并且用户可以在十个框之间拖放,那就是很多代码和很多子例程。
有更优雅的方法吗?我把下面的代码放在两个图片框之间使用拖放。我正在使用Visual Basic 2010 Express。感谢。
' Enable dropping.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
PictureBox2.AllowDrop = True
End Sub
' Set a flag to show that the mouse is down for PictureBox1
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
MouseIsDown = True
End If
End Sub
' Initiate dragging and allow either copy or move for PictureBox1
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If MouseIsDown Then
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or _
DragDropEffects.Move)
End If
MouseIsDown = False
End Sub
'Copy or Move data from PictureBox1
Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles PictureBox1.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
' Assign the image to the PictureBox2
Private Sub PictureBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
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
答案 0 :(得分:2)
您可以使用AddHandler将任意数量的PictureBox连接到相同的处理程序方法。 "发件人"这些处理程序中的参数将告诉您哪个PictureBox是事件的来源。请注意下面的代码中没有一种方法具有" Handles"他们两端的条款;所有内容都在Form的Load()事件中动态连接。表格直接包含的所有PictureBox都将被连线。在我的测试项目中,我将其中的十个放在表单上:
Public Class Form1
Private Source As PictureBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each PB As PictureBox In Me.Controls.OfType(Of PictureBox)()
PB.AllowDrop = True
AddHandler PB.MouseMove, AddressOf PBs_MouseMove
AddHandler PB.DragEnter, AddressOf PBs_DragEnter
AddHandler PB.DragDrop, AddressOf PBs_DragDrop
AddHandler PB.DragOver, AddressOf PBs_DragOver
Next
End Sub
Private Sub PBs_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
If Not IsNothing(PB.Image) AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
Source = PB
PB.DoDragDrop(PB.Image, DragDropEffects.Copy Or DragDropEffects.Move)
End If
End Sub
Private Sub PBs_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
If My.Computer.Keyboard.CtrlKeyDown Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PBs_DragOver(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
If My.Computer.Keyboard.CtrlKeyDown Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
PB.Image = e.Data.GetData(DataFormats.Bitmap)
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = Nothing
End If
End If
End Sub
End Class
来自评论:
使用AddHandlers,可以"交换"一张两张照片 拖/放?
当然是;而且很容易随着下面代码更改为DragDrop(),移动的默认行为将是交换两个图像。如果他们按住Ctrl键复制,那么它将覆盖而不进行交换:
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
Dim tmpImage As Image = PB.Image ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End Sub
来自评论:
如何将一个特定的图片框排除在" draggable"?
之外
在Load()事件中,您要检查要在循环中排除的PB。例如,这将排除" PictureBox1":
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each PB As PictureBox In Me.Controls.OfType(Of PictureBox)()
If Not (PB Is PictureBox1) Then
PB.AllowDrop = True
AddHandler PB.MouseMove, AddressOf PBs_MouseMove
AddHandler PB.DragEnter, AddressOf PBs_DragEnter
AddHandler PB.DragDrop, AddressOf PBs_DragDrop
AddHandler PB.DragOver, AddressOf PBs_DragOver
End If
Next
End Sub
答案 1 :(得分:0)
关于我关于将某些图片框排除在可拖动状态的最后一个问题(参见最后评论),我发现了图片框的“启用”属性。根据需要打开或关闭该属性非常容易。