Skip to content

Commit 1c85a01

Browse files
committed
now can use custom icon (place icon.ico into same folder as exe) fixes #127
1 parent f7f3903 commit 1c85a01

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

UnityLauncherPro/MainWindow.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</ControlTemplate>
3131
</Button.Template>
3232
</Button>
33-
<Image Source="Images/icon.png" RenderOptions.BitmapScalingMode="NearestNeighbor" HorizontalAlignment="Left" Width="16" Height="16" Margin="4,0,0,0" SnapsToDevicePixels="True" UseLayoutRounding="True" />
33+
<Image Source="Images/icon.png" x:Name="IconImage" RenderOptions.BitmapScalingMode="NearestNeighbor" HorizontalAlignment="Left" Width="16" Height="16" Margin="4,0,0,0" SnapsToDevicePixels="True" UseLayoutRounding="True" />
3434
<Label Content="UnityLauncherPro" IsHitTestVisible="False" Margin="19,0,0,-5" Foreground="{DynamicResource ThemeMainTitle}" FontSize="12" HorizontalAlignment="Left" />
3535
<!-- minimize -->
3636
<Button x:Name="btnMinimize" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="23" Width="23" Background="Transparent" Click="BtnMinimize_Click" Margin="0,0,27,0" Padding="2,0,2,8" IsTabStop="False">

UnityLauncherPro/MainWindow.xaml.cs

+41-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using System.Windows.Controls;
1919
using System.Windows.Data;
2020
using System.Windows.Input;
21+
using System.Windows.Interop;
2122
using System.Windows.Media;
23+
using System.Windows.Media.Imaging;
2224
using System.Windows.Shell;
2325
using UnityLauncherPro.Data;
2426
using UnityLauncherPro.Helpers;
@@ -45,7 +47,7 @@ public partial class MainWindow : Window
4547
const string githubURL = "https://github.com/unitycoder/UnityLauncherPro";
4648
const string resourcesURL = "https://github.com/unitycoder/UnityResources";
4749
const string defaultAdbLogCatArgs = "-s Unity ActivityManager PackageManager dalvikvm DEBUG -v color";
48-
System.Windows.Forms.NotifyIcon notifyIcon;
50+
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
4951

5052
UnityVersion[] updatesSource;
5153
public static List<string> updatesAsStrings = new List<string>();
@@ -110,6 +112,8 @@ void Init()
110112
{
111113
lblVersion.Content = "Build: " + Version.Stamp;
112114
}
115+
116+
CheckCustomIcon();
113117
}
114118

115119
void Start()
@@ -160,8 +164,6 @@ void Start()
160164
gridBuildReportData.Items.Clear();
161165

162166
// build notifyicon (using windows.forms)
163-
notifyIcon = new System.Windows.Forms.NotifyIcon();
164-
notifyIcon.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/Images/icon.ico")).Stream);
165167
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseClick);
166168

167169
// get original colors
@@ -3756,7 +3758,43 @@ private void OnPipeConnection(IAsyncResult result)
37563758
}
37573759
}
37583760

3761+
private void CheckCustomIcon()
3762+
{
3763+
string customIconPath = Path.Combine(Environment.CurrentDirectory, "icon.ico");
3764+
Console.WriteLine(customIconPath);
37593765

3766+
if (File.Exists(customIconPath))
3767+
{
3768+
try
3769+
{
3770+
// Load the custom icon using System.Drawing.Icon
3771+
using (var icon = new System.Drawing.Icon(customIconPath))
3772+
{
3773+
// Convert the icon to a BitmapSource and assign it to the WPF window's Icon property
3774+
this.Icon = Imaging.CreateBitmapSourceFromHIcon(
3775+
icon.Handle,
3776+
Int32Rect.Empty,
3777+
BitmapSizeOptions.FromEmptyOptions() // Use BitmapSizeOptions here
3778+
);
3779+
3780+
// window icon
3781+
IconImage.Source = this.Icon;
3782+
// tray icon
3783+
notifyIcon.Icon = new Icon(customIconPath);
3784+
}
3785+
}
3786+
catch (Exception ex)
3787+
{
3788+
SetStatus("Failed to load custom icon: " + ex.Message, MessageType.Warning);
3789+
Debug.WriteLine($"Failed to load custom icon: {ex.Message}");
3790+
}
3791+
}
3792+
else // no custom icon found
3793+
{
3794+
notifyIcon.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/Images/icon.ico")).Stream);
3795+
//Debug.WriteLine("Custom icon not found. Using default.");
3796+
}
3797+
}
37603798
//private void menuProjectProperties_Click(object sender, RoutedEventArgs e)
37613799
//{
37623800
// var proj = GetSelectedProject();

0 commit comments

Comments
 (0)