Skip to content

Commit a166042

Browse files
committed
Add support for getting DPI from the window (WinForms)
1 parent 2de0d2a commit a166042

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

sources/engine/Stride.Games/Desktop/GameWindowWinforms.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ protected override void Initialize(GameContext<Control> gameContext)
159159
gameForm.FullscreenToggle += OnFullscreenToggle;
160160
gameForm.DisableFullScreen += OnDisableFullScreen;
161161
gameForm.FormClosing += OnClosing;
162+
gameForm.DpiChanged += OnDpiChanged;
162163
}
163164
else
164165
{
@@ -273,12 +274,12 @@ public override bool Visible
273274
}
274275

275276
/// <inheritdoc />
276-
public override double Opacity
277+
public override double Opacity
277278
{
278279
get
279280
{
280281
return form?.Opacity ?? 1.0d;
281-
}
282+
}
282283
set
283284
{
284285
if (form != null)
@@ -287,7 +288,7 @@ public override double Opacity
287288
}
288289
}
289290
}
290-
291+
291292
public override Int2 Position
292293
{
293294
get
@@ -409,6 +410,12 @@ public override bool Focused
409410
}
410411
}
411412

413+
private void OnDpiChanged(object sender, DpiChangedEventArgs e)
414+
{
415+
Dpi = new Int2(e.DeviceDpiNew);
416+
DpiScale = e.DeviceDpiNew / 96.0f;
417+
}
418+
412419
protected override void Destroy()
413420
{
414421
if (Control != null)

sources/engine/Stride.Games/GameWindow.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
33
//
44
// Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5-
//
5+
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
88
// in the Software without restriction, including without limitation the rights
99
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
// copies of the Software, and to permit persons to whom the Software is
1111
// furnished to do so, subject to the following conditions:
12-
//
12+
//
1313
// The above copyright notice and this permission notice shall be included in
1414
// all copies or substantial portions of the Software.
15-
//
15+
//
1616
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -127,7 +127,7 @@ public abstract class GameWindow : ComponentBase
127127
/// </summary>
128128
/// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
129129
public abstract bool Visible { get; set; }
130-
130+
131131
/// <summary>
132132
/// Gets or sets the opacity of the window.
133133
/// </summary>
@@ -174,7 +174,7 @@ public string Title
174174
/// <summary>
175175
/// The size the window should have when switching from fullscreen to windowed mode.
176176
/// To get the current actual size use <see cref="ClientBounds"/>.
177-
/// This gets overwritten when the user resizes the window.
177+
/// This gets overwritten when the user resizes the window.
178178
/// </summary>
179179
public Int2 PreferredWindowedSize { get; set; } = new Int2(768, 432);
180180

@@ -215,6 +215,18 @@ internal void SetIsReallyFullscreen(bool isReallyFullscreen)
215215
isFullscreen = isReallyFullscreen;
216216
}
217217

218+
/// <summary>
219+
/// Gets the DPI scale factor of the display where this window is currently displayed,
220+
/// which is used to convert between physical pixels and device-independent pixels (DIPs).
221+
/// </summary>
222+
public float DpiScale { get; protected set; } = 1.0f; // 100 % = 96 DPI
223+
224+
/// <summary>
225+
/// Gets the dots per inch (DPI) of the display where this window is currently displayed
226+
/// in the horizontal and vertical directions.
227+
/// </summary>
228+
public Int2 Dpi { get; protected set; } = new Int2(96); // 96 DPI (baseline DPI for Windows and many other platforms)
229+
218230
#endregion
219231

220232
#region Public Methods and Operators
@@ -241,7 +253,7 @@ public void EndScreenDeviceChange()
241253
internal Action RunCallback;
242254

243255
internal Action ExitCallback;
244-
256+
245257
private bool isFullscreen;
246258

247259
internal abstract void Run();
@@ -284,9 +296,9 @@ protected void OnClientSizeChanged(object source, EventArgs e)
284296
{
285297
if (!isFullscreen)
286298
{
287-
// Update preferred windowed size in windowed mode
299+
// Update preferred windowed size in windowed mode
288300
var resizeSize = ClientBounds.Size;
289-
PreferredWindowedSize = new Int2(resizeSize.Width, resizeSize.Height);
301+
PreferredWindowedSize = new Int2(resizeSize.Width, resizeSize.Height);
290302
}
291303
var handler = ClientSizeChanged;
292304
handler?.Invoke(this, e);

0 commit comments

Comments
 (0)