Skip to content

Commit b561e5a

Browse files
authored
Fix: Modal window sample not scaled to dpi (#2147)
<!--- Provide a general summary of your changes in the Title above --> ## Description The Modal window example size is not scaled to dpi currently. On a high dpi screen,the window content is not even complete. This PR fixes it, together with sample code. ## Motivation and Context <!--- Why is this change required? What problem does it solve? --> <!--- If it fixes an open issue, please link to the issue here. --> ## How Has This Been Tested? <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots (if appropriate): Before: <img width="370" height="285" alt="image" src="https://github.com/user-attachments/assets/46b358a9-cf5a-49ea-887c-ba6e14e98387" /> After: <img width="1170" height="885" alt="image" src="https://github.com/user-attachments/assets/df01a450-553b-45b0-b831-64905705c202" /> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
1 parent e045dd9 commit b561e5a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

WinUIGallery/Samples/SampleCode/AppWindow/AppWindowSample5_cs.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ public sealed partial class ModalWindow : Window
1212
public ModalWindow()
1313
{
1414
this.InitializeComponent();
15-
AppWindow.Resize(new Windows.Graphics.SizeInt32(400, 300));
15+
16+
var dpi = Windows.Win32.PInvoke.GetDpiForWindow(new Windows.Win32.Foundation.HWND(WinRT.Interop.WindowNative.GetWindowHandle(this)));
17+
var scalingFactor = (float)dpi / 96;
18+
19+
AppWindow.Resize(new Windows.Graphics.SizeInt32((int)(400.0f * scalingFactor), (int)(300.0f * scalingFactor)));
1620
AppWindow.SetIcon("Assets/Tiles/GalleryIcon.ico");
1721
AppWindow.TitleBar.PreferredTheme = TitleBarTheme.UseDefaultAppMode;
1822

WinUIGallery/Samples/SamplePages/ModalWindow.xaml.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using Microsoft.UI;
@@ -15,7 +15,11 @@ public sealed partial class ModalWindow : Window
1515
public ModalWindow()
1616
{
1717
this.InitializeComponent();
18-
AppWindow.Resize(new Windows.Graphics.SizeInt32(400, 300));
18+
19+
var dpi = Windows.Win32.PInvoke.GetDpiForWindow(new Windows.Win32.Foundation.HWND(WinRT.Interop.WindowNative.GetWindowHandle(this)));
20+
var scalingFactor = (float)dpi / 96;
21+
22+
AppWindow.Resize(new Windows.Graphics.SizeInt32((int)(400.0f * scalingFactor), (int)(300.0f * scalingFactor)));
1923
AppWindow.SetIcon("Assets/Tiles/GalleryIcon.ico");
2024
AppWindow.TitleBar.PreferredTheme = TitleBarTheme.UseDefaultAppMode;
2125

0 commit comments

Comments
 (0)