Skip to content

Commit 53fd7bc

Browse files
committed
feat(resource): Add embedded icon resources and refactor icon loading logic
Add logo.ico as an embedded resource and refactor the InitializeNotifyIcon method into a more robust LoadIcon method. The new method attempts to load icons from multiple sources: 1. Resource files in the file system 2. Embedded resources 3. Executable file association icon
1 parent b8ccd45 commit 53fd7bc

2 files changed

Lines changed: 74 additions & 31 deletions

File tree

DevTools.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<Resource Include="Resources\Images\logo.png" />
3434
</ItemGroup>
3535

36+
<ItemGroup>
37+
<EmbeddedResource Include="Resources\Images\logo.ico">
38+
<LogicalName>DevTools.Resources.Images.logo.ico</LogicalName>
39+
</EmbeddedResource>
40+
</ItemGroup>
41+
3642
<ItemGroup>
3743
<None Include="Resources\Images\logo.ico">
3844
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

MainWindow.xaml.cs

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Configuration;
33
using System.Drawing;
4+
using System.IO;
5+
using System.Reflection;
46
using System.Text;
57
using System.Windows;
68
using System.Windows.Controls;
@@ -75,38 +77,14 @@ private void SaveSettings()
7577

7678
private void InitializeNotifyIcon()
7779
{
78-
try
79-
{
80-
var iconPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources", "Images", "logo.ico");
81-
if (System.IO.File.Exists(iconPath))
82-
{
83-
var icon = new System.Drawing.Icon(iconPath);
84-
_notifyIcon = new NotifyIcon
85-
{
86-
Icon = icon,
87-
Text = Strings.Toolbox,
88-
Visible = false
89-
};
90-
}
91-
else
92-
{
93-
_notifyIcon = new NotifyIcon
94-
{
95-
Icon = System.Drawing.SystemIcons.Application,
96-
Text = Strings.Toolbox,
97-
Visible = false
98-
};
99-
}
100-
}
101-
catch
80+
Icon? icon = LoadIcon();
81+
82+
_notifyIcon = new NotifyIcon
10283
{
103-
_notifyIcon = new NotifyIcon
104-
{
105-
Icon = System.Drawing.SystemIcons.Application,
106-
Text = Strings.Toolbox,
107-
Visible = false
108-
};
109-
}
84+
Icon = icon ?? SystemIcons.Application,
85+
Text = Strings.Toolbox,
86+
Visible = false
87+
};
11088

11189
_notifyIcon.MouseClick += (sender, e) =>
11290
{
@@ -126,6 +104,65 @@ private void InitializeNotifyIcon()
126104
_notifyIcon.ContextMenuStrip = contextMenu;
127105
}
128106

107+
private Icon? LoadIcon()
108+
{
109+
Icon? icon = null;
110+
111+
try
112+
{
113+
var iconPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "Images", "logo.ico");
114+
if (System.IO.File.Exists(iconPath))
115+
{
116+
icon = new Icon(iconPath);
117+
}
118+
}
119+
catch
120+
{
121+
}
122+
123+
if (icon == null)
124+
{
125+
try
126+
{
127+
var assembly = Assembly.GetExecutingAssembly();
128+
var resourceNames = assembly.GetManifestResourceNames();
129+
130+
foreach (var name in resourceNames)
131+
{
132+
if (name.EndsWith("logo.ico", StringComparison.OrdinalIgnoreCase))
133+
{
134+
using var stream = assembly.GetManifestResourceStream(name);
135+
if (stream != null)
136+
{
137+
icon = new Icon(stream);
138+
break;
139+
}
140+
}
141+
}
142+
}
143+
catch
144+
{
145+
}
146+
}
147+
148+
if (icon == null)
149+
{
150+
try
151+
{
152+
var exePath = Assembly.GetExecutingAssembly().Location;
153+
if (!string.IsNullOrEmpty(exePath) && System.IO.File.Exists(exePath))
154+
{
155+
icon = System.Drawing.Icon.ExtractAssociatedIcon(exePath);
156+
}
157+
}
158+
catch
159+
{
160+
}
161+
}
162+
163+
return icon;
164+
}
165+
129166
private void ShowWindow()
130167
{
131168
Show();

0 commit comments

Comments
 (0)