From 6ffa53f6eed834de8765ee5dbe9911ef5ed3c7bb Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Wed, 7 Apr 2021 16:39:35 +0200 Subject: [PATCH] Added Utilities to change DPIAwareness --- src/Artemis.UI/Bootstrapper.cs | 5 +++- src/Artemis.UI/Properties/AssemblyInfo.cs | 4 ++- src/Artemis.UI/Utilities/DPIAwareness.cs | 36 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/Artemis.UI/Utilities/DPIAwareness.cs diff --git a/src/Artemis.UI/Bootstrapper.cs b/src/Artemis.UI/Bootstrapper.cs index 471d56412..11d1f56c9 100644 --- a/src/Artemis.UI/Bootstrapper.cs +++ b/src/Artemis.UI/Bootstrapper.cs @@ -14,6 +14,7 @@ using Artemis.UI.Services; using Artemis.UI.Shared; using Artemis.UI.Shared.Services; using Artemis.UI.Stylet; +using Artemis.UI.Utilities; using Ninject; using Serilog; using Stylet; @@ -47,6 +48,8 @@ namespace Artemis.UI return; } + try { DPIAwareness.Initalize(); } catch (Exception ex) { logger.Error($"Failed to set DPI-Awareness: {ex.Message}"); } + IViewManager viewManager = Kernel.Get(); StartupArguments = Args.ToList(); @@ -68,7 +71,7 @@ namespace Artemis.UI Execute.OnUIThreadSync(() => { UIElement view = viewManager.CreateAndBindViewForModelIfNecessary(RootViewModel); - ((TrayViewModel) RootViewModel).SetTaskbarIcon(view); + ((TrayViewModel)RootViewModel).SetTaskbarIcon(view); }); // Initialize the core async so the UI can show the progress diff --git a/src/Artemis.UI/Properties/AssemblyInfo.cs b/src/Artemis.UI/Properties/AssemblyInfo.cs index 412a20daf..aee89dd05 100644 --- a/src/Artemis.UI/Properties/AssemblyInfo.cs +++ b/src/Artemis.UI/Properties/AssemblyInfo.cs @@ -1,11 +1,14 @@ using System.Runtime.InteropServices; using System.Windows; +using System.Windows.Media; // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] +[assembly: DisableDpiAwareness] + //In order to begin building localizable applications, set //CultureYouAreCodingWith in your .csproj file //inside a . For example, if you are using US english @@ -15,7 +18,6 @@ using System.Windows; //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] - [assembly: ThemeInfo( ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located //(used if a resource is not found in the page, diff --git a/src/Artemis.UI/Utilities/DPIAwareness.cs b/src/Artemis.UI/Utilities/DPIAwareness.cs new file mode 100644 index 000000000..1a89562a2 --- /dev/null +++ b/src/Artemis.UI/Utilities/DPIAwareness.cs @@ -0,0 +1,36 @@ +using System.Runtime.InteropServices; + +namespace Artemis.UI.Utilities +{ + public static class DPIAwareness + { + [DllImport("user32.dll", SetLastError = true)] + internal static extern bool SetProcessDpiAwarenessContext(int dpiFlag); + + [DllImport("SHCore.dll", SetLastError = true)] + internal static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness); + + [DllImport("user32.dll")] + internal static extern bool SetProcessDPIAware(); + + internal enum PROCESS_DPI_AWARENESS + { + Process_DPI_Unaware = 0, + Process_System_DPI_Aware = 1, + Process_Per_Monitor_DPI_Aware = 2 + } + + internal enum DPI_AWARENESS_CONTEXT + { + DPI_AWARENESS_CONTEXT_UNAWARE = 16, + DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17, + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18, + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34 + } + + public static void Initalize() + { + SetProcessDpiAwarenessContext((int)DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + } + } +}