使用拖放鼠标将图片框(在usercontrol内)粘贴到Windows资源管理器文件夹中

时间:2017-04-24 21:26:16

标签: c#

我在用户控件中有这个图片框

private void DrawPictureBox(string _filename, string _displayname)
{
    PictureBox Pic1 = new PictureBox();
    AllowDrop = true;

    Pic1.Location = new System.Drawing.Point(XLocation, YLocation);
    XLocation = XLocation + PicWidth + 20;
    if (XLocation + PicWidth >= CtrlWidth)
    {
        XLocation = 25;
        YLocation = YLocation + PicHeight + 20;
    }
    Pic1.Name = "PictureBox" + i;
    i += 1;

    Pic1.Size = new System.Drawing.Size(PicWidth, PicHeight);
    Pic1.TabIndex = 0;
    Pic1.TabStop = false;
    Pic1.BorderStyle = BorderStyle.Fixed3D;
    //  this.toolTip1.SetToolTip(Pic1, _displayname);
    Pic1.MouseEnter += Pic1_MouseEnter;
    Pic1.MouseLeave += Pic1_MouseLeave;
    Pic1.Click += Pic1_Click;

    Pic1.DragOver += Pic1_DragOver;

    this.Controls.Add(Pic1);
    Pic1.Image = Image.FromFile(_filename);
    Pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    Pic1.Image.Tag = _filename;

    if (i > 2) { return; }
}

然后我在这里有DragOver事件: / **************我在listview中使用此代码将listview中的文件粘贴到Windows资源管理器,但不能使用图片框

private void Pic1_DragOver(object sender, DragEventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    // Get new position of picture

    // Determine whether file data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.

    if (!e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effect = DragDropEffects.None;
        return;
    }

    if ((e.KeyState & SHIFT) == SHIFT &&
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        e.Effect = DragDropEffects.Move;
    }
    else if ((e.KeyState & CTRL) == CTRL &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        e.Effect = DragDropEffects.Copy;
    }

    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;

        // Implement the rather strange behaviour of explorer that if the disk
        // is different, then default to a COPY operation
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files.Length > 0 && !files[0].ToUpper().StartsWith(homeDisk) &&  // Probably better ways to do this
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
            e.Effect = DragDropEffects.Copy;
    }
    else
        e.Effect = DragDropEffects.None;

    // This is an example of how to get the item under the mouse

    Point p = new Point(Convert.ToInt32(e.X), Convert.ToInt32(e.Y));

    //Point pt = Listview1.PointToClient(new Point(e.X, e.Y));
    //ListViewItem itemUnder = Listview1.GetItemAt(pt.X, pt.Y);
    //Point pt = pb.PointToClient(new Point(e.X, e.Y));
    // PictureBox itemUnder = pb.GetChildAtPoint(Convert.ToInt32(pt.X)), pt.Y);

    pb.Top += e.Y - YLocation;
    pb.Left += e.X - XLocation;
    pb.BringToFront();

    //Point newRel = Pic1.Location; //258, 109
    //Point pt = Pic1_DragOver.Location(new Point(e.X, e.Y));
    //ListViewItem itemUnder = Listview1.GetItemAt(pt.X, pt.Y);
    // Point mouseDownLocation = new Point(e.X, e.Y);
}

这是带有用户控件的网址

http://www.authorcode.com/image-gallery-in-your-windows-applciation-in-c/

1 个答案:

答案 0 :(得分:0)

好的,你将图片加载到图片框:

pictureBox.Image = Image.FromFile(_filename);

将图片框订阅到活动:

pictureBox.MouseDown += PictureBox_MouseDown;

处理此事件的代码:

private void PictureBox_MouseDown(object sender, MouseEventArgs e)
{
    var info = new FileInfo(_filename);
    string[] paths = { info.FullName };
    var pictureBox = (PictureBox)sender;
    pictureBox.DoDragDrop(new DataObject(DataFormats.FileDrop, paths), DragDropEffects.Copy);
}

这就是全部!此代码允许将图像从picturebox拖放到Explorer的文件夹中。