From 2997f31bc3e9b889395a2761a47342ba4219a7e0 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 13 Mar 2017 19:51:58 +0000 Subject: [PATCH] Set OpenRA.Game.exe icon to match the mod. --- packaging/windows/WindowsLauncher.cs.in | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packaging/windows/WindowsLauncher.cs.in b/packaging/windows/WindowsLauncher.cs.in index 8231ed2285..b9d6096334 100644 --- a/packaging/windows/WindowsLauncher.cs.in +++ b/packaging/windows/WindowsLauncher.cs.in @@ -23,6 +23,34 @@ namespace OpenRA { class WindowsLauncher { + [DllImport("user32.dll")] + static extern int SendMessage(IntPtr hwnd, uint message, uint wParam, IntPtr lParam); + + [DllImport("shell32.dll")] + static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); + + [DllImport("user32.dll")] + public extern static bool DestroyIcon(IntPtr handle); + + struct SHFILEINFO + { + // Native type: HICON + public IntPtr hIcon; + + public int iIcon; + public uint dwAttributes; + + // Native type: TCHAR[MAX_PATH] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + + public string szDisplayName; + + // Native type: TCHAR[80] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] + + public string szTypeName; + } + static Process gameProcess; // Constants to be replaced by the wrapper / compilation script @@ -30,6 +58,9 @@ namespace OpenRA const string DisplayName = "DISPLAY_NAME"; const string FaqUrl = "FAQ_URL"; + // References to the OpenRA.Game.exe icons for later cleanup + static IntPtr[] iconHandle = { IntPtr.Zero, IntPtr.Zero }; + [STAThread] static void Main(string[] args) { @@ -60,6 +91,21 @@ namespace OpenRA if (gameProcess == null) return; + if (Platform.CurrentPlatform == PlatformType.Windows) + { + // Set the OpenRA.Game.exe icon to match the mod + // Icon.ExtractAssociatedIcon sets only the 32px icon, + // so we use native functions to set both 16 and 32px versions. + gameProcess.WaitForInputIdle(); + SHFILEINFO sfi = new SHFILEINFO(); + for (var i = 0; i < 2; i++) + { + SHGetFileInfo(Assembly.GetExecutingAssembly().Location, 0, ref sfi, (uint)Marshal.SizeOf(sfi), (uint)(0x100 + i)); + iconHandle[i] = sfi.hIcon; + SendMessage(gameProcess.MainWindowHandle, 0x80, (uint)(1 - i), sfi.hIcon); + } + } + gameProcess.EnableRaisingEvents = true; gameProcess.Exited += GameProcessExited; @@ -147,6 +193,11 @@ namespace OpenRA if (gameProcess.ExitCode != (int)RunStatus.Success) ShowErrorDialog(); + if (Platform.CurrentPlatform == PlatformType.Windows) + foreach (var handle in iconHandle) + if (handle != IntPtr.Zero) + DestroyIcon(handle); + Exit(); }