【发布时间】:2013-04-06 22:44:46
【问题描述】:
我正在使用 C# 在 Visual Studio 2012 中工作,我需要将一个图片框拖动到另一个图片框,基本上将目标图片框图像替换为拖动的图片框图像。
我该怎么做?
请具体说明,并尽量以最简单和最好的方式进行解释。 我对编程非常陌生,有点绝望,所以请耐心等待。
【问题讨论】:
标签: c# drag-and-drop picturebox
我正在使用 C# 在 Visual Studio 2012 中工作,我需要将一个图片框拖动到另一个图片框,基本上将目标图片框图像替换为拖动的图片框图像。
我该怎么做?
请具体说明,并尽量以最简单和最好的方式进行解释。 我对编程非常陌生,有点绝望,所以请耐心等待。
【问题讨论】:
标签: c# drag-and-drop picturebox
拖放在 PictureBox 控件上是隐藏的。不知道为什么,它工作得很好。这里可能的指导是,对于用户来说,您可以将图像放在控件上并不明显。您必须对此做一些事情,至少将 BackColor 属性设置为非默认值,以便用户可以看到它。
无论如何,您需要在第一个图片框上实现 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;
}
}
我假设您想移动图像,如果需要复制,则进行调整。然后你必须在第二个图片框上实现 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;
}
这确实允许您将图像从另一个应用程序拖到框中。让我们称之为功能。如果您想禁止这样做,请使用 bool 标志。
【讨论】:
汉斯的回答让我找到了正确的解决方案。该答案的问题在于将DoDragDrop 放入MouseDown 将阻止MouseClick 事件触发。
这是我的解决方案:
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);
}
}
}
我的GitHub 上的完整工作示例。
【讨论】:
您可以使用鼠标进入和离开事件轻松完成此操作。例如,您有两个图片框pictureBox1 和pictureBox2。你想把图片从图片框1拖到图片框2上,像这样。
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;
}
其中“a”只是一个锁或钥匙,用于检查鼠标是否已进入我们要放置此图像的控件。希望它有所帮助,对我有用。
【讨论】:
代码片段
Form1.AllowDrop = true;
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
int x = this.PointToClient(new Point(e.X, e.Y)).X;
int y = this.PointToClient(new Point(e.X, e.Y)).Y;
if(x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
pictureBox1.Image = Image.FromFile(files[0]);
}
}
【讨论】: