日期:2014-05-20  浏览次数:20971 次

求拖拽打开文件的C#代码
一个Win   Form上有一个PictureBox,
从资源管理器里把一个图片文件拖拽到这个Form上,PictureBox就显示这个图片。
请问用C#如何实现,最好有详细代码,谢谢!

------解决方案--------------------
http://www.mscourse.com/blog/u/Kayetyusha/archives/2007/160.html
------解决方案--------------------
给PictureBox添加如下的两个事件代码试试看,应该没有问题:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.pictureBox1.AllowDrop = true;//这句话直接写.
}
private void pictureBox1_DragOver(object sender, DragEventArgs e)
{
if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
{
e.Effect = DragDropEffects.Link;
}
}

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string[] items = (object)e.Data.GetData( "FileNameW ") as string[];
if (items.Length == 1)
{
Image img = Image.FromFile(items[0]);
this.pictureBox1.Image = img;
}
}