添加图标查询帮助类

This commit is contained in:
YONGYE 2025-10-13 13:38:02 +08:00
parent f990cea469
commit 3a11035329
3 changed files with 73 additions and 19 deletions

View File

@ -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

58
Utils/IconHelper.cs Normal file
View File

@ -0,0 +1,58 @@
using System;
using System.IO;
namespace WpfApp.Utils
{
public static class IconHelper
{
/// <summary>
/// 获取图标路径(智能搜索)
/// </summary>
/// <param name="iconFileName">图标文件名,例如 "FMSDGAUGE-main.ico",为空使用默认图标</param>
/// <param name="externalDirs">可选外部搜索目录</param>
/// <returns>图标绝对路径</returns>
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}");
}
/// <summary>
/// 在指定目录及子目录搜索文件
/// </summary>
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;
}
}
}

View File

@ -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();