From 3a110353296a7514c61fbcf38d8330f7d7960272 Mon Sep 17 00:00:00 2001 From: YONGYE Date: Mon, 13 Oct 2025 13:38:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BE=E6=A0=87=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=B8=AE=E5=8A=A9=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWindow.xaml.cs | 12 ++++----- Utils/IconHelper.cs | 58 +++++++++++++++++++++++++++++++++++++++++++ Utils/WindowHelper.cs | 22 +++++++--------- 3 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 Utils/IconHelper.cs diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 2ce832d..d2c7c6f 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -85,32 +85,32 @@ public partial class MainWindow : Window private void OnFilePageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600,isResizable: false, "..\\..\\..\\src\\public\\Icons\\programIcons\\main1-File.ico"); + WindowHelper.ShowPageDialog(new FilePage(), "记录列表", 810, 600,isResizable: false, IconHelper.GetIconPath("main1-File.ico")); } private void OnConfigPageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new ConfigPage(), "配置", 972, 648, isResizable: false, "..\\..\\..\\src\\public\\Icons\\programIcons\\main2-Config.ico"); + WindowHelper.ShowPageDialog(new ConfigPage(), "配置", 972, 648, isResizable: false, IconHelper.GetIconPath("main2-Config.ico")); } private void OnStandardPageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new StandardPage(), "主标定", 972, 648, isResizable: true, "..\\..\\..\\src\\public\\Icons\\programIcons\\main3-Standard.ico"); + WindowHelper.ShowPageDialog(new StandardPage(), "主标定", 972, 648, isResizable: true, IconHelper.GetIconPath("main3-Standard.ico")); } private void OnGaugePageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new GaugePage(), "Gauge R&&R", 1080, 720, isResizable:false, "..\\..\\..\\src\\public\\Icons\\programIcons\\main4-Gauge.ico"); + WindowHelper.ShowPageDialog(new GaugePage(), "Gauge R&&R", 1080, 720, isResizable:false, IconHelper.GetIconPath("main4-Gauge.ico")); } private void OnCgCgkPageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new CgCgkPage(), "CgCgk", 972, 648, isResizable: true, "..\\..\\..\\src\\public\\Icons\\programIcons\\main5-CgCgk.ico"); + WindowHelper.ShowPageDialog(new CgCgkPage(), "CgCgk", 972, 648, isResizable: true, IconHelper.GetIconPath("main5-CgCgk.ico")); } private void OnSysSetPageButtonClick(object sender, RoutedEventArgs e) { - WindowHelper.ShowPageDialog(new SysSetPage(), "系统设置", 810, 600, isResizable:false, "..\\..\\..\\src\\public\\Icons\\programIcons\\main6-SysSet.ico"); + WindowHelper.ShowPageDialog(new SysSetPage(), "系统设置", 810, 600, isResizable:false, IconHelper.GetIconPath("main6-SysSet.ico")); } #endregion diff --git a/Utils/IconHelper.cs b/Utils/IconHelper.cs new file mode 100644 index 0000000..eed5cd0 --- /dev/null +++ b/Utils/IconHelper.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; + +namespace WpfApp.Utils +{ + public static class IconHelper + { + /// + /// 获取图标路径(智能搜索) + /// + /// 图标文件名,例如 "FMSDGAUGE-main.ico",为空使用默认图标 + /// 可选外部搜索目录 + /// 图标绝对路径 + public static string GetIconPath(string iconFileName = null, string[] externalDirs = null) + { + // 默认图标文件名 + string defaultIconName = "FMSDGAUGE-main.ico"; + iconFileName ??= defaultIconName; + + // 默认项目图标目录(相对于 EXE) + string projectIconDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\src\public\Icons"); + + // 先搜索项目目录 + string path = SearchDirectory(projectIconDir, iconFileName); + if (!string.IsNullOrEmpty(path)) + return path; + + // 再搜索外部目录 + if (externalDirs != null) + { + foreach (var dir in externalDirs) + { + path = SearchDirectory(dir, iconFileName); + if (!string.IsNullOrEmpty(path)) + return path; + } + } + + // 都没找到就抛异常 + throw new FileNotFoundException($"找不到图标文件: {iconFileName}"); + } + + /// + /// 在指定目录及子目录搜索文件 + /// + private static string SearchDirectory(string rootDir, string fileName) + { + if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir)) + return null; + + var files = Directory.GetFiles(rootDir, fileName, SearchOption.AllDirectories); + if (files.Length > 0) + return Path.GetFullPath(files[0]); + + return null; + } + } +} diff --git a/Utils/WindowHelper.cs b/Utils/WindowHelper.cs index 4bfa061..7c4dfd1 100644 --- a/Utils/WindowHelper.cs +++ b/Utils/WindowHelper.cs @@ -31,20 +31,16 @@ namespace WpfApp.Utils Owner = Application.Current.MainWindow, ResizeMode = isResizable ? ResizeMode.CanResize : ResizeMode.NoResize }; - string defaultIconPath = null; - if (iconPath == null) - defaultIconPath = @"..\..\..\src\public\Icons\programIcons\FMSDGAUGE-main.ico"; - else - defaultIconPath = iconPath; + string defaultIconPath = iconPath == null ? IconHelper.GetIconPath("FMSDGAUGE-main.ico") : iconPath; - try - { - navWin.Icon = new BitmapImage(new Uri(System.IO.Path.GetFullPath(defaultIconPath), UriKind.Absolute)); - } - catch (Exception ex) - { - Console.WriteLine($"设置图标失败: {ex.Message}"); - } + try + { + navWin.Icon = new BitmapImage(new Uri(System.IO.Path.GetFullPath(defaultIconPath), UriKind.Absolute)); + } + catch (Exception ex) + { + Console.WriteLine($"设置图标失败: {ex.Message}"); + } navWin.ShowDialog();