Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32: Fix edge-case bug with DPI scaling #16143

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected virtual unsafe IntPtr AppWndProc(IntPtr hWnd, uint msg, IntPtr wParam,
}

case WindowsMessage.WM_DPICHANGED:
if (!_ignoreDpiChanges)
{
_dpi = (uint)wParam >> 16;
var newDisplayRect = Marshal.PtrToStructure<RECT>(lParam);
Expand All @@ -151,6 +152,7 @@ protected virtual unsafe IntPtr AppWndProc(IntPtr hWnd, uint msg, IntPtr wParam,

return IntPtr.Zero;
}
break;

case WindowsMessage.WM_GETICON:
if (_iconImpl == null)
Expand Down
14 changes: 14 additions & 0 deletions src/Windows/Avalonia.Win32/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ internal partial class WindowImpl : IWindowImpl, EglGlPlatformSurface.IEglWindow
private bool _shown;
private bool _hiddenWindowIsParent;
private uint _langid;
private bool _ignoreDpiChanges;
internal bool _ignoreWmChar;
private WindowTransparencyLevel _transparencyLevel;
private readonly WindowTransparencyLevel _defaultTransparencyLevel;
Expand Down Expand Up @@ -707,7 +708,20 @@ public void SetParent(IWindowImpl? parent)

_hiddenWindowIsParent = parentHwnd == OffscreenParentWindow.Handle;

// I can't find mention of this *anywhere* online, but it seems that setting
// GWL_HWNDPARENT to a window which is on the non-primary monitor can cause two
// WM_DPICHANGED messages to be sent: the first changing the DPI to the parent's DPI,
// then another changing the DPI back. This then causes Windows to provide an incorrect
// suggested new rectangle to the WM_DPICHANGED message if the window is immediately
// moved to the parent window's monitor (e.g. when using
// WindowStartupLocation.CenterOwner) causing the window to be shown with an incorrect
// size.
//
// Just ignore any WM_DPICHANGED while we're setting the parent as this shouldn't
// change the DPI anyway.
_ignoreDpiChanges = true;
SetWindowLongPtr(_hwnd, (int)WindowLongParam.GWL_HWNDPARENT, parentHwnd);
_ignoreDpiChanges = false;
}

public void SetEnabled(bool enable) => EnableWindow(_hwnd, enable);
Expand Down
Loading