Skip to content

Instantly share code, notes, and snippets.

@SQL-MisterMagoo
Last active September 4, 2024 00:13
Show Gist options
  • Save SQL-MisterMagoo/d82de1583ebcf5b921a81f25d77994c9 to your computer and use it in GitHub Desktop.
Save SQL-MisterMagoo/d82de1583ebcf5b921a81f25d77994c9 to your computer and use it in GitHub Desktop.
Get Windows Display By Convention - Numbered Top Left To Bottom Right

Windows Displays

...are likely/possibly numbered in the order they are detected - not necessarily in the order they appear in Settings or their physical order on your desk.

So, for example, it might seem natural to number your displays from top left to bottom right - just like reading a book.

This function GetDisplayByConvention() maps a number to a screen based on the Convention described above.

It gets AllScreens, orders them by Top, then by Left, skips the required number and returns the requested screen

e.g. on my laptop, which has this display setup

image

Calling the function for Display 1 GetDisplayByConvention(1) returns Display3 because it is in the top,left position:

[Screen[Bounds={X=-1080,Y=0,Width=1080,Height=1920} WorkingArea={X=-1080,Y=0,Width=1080,Height=1920} Primary=False DeviceName=\\.\DISPLAY3]

Maybe this works for you - or maybe you have a different Convention?

using System.Windows.Forms;
Screen GetDisplayByConvention(int displayNumber)
=> System.Windows.Forms.Screen.AllScreens
.OrderBy(screen => screen.WorkingArea.Top)
.ThenBy(screen => screen.WorkingArea.Left)
.Skip(displayNumber - 1)
.FirstOrDefault();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment