我在用户控件中有这个图片框
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事件: /**************我在列表视图中使用此代码将文件从列表视图复制粘贴到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);
}下面是带有用户控件的url。
http://www.authorcode.com/image-gallery-in-your-windows-applciation-in-c/
发布于 2017-04-26 00:31:31
好的,你把图片加载到图片框:
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拖放到资源管理器的文件夹中。
https://stackoverflow.com/questions/43597993
复制相似问题