From 078de51ce7a4179cc79ffa094b18cb8ec414ef49 Mon Sep 17 00:00:00 2001 From: pizzaboxer <41478239+pizzaboxer@users.noreply.github.com> Date: Mon, 9 Jan 2023 19:39:27 +0000 Subject: [PATCH] Add support for WinForms DPI scaling --- .../BootstrapperDialogForm.cs | 13 +++++++ .../BootstrapperDialogs/LegacyDialog2009.cs | 1 + .../BootstrapperDialogs/LegacyDialog2011.cs | 1 + Bloxstrap/Helpers/Utilities.cs | 1 - Bloxstrap/Helpers/WindowScaling.cs | 37 +++++++++++++++++++ Bloxstrap/Program.cs | 2 +- 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Bloxstrap/Helpers/WindowScaling.cs diff --git a/Bloxstrap/Dialogs/BootstrapperDialogs/BootstrapperDialogForm.cs b/Bloxstrap/Dialogs/BootstrapperDialogs/BootstrapperDialogForm.cs index adac9b6..425df1b 100644 --- a/Bloxstrap/Dialogs/BootstrapperDialogs/BootstrapperDialogForm.cs +++ b/Bloxstrap/Dialogs/BootstrapperDialogs/BootstrapperDialogForm.cs @@ -1,4 +1,5 @@ using Bloxstrap.Enums; +using Bloxstrap.Helpers; namespace Bloxstrap.Dialogs.BootstrapperDialogs { @@ -59,6 +60,18 @@ namespace Bloxstrap.Dialogs.BootstrapperDialogs } } + public void ScaleWindow() + { + this.Size = this.MinimumSize = this.MaximumSize = WindowScaling.GetScaledSize(this.Size); + + foreach (Control control in this.Controls) + { + control.Size = WindowScaling.GetScaledSize(control.Size); + control.Location = WindowScaling.GetScaledPoint(control.Location); + control.Padding = WindowScaling.GetScaledPadding(control.Padding); + } + } + public void SetupDialog() { if (Program.IsQuiet) diff --git a/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2009.cs b/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2009.cs index b09109b..24224d8 100644 --- a/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2009.cs +++ b/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2009.cs @@ -35,6 +35,7 @@ namespace Bloxstrap.Dialogs.BootstrapperDialogs Bootstrapper = bootstrapper; + ScaleWindow(); SetupDialog(); } diff --git a/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2011.cs b/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2011.cs index c9dcfdd..b6236e0 100644 --- a/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2011.cs +++ b/Bloxstrap/Dialogs/BootstrapperDialogs/LegacyDialog2011.cs @@ -39,6 +39,7 @@ namespace Bloxstrap.Dialogs.BootstrapperDialogs // have to convert icon -> bitmap since winforms scaling is poop this.IconBox.Image = Program.Settings.BootstrapperIcon.GetIcon().ToBitmap(); + ScaleWindow(); SetupDialog(); } diff --git a/Bloxstrap/Helpers/Utilities.cs b/Bloxstrap/Helpers/Utilities.cs index 6b2e93e..11530c4 100644 --- a/Bloxstrap/Helpers/Utilities.cs +++ b/Bloxstrap/Helpers/Utilities.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.IO; -using System.Net.Http; using System.Security.Cryptography; using System.Text.Json; diff --git a/Bloxstrap/Helpers/WindowScaling.cs b/Bloxstrap/Helpers/WindowScaling.cs new file mode 100644 index 0000000..9954f34 --- /dev/null +++ b/Bloxstrap/Helpers/WindowScaling.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace Bloxstrap.Helpers +{ + public class WindowScaling + { + public static double GetFactor() + { + return Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth; + } + + public static int GetScaledNumber(int number) + { + return (int)Math.Ceiling(number * GetFactor()); + } + + public static System.Drawing.Size GetScaledSize(System.Drawing.Size size) + { + return new System.Drawing.Size(GetScaledNumber(size.Width), GetScaledNumber(size.Height)); + } + + public static System.Drawing.Point GetScaledPoint(System.Drawing.Point point) + { + return new System.Drawing.Point(GetScaledNumber(point.X), GetScaledNumber(point.Y)); + } + + public static Padding GetScaledPadding(Padding padding) + { + return new Padding(GetScaledNumber(padding.Left), GetScaledNumber(padding.Top), GetScaledNumber(padding.Right), GetScaledNumber(padding.Bottom)); + } + } +} diff --git a/Bloxstrap/Program.cs b/Bloxstrap/Program.cs index 14c4909..a0e6251 100644 --- a/Bloxstrap/Program.cs +++ b/Bloxstrap/Program.cs @@ -132,7 +132,7 @@ namespace Bloxstrap string commandLine = ""; -#if false //DEBUG +#if DEBUG new Preferences().ShowDialog(); #else if (args.Length > 0)