Skip to content

Commit 2b37134

Browse files
committed
Fix: Rounding issue
1 parent 6563620 commit 2b37134

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NETworkManager.Settings;
99
using NETworkManager.Utilities;
1010
using System;
11+
using System.Diagnostics;
1112
using System.Threading.Tasks;
1213
using System.Windows;
1314
using System.Windows.Input;
@@ -162,15 +163,15 @@ private void DisconnectAction()
162163

163164
#region Methods
164165

165-
private Tuple<int, int> GetDesktopSize()
166+
private Tuple<double, double> GetDesktopSize()
166167
{
167168
// Get the screen size
168-
int desktopWidth, desktopHeight;
169+
double desktopWidth, desktopHeight;
169170

170171
if (_sessionInfo.AdjustScreenAutomatically || _sessionInfo.UseCurrentViewSize)
171172
{
172-
desktopWidth = (int)Math.Floor(RdpGrid.ActualWidth);
173-
desktopHeight = (int)Math.Floor(RdpGrid.ActualHeight);
173+
desktopWidth = RdpGrid.ActualWidth;
174+
desktopHeight = RdpGrid.ActualHeight;
174175
}
175176
else
176177
{
@@ -185,10 +186,10 @@ private Tuple<int, int> GetDesktopSize()
185186
desktopHeight = desktopHeight * scaleFactor / 100;
186187

187188
// Round the screen size to an even number
188-
desktopWidth = desktopWidth % 2 == 0 ? desktopWidth : desktopWidth - 1;
189-
desktopHeight = desktopHeight % 2 == 0 ? desktopHeight : desktopHeight - 1;
189+
desktopWidth = Math.Floor(desktopWidth / 2) * 2;
190+
desktopHeight = Math.Floor(desktopHeight / 2) * 2;
190191

191-
return new Tuple<int, int>(desktopWidth, desktopHeight);
192+
return new Tuple<double, double>(desktopWidth, desktopHeight);
192193
}
193194

194195
/// <summary>
@@ -222,8 +223,8 @@ private void Connect()
222223

223224
var desktopSize = GetDesktopSize();
224225

225-
RdpClient.DesktopWidth = desktopSize.Item1;
226-
RdpClient.DesktopHeight = desktopSize.Item2;
226+
RdpClient.DesktopWidth = (int)desktopSize.Item1;
227+
RdpClient.DesktopHeight = (int)desktopSize.Item2;
227228

228229
FixWindowsFormsHostSize(desktopSize.Item1, desktopSize.Item2);
229230

@@ -336,8 +337,8 @@ private void Reconnect()
336337

337338
var desktopSize = GetDesktopSize();
338339

339-
RdpClient.DesktopWidth = desktopSize.Item1;
340-
RdpClient.DesktopHeight = desktopSize.Item2;
340+
RdpClient.DesktopWidth = (int)desktopSize.Item1;
341+
RdpClient.DesktopHeight = (int)desktopSize.Item2;
341342

342343
FixWindowsFormsHostSize(desktopSize.Item1, desktopSize.Item2);
343344

@@ -352,7 +353,7 @@ public void FullScreen()
352353
RdpClient.FullScreen = true;
353354
}
354355

355-
public async void AdjustScreen()
356+
public async void AdjustScreen(bool dpiChange = false)
356357
{
357358
// Check preconditions
358359
if (IsConnecting)
@@ -366,20 +367,30 @@ public async void AdjustScreen()
366367

367368
var desktopSize = GetDesktopSize();
368369

369-
// Check if we need to adjust the screen
370-
if (RdpClient.DesktopWidth == desktopSize.Item1 && RdpClient.DesktopHeight == desktopSize.Item2)
371-
return;
370+
// Check if we need to adjust the screen (always on DPI changes)
371+
if (dpiChange == false)
372+
{
373+
if (RdpClient.DesktopWidth == desktopSize.Item1 && RdpClient.DesktopHeight == desktopSize.Item2)
374+
{
375+
Debug.WriteLine("==>>> Screen size is already correct.");
376+
return;
377+
}
372378

373-
if (RdpClient.Width == desktopSize.Item1 && RdpClient.Height == desktopSize.Item2)
374-
return;
379+
380+
if (RdpClient.Width == desktopSize.Item1 && RdpClient.Height == desktopSize.Item2)
381+
{
382+
Debug.WriteLine("==>>> Screen size is already correct.");
383+
return;
384+
}
385+
}
375386

376387
try
377388
{
378-
// This may fail if the RDP session was connected recently
379-
RdpClient.UpdateSessionDisplaySettings((uint)desktopSize.Item1, (uint)desktopSize.Item2, (uint)desktopSize.Item1, (uint)desktopSize.Item2, 0, GetDesktopScaleFactor(), GetDeviceScaleFactor());
380-
389+
Debug.WriteLine("UpdateSessionDisplaySettings...");
381390
// Fix the size of the WindowsFormsHost and the RDP control
382391
FixWindowsFormsHostSize(desktopSize.Item1, desktopSize.Item2);
392+
// This may fail if the RDP session was connected recently
393+
RdpClient.UpdateSessionDisplaySettings((uint)desktopSize.Item1, (uint)desktopSize.Item2, (uint)desktopSize.Item1, (uint)desktopSize.Item2, 0, GetDesktopScaleFactor(), GetDeviceScaleFactor());
383394
}
384395
catch (Exception ex)
385396
{
@@ -395,13 +406,23 @@ public async void AdjustScreen()
395406
/// <param name="height">Height of the RDP session.</param>
396407
private void FixWindowsFormsHostSize(double width, double height)
397408
{
409+
var widthScaled = width / GetDpiScaleFactor() * 100;
410+
var heightScaled = height / GetDpiScaleFactor() * 100;
411+
412+
widthScaled = Math.Ceiling(widthScaled / 2) * 2;
413+
heightScaled = Math.Ceiling(heightScaled / 2) * 2;
414+
398415
// Set the max width and height for the WindowsFormsHost
399-
WindowsFormsHostMaxWidth = (int)(width / GetDpiScaleFactor() * 100);
400-
WindowsFormsHostMaxHeight = (int)(height / GetDpiScaleFactor() * 100);
416+
WindowsFormsHostMaxWidth = widthScaled;
417+
WindowsFormsHostMaxHeight = heightScaled;
401418

402419
// Update the size of the RDP control
403420
RdpClient.Width = (int)width;
404421
RdpClient.Height = (int)height;
422+
423+
Debug.WriteLine($"RDP control size: {RdpClient.Width}x{RdpClient.Height}");
424+
Debug.WriteLine($"WindowsFormsHost size: {WindowsFormsHostMaxWidth}x{WindowsFormsHostMaxHeight}");
425+
Debug.WriteLine($"Values: {width}x{height} - DPI: {GetDpiScaleFactor()}%");
405426
}
406427

407428
/// <summary>
@@ -634,12 +655,15 @@ private void RdpClient_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnect
634655

635656
public void UpdateOnWindowResize()
636657
{
658+
659+
Debug.WriteLine("UpdateOnWindowResize...");
660+
637661
if (_sessionInfo.AdjustScreenAutomatically)
638662
AdjustScreen();
639663
}
640664

641665
private void WindowsFormsHost_DpiChanged(object sender, DpiChangedEventArgs e)
642666
{
643-
AdjustScreen();
667+
AdjustScreen(dpiChange: true);
644668
}
645669
}

0 commit comments

Comments
 (0)