From 03a221bff440b42e9f65380436efd8760d76b4ab Mon Sep 17 00:00:00 2001 From: NinjaPedroX <32976499+NinjaPedroX@users.noreply.github.com> Date: Thu, 27 Apr 2023 17:05:22 -0400 Subject: [PATCH] Restore Launcher Window Animations (#2031) * Restoring Window Animations Code for restoring the window animations * Refactored window animation enable code to WindowsApi.cs --------- Co-authored-by: Measurity --- NitroxLauncher/MainWindow.xaml | 3 +- NitroxLauncher/MainWindow.xaml.cs | 15 ++++++-- .../OS/Windows/Internal/Win32Native.cs | 38 +++++++++++++++++++ .../Platforms/OS/Windows/WindowsApi.cs | 26 +++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 NitroxModel/Platforms/OS/Windows/WindowsApi.cs diff --git a/NitroxLauncher/MainWindow.xaml b/NitroxLauncher/MainWindow.xaml index 9a96b36674..0caaef9120 100644 --- a/NitroxLauncher/MainWindow.xaml +++ b/NitroxLauncher/MainWindow.xaml @@ -11,7 +11,8 @@ Title="Nitrox Launcher" Height="642" MinHeight="642" Width="1024" MinWidth="1024" WindowStyle="None" WindowStartupLocation="CenterScreen" Closing="OnClosing" - Background="Black"> + Background="Black" + Loaded="Window_Loaded"> diff --git a/NitroxLauncher/MainWindow.xaml.cs b/NitroxLauncher/MainWindow.xaml.cs index 401b32a5ce..f4321f055d 100644 --- a/NitroxLauncher/MainWindow.xaml.cs +++ b/NitroxLauncher/MainWindow.xaml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.IO; using System.Net.NetworkInformation; @@ -7,11 +7,13 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Interop; using NitroxLauncher.Models.Events; using NitroxLauncher.Models.Properties; using NitroxLauncher.Pages; using NitroxModel.Discovery; using NitroxModel.Helper; +using NitroxModel.Platforms.OS.Windows; namespace NitroxLauncher { @@ -41,7 +43,7 @@ public MainWindow() { Log.Setup(); LauncherNotifier.Setup(); - + logic = new LauncherLogic(); MaxHeight = SystemParameters.VirtualScreenHeight; @@ -66,7 +68,7 @@ public MainWindow() MessageBoxImage.Error); Environment.Exit(1); } - + // This pirate detection subscriber is immediately invoked if pirate has been detected right now. PirateDetection.PirateDetected += (o, eventArgs) => { @@ -78,7 +80,7 @@ public MainWindow() HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Margin = new Thickness(0), - + Height = MinHeight * 0.7, Width = MinWidth * 0.7 }; @@ -216,5 +218,10 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + + private void Window_Loaded(object sender, RoutedEventArgs e) + { + WindowsApi.EnableDefaultWindowAnimations(new WindowInteropHelper(this).Handle); + } } } diff --git a/NitroxModel/Platforms/OS/Windows/Internal/Win32Native.cs b/NitroxModel/Platforms/OS/Windows/Internal/Win32Native.cs index 57a1295e44..66835367b8 100644 --- a/NitroxModel/Platforms/OS/Windows/Internal/Win32Native.cs +++ b/NitroxModel/Platforms/OS/Windows/Internal/Win32Native.cs @@ -334,6 +334,44 @@ private enum UIContext Install } + [DllImport("user32.dll", EntryPoint = "SetWindowLong")] + internal static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong); + + [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] + internal static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong); + + [Flags] + public enum WS : long + { + WS_BORDER = 0x00800000L, + WS_CAPTION = 0x00C00000L, + WS_CHILD = 0x40000000L, + WS_CHILDWINDOW = 0x40000000L, + WS_CLIPCHILDREN = 0x02000000L, + WS_CLIPSIBLINGS = 0x04000000L, + WS_DISABLED = 0x08000000L, + WS_DLGFRAME = 0x00400000L, + WS_GROUP = 0x00020000L, + WS_HSCROLL = 0x00100000L, + WS_ICONIC = 0x20000000L, + WS_MAXIMIZE = 0x01000000L, + WS_MAXIMIZEBOX = 0x00010000L, + WS_MINIMIZE = 0x20000000L, + WS_MINIMIZEBOX = 0x00020000L, + WS_OVERLAPPED = 0x00000000L, + WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, + WS_POPUP = 0x80000000L, + WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU, + WS_SIZEBOX = 0x00040000L, + WS_SYSMENU = 0x00080000L, + WS_TABSTOP = 0x00010000L, + WS_THICKFRAME = 0x00040000L, + WS_TILED = 0x00000000L, + WS_TILEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, + WS_VISIBLE = 0x10000000L, + WS_VSCROLL = 0x00200000L + } + [StructLayout(LayoutKind.Sequential)] private struct WINTRUST_DATA : IDisposable { diff --git a/NitroxModel/Platforms/OS/Windows/WindowsApi.cs b/NitroxModel/Platforms/OS/Windows/WindowsApi.cs new file mode 100644 index 0000000000..ff3493215b --- /dev/null +++ b/NitroxModel/Platforms/OS/Windows/WindowsApi.cs @@ -0,0 +1,26 @@ +using System; +using System.Runtime.InteropServices; +using NitroxModel.Platforms.OS.Windows.Internal; + +namespace NitroxModel.Platforms.OS.Windows; + +public class WindowsApi +{ + public static void EnableDefaultWindowAnimations(IntPtr hWnd, int nIndex = -16) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + IntPtr dwNewLong = new((long)(Win32Native.WS.WS_CAPTION | Win32Native.WS.WS_CLIPCHILDREN | Win32Native.WS.WS_MINIMIZEBOX | Win32Native.WS.WS_MAXIMIZEBOX | Win32Native.WS.WS_SYSMENU | Win32Native.WS.WS_SIZEBOX)); + HandleRef handle = new(null, hWnd); + switch (IntPtr.Size) + { + case 8: + Win32Native.SetWindowLongPtr64(handle, nIndex, dwNewLong); + break; + default: + Win32Native.SetWindowLong32(handle, nIndex, dwNewLong.ToInt32()); + break; + } + } + } +}