基本信息
源码名称:wpf拖拽效果
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2015-10-31
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ImageExplorer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private BitmapImage bmi;
private string path;
public MainWindow()
{
InitializeComponent();
path = Environment.CurrentDirectory @"\image\";
GetImage();
}
private void GetImage()
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
lstImage.Items.Add(file);
}
}
private void lstImage_Drop(object sender, DragEventArgs e)
{
//仅支持文件的拖放
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
{
return;
}
//获取拖拽的文件
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
//这里需要注意,由于程序既支持拖过来也支持拖过去,那么ListBox就也能接收自身拖拽过来的文件
//为了防止鼠标点击和拖拽的冲突,需要屏蔽从程序自身拖拽过来的文件
//这里判断文件是否从程序外部拖拽进来,也就是判断图片是否在工作目录下
if (files.Length > 0 && !files[0].StartsWith(path) &&
(e.AllowedEffects & DragDropEffects.Copy) == DragDropEffects.Copy)
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
foreach (string file in files)
{
try
{
//如果是从外部拖拽进来的图像,则复制该文件到工作目录下做备份
string destFile = path System.IO.Path.GetFileName(file);
switch (e.Effects)
{
case DragDropEffects.Copy:
File.Copy(file, destFile, false);
bmi = new BitmapImage(new Uri(destFile));
imgShow.Source = bmi;
lstImage.Items.Add(destFile);
break;
default:
break;
}
}
catch
{
MessageBox.Show("已存在此文件或导入了非图像文件!");
}
}
}
private void lstImage_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && e.RightButton == MouseButtonState.Released)
{
bmi = new BitmapImage(new Uri(lstImage.SelectedItem.ToString()));
imgShow.Source = bmi;
}
}
private void lstImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lstImage.SelectedIndex > -1)
{
//只使用了Listbox单选功能
string[] files = new string[1];
files[0] = lstImage.SelectedItem.ToString();
DragDrop.DoDragDrop(lstImage, new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy | DragDropEffects.Move /* | DragDropEffects.Link */);
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ImageExplorer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private BitmapImage bmi;
private string path;
public MainWindow()
{
InitializeComponent();
path = Environment.CurrentDirectory @"\image\";
GetImage();
}
private void GetImage()
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
lstImage.Items.Add(file);
}
}
private void lstImage_Drop(object sender, DragEventArgs e)
{
//仅支持文件的拖放
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
{
return;
}
//获取拖拽的文件
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
//这里需要注意,由于程序既支持拖过来也支持拖过去,那么ListBox就也能接收自身拖拽过来的文件
//为了防止鼠标点击和拖拽的冲突,需要屏蔽从程序自身拖拽过来的文件
//这里判断文件是否从程序外部拖拽进来,也就是判断图片是否在工作目录下
if (files.Length > 0 && !files[0].StartsWith(path) &&
(e.AllowedEffects & DragDropEffects.Copy) == DragDropEffects.Copy)
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
foreach (string file in files)
{
try
{
//如果是从外部拖拽进来的图像,则复制该文件到工作目录下做备份
string destFile = path System.IO.Path.GetFileName(file);
switch (e.Effects)
{
case DragDropEffects.Copy:
File.Copy(file, destFile, false);
bmi = new BitmapImage(new Uri(destFile));
imgShow.Source = bmi;
lstImage.Items.Add(destFile);
break;
default:
break;
}
}
catch
{
MessageBox.Show("已存在此文件或导入了非图像文件!");
}
}
}
private void lstImage_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && e.RightButton == MouseButtonState.Released)
{
bmi = new BitmapImage(new Uri(lstImage.SelectedItem.ToString()));
imgShow.Source = bmi;
}
}
private void lstImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lstImage.SelectedIndex > -1)
{
//只使用了Listbox单选功能
string[] files = new string[1];
files[0] = lstImage.SelectedItem.ToString();
DragDrop.DoDragDrop(lstImage, new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy | DragDropEffects.Move /* | DragDropEffects.Link */);
}
}
}
}