首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求拖拽打开文件的C#代码解决方法

2012-02-25 
求拖拽打开文件的C#代码一个WinForm上有一个PictureBox,从资源管理器里把一个图片文件拖拽到这个Form上,Pi

求拖拽打开文件的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;
}
}

热点排行