我想将图片框拖放到新的图片框中。
我使用的代码,但想知道是否需要更多代码来允许drga和drop。
public void mouseEvents()
{
// Allow mouse down
pbMIDI.MouseDown += pb1_MouseDown;
// Set picture poxes to allow drop
dragTo1.AllowDrop = true;
// Allow enter and drop on each picture box
drop1.DragEnter += drag1_DragEnter;
drop1.DragDrop += drop1_DragDrop;
}
// MIDI Drag and Drop
private void pb1_MouseDown(object sender, MouseEventArgs e)
{
var img = pbMIDI.Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
{
pbMIDI.Image = null;
}
}
void drag1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Move;
}
}
void drop1_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
drop1.Image = bmp;
}
在pictureScreen()中调用的mouseEvents:
public pictureScreen()
{
InitializeComponent();
mouseEvents();
}