使用拖放鼠标将粘贴图片从图片框(USERCONTROL内部)复制到Windows Explorer文件夹



我在用户控件中有此图片框

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 Explorer,但不适用于图片框

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);
}

这是带有用户控制的URL

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

好吧,您将图像加载到图片框:

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);
}

仅此而已!此代码允许将图像从图片框拖放到Explorer的文件夹。

最新更新