Skip to content

Commit

Permalink
WinForms: Improve form size handling with high-dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Sep 2, 2024
1 parent 15e4a0e commit e258df0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 7 additions & 1 deletion NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ public override void InitForm(Window window)
form.DpiChanged += (_, _) => (window as IFormBase)?.LayoutController.Invalidate();
}

public override float GetScaleFactor(Window window) => window.ToNative().DeviceDpi / 96f;
public override float GetScaleFactor(Window window)
{
var form = window.ToNative();
// Force creation of form handle
_ = form.Handle;
return form.DeviceDpi / 96f;
}

public override bool ScaleLayout => true;

Expand Down
10 changes: 5 additions & 5 deletions NAPS2.Lib/EtoForms/FormStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void UpdateLayoutSize(LayoutController layoutController)
var oldDefaultClientSize = DefaultClientSize;
var oldMaximumClientSize = _maximumClientSize;
DefaultClientSize = layoutController.GetLayoutSize(true) + DefaultExtraLayoutSize;
_maximumClientSize = FixedHeightLayout ? new Size(0, _minimumClientSize.Height) : Size.Empty;
_maximumClientSize = FixedHeightLayout || !Resizable ? new Size(0, _minimumClientSize.Height) : Size.Empty;

if (_loaded)
{
Expand Down Expand Up @@ -111,10 +111,6 @@ protected void DoRestoreFormState()
throw new InvalidOperationException();
}
var location = new Point(_formState.Location.X, _formState.Location.Y);
var scale = EtoPlatform.Current.GetLayoutScaleFactor(_window);
var size = new Size(
(int) Math.Round(_formState.Size.Width * scale),
(int) Math.Round(_formState.Size.Height * scale));
if (!location.IsZero)
{
if (Screen.Screens.Any(x => x.WorkingArea.Contains(location)))
Expand All @@ -124,6 +120,10 @@ protected void DoRestoreFormState()
EtoPlatform.Current.SetFormLocation(_window, location);
}
}
var scale = EtoPlatform.Current.GetLayoutScaleFactor(_window);
var size = new Size(
(int) Math.Round(_formState.Size.Width * scale),
(int) Math.Round(_formState.Size.Height * scale));
if (!size.IsEmpty && Resizable)
{
if (!_minimumClientSize.IsEmpty)
Expand Down

0 comments on commit e258df0

Please sign in to comment.