Skip to content

Commit

Permalink
Merge pull request #5 from hellcat707hp/fix-mismatch-display-numbers
Browse files Browse the repository at this point in the history
fix: corrected instances where DeviceName uses a higher number than monitor number
  • Loading branch information
hellcat707hp authored Jul 5, 2023
2 parents 962d507 + e4d6195 commit 74ae522
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion MultiMonitorRdp/MultiMonitorRdp.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.1</VersionPrefix>
</PropertyGroup>

</Project>
12 changes: 8 additions & 4 deletions MultiMonitorRdp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@
return;
}

var displaysltr = (from display in Screen.AllScreens
orderby display.Bounds.Left select display).ToList();
//Windows will sometimes give monitors arbitrarily high numbers as part of the Screen.DeviceName, like 16, 17, 18, when there's only 3 monitors connected.
//To adjust for this, we take all the Screens and order them by the number in DeviceName, then set a proper display number based on the index of the item in the resulting list
int realDisplayNumber = 0;
var displaysltr = Screen.AllScreens.Select(x => new { Screen = x, DeviceNumber = int.Parse(Regex.Match(x.DeviceName, @"\d+").Value) })
.OrderBy(x => x.DeviceNumber)
.Select(x => new { x.Screen, DisplayNumber = realDisplayNumber++ }) //Setting the "real" display number based on the order of the displays DeviceName number
.OrderBy(x => x.Screen.Bounds.Left).ToList(); //Now, we order them by left-to-right so we can actually set the correct monitors to use


//Figure out the actual display numbers that Windows is using so we can hand those to RDP
Expand All @@ -52,8 +57,7 @@
return;
}
var display = displaysltr[desiredDisplayNum];
int displayNumber = int.Parse(Regex.Match(display.DeviceName, @"\d+").Value) - 1;
displayNumbersToUse.Add(displayNumber);
displayNumbersToUse.Add(display.DisplayNumber);
}

if (!File.Exists(args[1]))
Expand Down

0 comments on commit 74ae522

Please sign in to comment.