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检测的系统上管理员权限开启的程序时无法支持外部拖拽的

同行描述原因见https://www.cnblogs.com/swack/p/10508649.html

最新文章

  1. MySql LIKE 查找带反斜线“\”的记录
  2. springmvc 文件上传实现(不是服务器的)
  3. 【GO】GO语言学习笔记二
  4. python之路-Day8
  5. listView后面加控件,防止被挤
  6. 工作当中实际运用(1)——tab选项卡
  7. ycsb使用方法
  8. iPhone —— 如何自制铃声(图文)
  9. 【html】【10】div布局[div水平垂直居中]
  10. java String对象的创建(jvm).
  11. iOS详解MMDrawerController抽屉效果(一)
  12. logging格式
  13. Man方法
  14. BZOJ.5286.[AHOI/HNOI2018]转盘(线段树)
  15. docker 的使用
  16. 42-2017蓝桥杯b java
  17. C:\WINDOWS\system32\drivers\etc\hosts 文件的作用
  18. Postman+Newman+Jenkins 详细教程
  19. indexes和indices的区别
  20. 关于SIGPIPE信号

热门文章

  1. 题解-SDOI2013 淘金
  2. nginx介绍1
  3. CentOS7 扩容磁盘非根分区
  4. SQL注入 (一)
  5. 移动端 canvas基础1
  6. [日常摸鱼]poj1741Tree-点分治
  7. 使用gitlab-runner本地验证.gitlab-ci.yml
  8. Python——元组的基本语法(创建、访问、修改、删除)
  9. 【剑指offer】00 开撸剑指offer
  10. 【磁盘/文件系统】第二篇:标准磁盘分区流程针对fdisk(硬盘容量小于2T且分区数不能大于15个分区)