如何从一个ListView(与ImageView的一起),以pictureboxes拖动图像? [英] How to drag images from a listview (together with an imageview) to pictureboxes?

查看:647
本文介绍了如何从一个ListView(与ImageView的一起),以pictureboxes拖动图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的C#和奋斗了很多小的项目,我想做的事情。

I am pretty new to C# and struggle a lot with a small project I want to do.

我忙着有点像拼贴制造商,我对左侧图片列表,我想拖放多个图像右手边,我希望能够将它们四处移动打造我的拼贴画。

I am busy with something like a collage maker where I have a list of pictures on the left hand side and I want to drag and drop multiple images to the right hand side where I want to be able to move them around to create my collage.

我想表现的形象,但我不允许小于10声誉点后的图像。但看看这里的图片

I wanted to show an image but I am not allowed to post images with less than 10 reputation points. But look here for the image:

在这里输入的形象描述

我不能设法得到它的工作。我看了网上的帮助,但我真的不能找到我要找的。我找到的东西都太不清楚,我努力去理解。

I can't manage to get it to work. I've looked online for help but I can't really find what I'm looking for. The stuff I do find are too unclear and I struggle to understand.

这是我迄今为止为code拖动从左至右下降,但它不工作;

This is what I have so far for the code to drag and drop from the left to the right but it doesn't work;

private void pictureBox1_DragEnter(object sender, DragEventArgs e)
    {
        int len = e.Data.GetFormats().Length - 1;
        int i;
        for (i = 0; i <= len; i++)
        {
            if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
            {
                //The data from the drag source is moved to the target.
                e.Effect = DragDropEffects.Move;
            }
        }
    }

    private void pictureBox1_DragDrop(object sender, DragEventArgs e)
    {
        //Return if the items are not selected in the ListView control.
        if (listView1.SelectedItems.Count == 0)
        {
            return;
        }
        ListViewItem dragitem = listView1.SelectedItems[0];
        pictureBox2.Image = imageList1.Images[dragitem.ImageIndex];
        listView1.Items.Remove(dragitem);
    }

    private void listView1_MouseDown(object sender, MouseEventArgs e)
    {
        listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
    }

后,我可以添加图片到左边​​,我怎么可以将使用鼠标坐标移动它们身边?

And after I can add image to the left, how can I drag and move them around using the mouse coordinates?

任何帮助将AP preciated请。
一切都在使用C#在Windows窗体中完成的。

Any help will be appreciated please. Everything is done using C# in Windows Forms.

推荐答案

下面是一个完整的例子,你可能想一起玩:

Here is a full example you may want to play with:

它使用了的ListView ,一个的ImageList 面板持有 PictureBoxes

It uses a form with a ListView, an ImageList and a Panel to hold the PictureBoxes.

您可以使用 FlowLayoutPanel的,但这不会让您移动 Pictureboxes 之后。

You could use a FlowLayoutPanel but this won't let you move the Pictureboxes later.

您也可以使用面板的BackgroundImage

首先,我们建立如下形式:

First we set up the form:

private void Form1_Load(object sender, EventArgs e)
{
    KeyPreview = true;
    FillLV(10);
    AddPBs(36);
    listView1.MouseMove += listView1_MouseMove;
}

void FillLV(int count)
{
    for (int i = 0; i < count; i++) listView1.Items.Add("Item" + i, i);
}

void AddPBs(int count)
{
    int iWidth = imageList1.ImageSize.Width;
    int iHeight = imageList1.ImageSize.Height;
    int cols = panel1.ClientSize.Width / iWidth;

    for (int i = 0; i < count; i++)
    {
        PictureBox PB = new PictureBox();
        PB.BackColor = Color.LightGray;
        PB.Margin = new System.Windows.Forms.Padding(0);
        PB.ClientSize = new System.Drawing.Size(iWidth , iHeight );
        PB.Location = new Point(iWidth * (i % cols), iHeight * (i / cols));
        PB.Parent = panel1;
        PB.DragEnter += PB_DragEnter;
        PB.DragDrop += PB_DragDrop;
        PB.AllowDrop = true;
        PB.MouseClick += (ss, ee) => { currentPB = PB; PB.Focus(); };
    }
}

请注意,我们如何事件添加到动态创建的 PictureBoxes ,以及我们如何设置他们隐藏的的AllowDrop 属性。

Note how we add events to the dynamically created PictureBoxes and how we set their hidden AllowDrop property.

另外要注意的小拉姆达使鼠标点击 prepare与键盘周围移动它们。

Also note the little lambda to make the MouseClick prepare for moving them around with the keyboard.

有关这一点,我们还需要将当前框的引用:

For this we will also need a reference to the current box:

PictureBox currentPB = null;

现在我们开始拖动。这应该的不可以中完成的的MouseDown ,否则它将与正常选择干扰,也发生了新的选择作出之前..

Now we start the dragging. This should not be done in the MouseDown or else it will interfere with normal selection and also happen before the new selection is made..

你这样做在的MouseMove

void listView1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            listView1.DoDragDrop(listView1.SelectedItems[0], DragDropEffects.Move);
        }
    }
}

更新)为列表视图更好,更简单的(感谢汉斯!)在他们的 ItemDrag 事件:

Or (Update) for ListViews better and much simpler (thanks Hans!) in their ItemDrag event:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
    listView1.DoDragDrop(e.Item, DragDropEffects.Move);
}

更新:这两种方式现在用拖动的项目,而不仅仅是它imageIndex指定,以更简单地从LV删除项目。

Update: Both ways now use the dragged Item, not just its imageIndex, to make it simpler to remove the Item from the LV..

void PB_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(ListViewItem))) 
        e.Effect = DragDropEffects.Move;
    else
        e.Effect = DragDropEffects.None;
}

void PB_DragDrop(object sender, DragEventArgs e)
{
    PictureBox pb = sender as PictureBox;
    var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
    int index = item.ImageIndex;
    pb.Image = imageList1.Images[index];  // make sure you have images for indices!!

}

最后,我们使用键盘来移动当前框。我已经加入code把它向上和向下的z顺序。

Finally we use the keyboard to move the current box around. I have added code to bring it up and down in the z-order as well.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (currentPB == null) return;
    if (e.KeyData == Keys.Left) currentPB.Left -= 1;
    else if (e.KeyData == Keys.Right) currentPB.Left += 1;
    else if (e.KeyData == Keys.Up) currentPB.Top -= 1;
    else if (e.KeyData == Keys.Down) currentPB.Top += 1;
    else
    { 
        int z = panel1.Controls.GetChildIndex(currentPB);
        if (e.KeyData == Keys.Home) panel1.Controls.SetChildIndex(currentPB, 0);
        else if (e.KeyData == Keys.End) 
             panel1.Controls.SetChildIndex(currentPB, panel1.Controls.Count);
        else if (e.KeyData == Keys.PageUp) 
             panel1.Controls.SetChildIndex(currentPB, z + 1);
        else if (e.KeyData == Keys.PageDown) 
             { if (z > 0) panel1.Controls.SetChildIndex(currentPB, z - 1); }
    }
    panel1.Invalidate();

}

对于这项工作的形式必须有:键preVIEW = TRUE;

请注意,在一个尺寸的ImageList 被限制为256×256。所以,你可能希望只使用索引,并用它来从磁盘加载较大的图像。

Note that the sizes in an ImageList are restricted to 256x256. So you may want to use only the index and use it to load larger images from disk..

这篇关于如何从一个ListView(与ImageView的一起),以pictureboxes拖动图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆