C# 从一个图片框拖放到另一个图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16004682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Drag and Drop from one Picture box into Another
提问by user2250165
I'm working in visual studio 2012 with C# and I need to Drag a Picture box into another picture box, basically replace the target Picturebox Image with the Dragged Picture box image.
我正在使用 C# 在 Visual Studio 2012 中工作,我需要将一个图片框拖动到另一个图片框中,基本上用拖动的图片框图像替换目标图片框图像。
How do I do this?
我该怎么做呢?
Please be specific and try to explain as simplest and as best as possible. I'm extremely new to programming, and a bit desperate so please be patient with me.
请具体说明,并尽量解释得最简单和最好。我对编程非常陌生,有点绝望,所以请耐心等待。
回答by Wajahat
You can use mouse enter and leave events to do this easily..For example you have two picture boxes pictureBox1 and pictureBox2... And you want to drag the image from picture box1 and drop it onto picture box2 do somthing like this...
您可以使用鼠标进入和离开事件轻松完成此操作..例如,您有两个图片框pictureBox1 和pictureBox2...并且您想将图片从图片box1 拖放到图片box2 上,请执行以下操作...
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (a == 1)
{
pictureBox1.Image = pictureBox2.Image;
a = 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
a = 1;
}
where 'a' is just a lock or key which checks whether the mouse has entered the control on which we want to drop this image on...hope it helped..worked for me
其中“a”只是一个锁或钥匙,用于检查鼠标是否已进入我们想要放置此图像的控件...希望它对我有帮助...对我有用
回答by Hans Passant
Drag+drop is hidden on the PictureBox control. Not sure why, it works just fine. The probable guidance here is that it will not be obvious to the user that you could drop an image on the control. You'll have to do something about that, at least set the BackColor property to a non-default value so the user can see it.
拖放操作隐藏在 PictureBox 控件上。不知道为什么,它工作得很好。此处可能的指导是,您可以在控件上放置图像对用户来说并不明显。您必须对此做一些事情,至少将 BackColor 属性设置为非默认值,以便用户可以看到它。
Anyhoo, you'll need to implement the MouseDown event on the first picturebox so you can click it and start dragging:
Anyhoo,您需要在第一个图片框上实现 MouseDown 事件,以便您可以单击它并开始拖动:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
var img = pictureBox1.Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) {
pictureBox1.Image = null;
}
}
I assumed you wanted to move the image, tweak if necessary if copying was intended. Then you'll have to implement the DragEnter and DragDrop events on the second picturebox. Since the properties are hidden, you should set them in the form's constructor. Like this:
我假设您想移动图像,如果需要进行复制,则在必要时进行调整。然后您必须在第二个图片框上实现 DragEnter 和 DragDrop 事件。由于属性是隐藏的,您应该在表单的构造函数中设置它们。像这样:
public Form1() {
InitializeComponent();
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox2.AllowDrop = true;
pictureBox2.DragEnter += pictureBox2_DragEnter;
pictureBox2.DragDrop += pictureBox2_DragDrop;
}
void pictureBox2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
void pictureBox2_DragDrop(object sender, DragEventArgs e) {
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
This does allow you to drag an image from another application into the box. Let's call it a feature. Use a bool flag if you want to disallow this.
这确实允许您将图像从另一个应用程序拖到框中。我们称之为功能。如果您想禁止这样做,请使用 bool 标志。
回答by javon27
Hans's answer led me to the correct solution. The problem with that answer is that putting DoDragDropinside MouseDownwill prevent MouseClickevents from firing.
汉斯的回答让我找到了正确的解决方案。该答案的问题在于,放入DoDragDrop内部MouseDown会阻止MouseClick事件触发。
Here's my solution:
这是我的解决方案:
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var pb = (PictureBox)sender;
if (pb.BackgroundImage != null)
{
pb.DoDragDrop(pb, DragDropEffects.Move);
}
}
}
private void PictureBox_DragEnter (object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void PictureBox_DragDrop (object sender, DragEventArgs e)
{
var target = (PictureBox)sender;
if (e.Data.GetDataPresent(typeof(PictureBox)))
{
var source = (PictureBox)e.Data.GetData(typeof(PictureBox));
if (source != target)
{
// You can swap the images out, replace the target image, etc.
SwapImages(source, target);
}
}
}
Full working example on my GitHub.
我的GitHub 上的完整工作示例。

