Add support for WinForms DPI scaling

This commit is contained in:
pizzaboxer 2023-01-09 19:39:27 +00:00
parent 7a858ca08f
commit 078de51ce7
6 changed files with 53 additions and 2 deletions

View File

@ -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)

View File

@ -35,6 +35,7 @@ namespace Bloxstrap.Dialogs.BootstrapperDialogs
Bootstrapper = bootstrapper;
ScaleWindow();
SetupDialog();
}

View File

@ -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();
}

View File

@ -1,6 +1,5 @@
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text.Json;

View File

@ -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));
}
}
}

View File

@ -132,7 +132,7 @@ namespace Bloxstrap
string commandLine = "";
#if false //DEBUG
#if DEBUG
new Preferences().ShowDialog();
#else
if (args.Length > 0)