实现拖动文件到窗体(控件)
前言
实现从窗口外部拖文件到窗口内部并自动捕获文件地址。
第一步 开启属性
启用底层Window
的AllowDrop
属性,添加Drop
事件。
Drop
事件:当你拖动文件到对应控件后,松开触发。
除
Drop
事件外,我们还可以使用DragEnter
、DragOver
、DragLeave
三个事件。
第二步 事件代码
private void MainWindow_Drop(object sender, DragEventArgs e) { string msg = "Drop"; if (e.Data.GetDataPresent(DataFormats.FileDrop)) { msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); } MessageBox.Show(msg); }
实例1:
在WPF中的实现和WinForm中的实现99%相似,将要实现接受拖拽释放的控件添加DragEnter事件和Drop事件,本例中控件Grid grid作为接受控件,添加事件操作如下:
private void grid_Drop(object sender, DragEventArgs e) { string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); //获得文件名后的操作... } private void grid_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effects = DragDropEffects.Link; //WinForm中为e.Effect = DragDropEffects.Link else e.Effects = DragDropEffects.None; //WinFrom中为e.Effect = DragDropEffects.None }
实例2:
<Window x:Class="GetPixelFormat.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="获取图片的像素格式" Height="450" Width="565"> <Grid DragEnter="Grid_DragEnter" Drop="Grid_Drop" Background="White" > <Label Content="获取图片的PixelFormat(1.0支持单个文件拖入)" HorizontalAlignment="Left" Margin="155,19,0,0" VerticalAlignment="Top"/> <Label Content="文件名:" HorizontalAlignment="Left" Margin="109,68,0,0" VerticalAlignment="Top"/> <Label Name="lblFileName" Content="Label" HorizontalAlignment="Left" Margin="173,68,0,0" VerticalAlignment="Top" FontFamily="Microsoft YaHei"/> <Label Content="PixelFormat:" HorizontalAlignment="Left" Margin="87,129,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.697,0.627"/> <Label Name="lblPixedFormat" Content="Label" HorizontalAlignment="Left" Margin="173,129,0,0" VerticalAlignment="Top" FontFamily="Microsoft YaHei"/> <Label Content="图片对象:" HorizontalAlignment="Left" Margin="97,200,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.016,-0.604"/> <Border BorderBrush="#FFD4FBFD" BorderThickness="1" HorizontalAlignment="Left" Margin="181,200,0,0" VerticalAlignment="Top" > <Image Name="imgOne" HorizontalAlignment="Left" VerticalAlignment="Top" /> </Border> </Grid> </Window>
/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //启用接收 拖放 this.AllowDrop = true; } private void Grid_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effects = DragDropEffects.Link; else { e.Effects = DragDropEffects.None; lblFileName.Foreground = System.Windows.Media.Brushes.Red; lblFileName.Content = "拖入的内容不是文件"; } } private void Grid_Drop(object sender, DragEventArgs e) { System.Array array = e.Data.GetData(DataFormats.FileDrop) as Array; string fileName = array.GetValue(0).ToString(); lblFileName.Content = fileName; //获取图片 System.Drawing.Bitmap bit = new Bitmap(fileName); //获取图片的格式 lblPixedFormat.Content = bit.PixelFormat; //显示图片 IntPtr hBitmap = bit.GetHbitmap(); ImageSource imgS = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() ); imgOne.Source = imgS; } }
显示结果:
更多参考: