Skip to content

Commit 30ba69d

Browse files
committed
WPF - GetScreenInfo scale Rect/AvailableRect
Scale by DpiScaleFactor
1 parent 9ca6fa9 commit 30ba69d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,11 +732,20 @@ private void InternalDispose(bool disposing)
732732
/// <returns>ScreenInfo containing the current DPI scale factor</returns>
733733
protected virtual ScreenInfo? GetScreenInfo()
734734
{
735+
Rect rect = monitorInfo.Monitor;
736+
Rect availableRect = monitorInfo.WorkArea;
737+
738+
if (DpiScaleFactor > 1.0)
739+
{
740+
rect = rect.ScaleByDpi(DpiScaleFactor);
741+
availableRect = availableRect.ScaleByDpi(DpiScaleFactor);
742+
}
743+
735744
var screenInfo = new ScreenInfo
736745
{
737746
DeviceScaleFactor = (float)DpiScaleFactor,
738-
Rect = monitorInfo.Monitor, //TODO: Do values need to be scaled?
739-
AvailableRect = monitorInfo.WorkArea //TODO: Do values need to be scaled?
747+
Rect = rect,
748+
AvailableRect = availableRect
740749
};
741750

742751
return screenInfo;

CefSharp/Structs/Rect.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5+
using System;
56
using System.Diagnostics;
67

78
namespace CefSharp.Structs
@@ -47,5 +48,20 @@ public Rect(int x, int y, int width, int height)
4748
Width = width;
4849
Height = height;
4950
}
51+
52+
/// <summary>
53+
/// Returns a new Rect with Scaled values
54+
/// </summary>
55+
/// <param name="dpi">Dpi to scale by</param>
56+
/// <returns>New rect with scaled values</returns>
57+
public Rect ScaleByDpi(double dpi)
58+
{
59+
var x = (int)Math.Floor(X / dpi);
60+
var y = (int)Math.Floor(Y / dpi);
61+
var width = (int)Math.Floor(Width / dpi);
62+
var height = (int)Math.Floor(Height / dpi);
63+
64+
return new Rect(x, y, width, height);
65+
}
5066
}
5167
}

0 commit comments

Comments
 (0)