如何使图像拖放以重新排序?

时间:2016-06-29 11:00:04

标签: c# .net winforms user-interface

我正在构建一个应用程序,我希望用户能够以两列的形式重新排序图片中的图片。我有一个设置宽度的flowLayoutPanel,并且通过OpenFileDialog添加图片并缩放到流布局面板的宽度的一半(减去滚动条的余量)。

这就是我被困的地方 - 我已经尝试将图片添加为标签,按钮和现在的PictureBoxes,我无法解决如何实际移动它们的问题。我放弃了标签因为CanSelect是假的 - 虽然我不知道这是否会产生影响 - 我从按钮开始,因为我意识到图片框已经存在。我打开以切换出我使用的控件,但图像总是需要在两列中。

这是我目前为DragEnter和DragDrop事件提供的代码:

private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}
private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
    MessageBox.Show("dropped");
}

我该如何实现?我应该使用哪些控件以及我应该考虑哪些属性来实现这一目标?

1 个答案:

答案 0 :(得分:1)

感谢@ TaW的评论我现在知道你必须在拖动任何内容时向MouseDown事件添加DoDragDrop调用。我现在正在使用的代码如下(主要归功于this教程):

private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
    PictureBox picture = (PictureBox)e.Data.GetData(typeof(PictureBox));    
    FlowLayoutPanel _source = (FlowLayoutPanel)picture.Parent;
    FlowLayoutPanel _destination = (FlowLayoutPanel)sender;

    if (_source != _destination)
    {
        //where did you even get this from?
    }
    else
    {
        Point p = _destination.PointToClient(new Point(e.X, e.Y));
        var item = _destination.GetChildAtPoint(p);
        int index = _destination.Controls.GetChildIndex(item, false);
        _destination.Controls.SetChildIndex(picture, index);
        _destination.Invalidate();

    }
}

private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}

void p_MouseDown(object sender, MouseEventArgs e)
{
    PictureBox p = (PictureBox)sender;
    p.DoDragDrop(p, DragDropEffects.All);
}
相关问题