using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace DragDropTextFilesApp
{
    public partial class MainWindow : Window
    {
        private readonly SolidColorBrush NormalBorderBrush = new SolidColorBrush(Color.FromRgb(153, 153, 153)); // #999999
        private readonly SolidColorBrush HighlightBorderBrush = new SolidColorBrush(Color.FromRgb(0, 122, 204)); // 蓝色高亮

        public MainWindow()
        {
            InitializeComponent();
            StatusText.Text = "请将包含文本文档的文件夹拖放到上方区域。";
            DropArea.BorderBrush = NormalBorderBrush;
        }

        private void DropArea_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                if (paths.Length > 0 && Directory.Exists(paths[0]))
                {
                    e.Effects = DragDropEffects.Copy;
                    AnimateBorderColor(DropArea, HighlightBorderBrush);
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                    AnimateBorderColor(DropArea, NormalBorderBrush);
                }
            }
            else
            {
                e.Effects = DragDropEffects.None;
                AnimateBorderColor(DropArea, NormalBorderBrush);
            }
            e.Handled = true;
        }

        private void DropArea_Drop(object sender, DragEventArgs e)
        {
            AnimateBorderColor(DropArea, NormalBorderBrush);
            StatusText.Text = "正在处理文件...";
            ContentRichTextBox.Visibility = Visibility.Collapsed;

            try
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
                {
                    string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                    if (paths.Length > 0 && Directory.Exists(paths[0]))
                    {
                        string folderPath = paths[0];
                        // 获取所有的 .txt 文件（不包括子文件夹）
                        string[] txtFiles = Directory.GetFiles(folderPath, "*.txt", SearchOption.TopDirectoryOnly);

                        if (txtFiles.Length == 0)
                        {
                            MessageBox.Show("所选文件夹中没有找到 .txt 文档。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                            StatusText.Text = "未找到 .txt 文件。";
                            return;
                        }

                        StringBuilder sb = new StringBuilder();
                        foreach (var file in txtFiles.OrderBy(f => f))
                        {
                            sb.AppendLine($"--- 文件: {System.IO.Path.GetFileName(file)} ---");
                            sb.AppendLine(File.ReadAllText(file));
                            sb.AppendLine();
                        }

                        // 使用 RichTextBox 显示内容
                        ContentRichTextBox.Document.Blocks.Clear();
                        ContentRichTextBox.Document.Blocks.Add(new Paragraph(new Run(sb.ToString())));
                        ContentRichTextBox.Visibility = Visibility.Visible;

                        StatusText.Text = $"成功加载 {txtFiles.Length} 个文件。";
                    }
                    else
                    {
                        MessageBox.Show("请拖放一个有效的文件夹。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        StatusText.Text = "无效的拖放内容。";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"处理文件时发生错误:\n{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                StatusText.Text = "发生错误。";
            }
        }

        /// <summary>
        /// 动画切换边框颜色
        /// </summary>
        /// <param name="border">目标边框</param>
        /// <param name="toColor">目标颜色</param>
        private void AnimateBorderColor(Border border, SolidColorBrush toColor)
        {
            ColorAnimation colorAnimation = new ColorAnimation
            {
                To = toColor.Color,
                Duration = TimeSpan.FromMilliseconds(200)
            };
            border.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
        }
    }
}<Window x:Class="DragDropTextFilesApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DragDropTextFilesApp"
        Title="文本文件合并器" Height="600" Width="900"
        Background="#1E1E1E" <!-- 深色背景 -->
    WindowStartupLocation="CenterScreen"
        AllowsTransparency="False"
        ResizeMode="CanResize">
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <!-- 拖放区域 -->
            <RowDefinition Height="3*" />
            <!-- 显示内容区域 -->
            <RowDefinition Height="7*" />
            <!-- 状态栏 -->
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- 拖放区域及选择文件夹按钮 -->
        <Border x:Name="DropArea"
                Grid.Row="0"
                BorderBrush="#4D4D4D" <!-- 边框颜色 -->
            BorderThickness="2"
                CornerRadius="10"
                Background="#2D2D30"
            <!-- 拖放区域背景 -->
            AllowDrop="True"
                DragOver="DropArea_DragOver"
                Drop="DropArea_Drop"
                Padding="20"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">
            <Grid>
                <TextBlock Text="将包含文本文档的文件夹或多个文本文件拖放到此区域"
                           FontSize="18"
                           Foreground="White"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           TextWrapping="Wrap"
                           TextAlignment="Center"/>
            </Grid>
        </Border>

        <!-- 显示内容区域 -->
        <RichTextBox x:Name="ContentRichTextBox"
                     Grid.Row="1"
                     Margin="0,20,0,20"
                     VerticalScrollBarVisibility="Auto"
                     HorizontalScrollBarVisibility="Auto"
                     IsReadOnly="True"
                     FontFamily="Consolas"
                     FontSize="14"
                     Background="#1E1E1E" <!-- RichTextBox 背景 -->
            Foreground="White"
            <!-- RichTextBox 字体颜色 -->
            SelectionBrush="LightBlue"
            <!-- 选择文本时的背景色 -->
            Visibility="Collapsed">
        </RichTextBox>

        <!-- 状态栏 -->
        <StatusBar Grid.Row="2" Background="#2D2D30">
            <StatusBarItem>
                <TextBlock x:Name="StatusText" Text="准备就绪" Foreground="White" />
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window> NET 4.8 FRAM WORK 现在实现了拖入文件夹 但是不能拖入文本文件 我想的是 可以一次性拖入鼠标选中的不同文件夹 文本文件 然后再UI 顺序保留内容 并且可以复制他们