WPF窗体代码
<Window x:Class="SerialLabelDemo.Test.Window10" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SerialLabelDemo.Test" mc:Ignorable="d" AllowDrop="True" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="35"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" Background="#CC0088FF"> <Button Name="btClear" Click="ClickClear" Content="Clear" Margin="5" /> <Border BorderBrush="Black" BorderThickness="1" MaxHeight="25"> <CheckBox Name="cbWrap" Content="Wrap Content" IsChecked="False" Margin="5" Padding="5,0,0,0" VerticalAlignment="Center" VerticalContentAlignment="Center" Click="ClickWrap" /> </Border> <Label Name="lblInstructions" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Content=" Drop a single file below to display its contents. " /> </StackPanel> <TextBox Name="tbDisplayFileContents" Grid.Row="1" AcceptsReturn="True" AcceptsTab="True" AllowDrop="True" BorderThickness="1" BorderBrush="Black" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PreviewDragOver="EhDragOver" PreviewDrop="EhDrop"/> <TextBlock Text="管理员运行是不可以进行拖拽的"/> </Grid> </Window>
窗体后台代码
public partial class Window10 : Window { public Window10() { InitializeComponent(); if ((bool)cbWrap.IsChecked) tbDisplayFileContents.TextWrapping = TextWrapping.Wrap; else tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap; } private void ClickClear(object sender, RoutedEventArgs args) { tbDisplayFileContents.Clear(); } private void ClickWrap(object sender, RoutedEventArgs args) { if ((bool)cbWrap.IsChecked) tbDisplayFileContents.TextWrapping = TextWrapping.Wrap; else tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap; } private void EhDragOver(object sender, DragEventArgs args) { // As an arbitrary design decision, we only want to deal with a single file. args.Effects = IsSingleFile(args) != null ? DragDropEffects.Copy : DragDropEffects.None; // Mark the event as handled, so TextBox's native DragOver handler is not called. args.Handled = true; } private void EhDrop(object sender, DragEventArgs args) { // Mark the event as handled, so TextBox's native Drop handler is not called. args.Handled = true; var fileName = IsSingleFile(args); if (fileName == null) return; var fileToLoad = new StreamReader(fileName); tbDisplayFileContents.Text = fileToLoad.ReadToEnd(); fileToLoad.Close(); // Set the window title to the loaded file. Title = "File Loaded: " + fileName; } // If the data object in args is a single file, this method will return the filename. // Otherwise, it returns null. private string IsSingleFile(DragEventArgs args) { // Check for files in the hovering data object. if (args.Data.GetDataPresent(DataFormats.FileDrop, true)) { var fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[]; // Check fo a single file or folder. if (fileNames.Length == 1) { // Check for a file (a directory will return false). if (File.Exists(fileNames[0])) { // At this point we know there is a single file. return fileNames[0]; } } } return null; } }
运行画面:
调试时发现无法拖拽:
原因,以管理员启动VS进行调试是不能拖拽外部文件到程序中的
在debug目录下以管理员运行exe运行也是一样的结果
说是UAC检测的系统上管理员权限开启的程序时无法支持外部拖拽的