c# 窗体与窗体外的文件互动(拖拽)
大部分控件都有此事件drag相关。
以picturebox为例:
pictureBox1.AllowDrop = true;//此属性需要全打出来,不会自动识别添加
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
/// <summary>
/// 此事件需要配合DragEnter事件才能响应
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] path = (string[])e.Data.GetData(DataFormats.FileDrop);
if (System.IO.File.Exists(path[0]))
{
pictureBox1.Image = Image.FromFile(path[0]);
}
}
}

浙公网安备 33010602011771号