From 44de1cb9826f6ca84a187d7378e0dbaa0a8a0263 Mon Sep 17 00:00:00 2001
From: "Mr.Xia" <1424473282@qq.com>
Date: Wed, 17 Dec 2025 19:39:57 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=AA=97=E5=8F=A3=E7=BD=AE?=
=?UTF-8?q?=E9=A1=B6=E5=8A=9F=E8=83=BD=E5=B9=B6=E4=BC=98=E5=8C=96sort.txt?=
=?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=B9=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
新增窗口“置顶/取消置顶”按钮,支持动态切换窗口置顶状态,按钮文本随状态变化自动更新。优化 sort.txt 文件读取,改用 cmd type 命令读取并按行分割,提升编码兼容性,避免因编码问题导致读取异常。
---
MainWindow.xaml | 4 ++++
MainWindow.xaml.cs | 12 ++++++++++
MainWindowViewModel.cs | 53 ++++++++++++++++++++++++++++++++++++++++--
3 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/MainWindow.xaml b/MainWindow.xaml
index 756db7d..8159381 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -42,6 +42,10 @@
Click="ApplyOrderButton_Click"
Content="应用并保存"
IsEnabled="{Binding HasOriginalFile}" />
+
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 8240b24..4e95147 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -19,6 +19,9 @@ namespace DrillTools
var viewModel = new MainWindowViewModel();
DataContext = viewModel;
InitializeDragDrop();
+
+ // 设置默认置顶状态
+ this.Topmost = viewModel.IsTopmost;
}
///
@@ -141,6 +144,15 @@ namespace DrillTools
ViewModel.MoveSelectedToolDown();
}
+ ///
+ /// 置顶按钮点击事件
+ ///
+ private void ToggleTopmostButton_Click(object sender, RoutedEventArgs e)
+ {
+ ViewModel.ToggleTopmost();
+ this.Topmost = ViewModel.IsTopmost;
+ }
+
///
/// 刀具列表双击事件
///
diff --git a/MainWindowViewModel.cs b/MainWindowViewModel.cs
index dad54eb..b37674a 100644
--- a/MainWindowViewModel.cs
+++ b/MainWindowViewModel.cs
@@ -25,6 +25,7 @@ namespace DrillTools
private bool _canMoveUp;
private bool _canMoveDown;
private string _originalFilePath = string.Empty;
+ private bool _isTopmost = true; // 默认置顶
///
/// 刀具列表
@@ -107,6 +108,27 @@ namespace DrillTools
///
public bool HasOriginalFile => !string.IsNullOrEmpty(OriginalFilePath);
+ ///
+ /// 窗口是否置顶
+ ///
+ public bool IsTopmost
+ {
+ get => _isTopmost;
+ set
+ {
+ if (SetProperty(ref _isTopmost, value))
+ {
+ // 当IsTopmost改变时,通知TopmostButtonText属性也已更改
+ OnPropertyChanged(nameof(TopmostButtonText));
+ }
+ }
+ }
+
+ ///
+ /// 置顶按钮显示文本
+ ///
+ public string TopmostButtonText => IsTopmost ? "取消置顶" : "窗口置顶";
+
///
/// 从钻带内容加载刀具信息
///
@@ -1223,7 +1245,7 @@ M30";
try
{
- // 使用ANSI编码读取文件
+ // 使用ANSI编码
Encoding ansiEncoding;
try
{
@@ -1234,7 +1256,26 @@ M30";
ansiEncoding = Encoding.Default; // 如果获取失败,使用系统默认编码
}
- string[] lines = File.ReadAllLines(sortFilePath, ansiEncoding);
+ // 使用 cmd 命令读取-sort.txt文件,参考钻带文件读取方法
+ var process = new Process
+ {
+ StartInfo = new ProcessStartInfo
+ {
+ FileName = "cmd.exe",
+ Arguments = $"/c type \"{sortFilePath}\"",
+ RedirectStandardOutput = true,
+ UseShellExecute = false,
+ CreateNoWindow = true,
+ StandardOutputEncoding = ansiEncoding
+ }
+ };
+
+ process.Start();
+ string fileContent = process.StandardOutput.ReadToEnd();
+ process.WaitForExit();
+
+ // 按行分割内容
+ string[] lines = fileContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
@@ -1293,5 +1334,13 @@ M30";
tools.Add(tool);
}
}
+
+ ///
+ /// 切换窗口置顶状态
+ ///
+ public void ToggleTopmost()
+ {
+ IsTopmost = !IsTopmost;
+ }
}
}
\ No newline at end of file