41

How would one poll windows to see what monitors are attached and what resolution they are running at?

0

3 Answers 3

68

In C#: Screen Class Represents a display device or multiple display devices on a single system. You want the Bounds attribute.

foreach(var screen in Screen.AllScreens)
{
    // For each screen, add the screen properties to a list box.
    listBox1.Items.Add("Device Name: " + screen.DeviceName);
    listBox1.Items.Add("Bounds: " + screen.Bounds.ToString());
    listBox1.Items.Add("Type: " + screen.GetType().ToString());
    listBox1.Items.Add("Working Area: " + screen.WorkingArea.ToString());
    listBox1.Items.Add("Primary Screen: " + screen.Primary.ToString());
}
4
  • 4
    By using foreach (Screen screen in Screen.AllScreens) this looks even better.
    – Max Truxa
    Commented Jul 25, 2013 at 13:23
  • Indeed. When I answered, I didn't know C# :)
    – Joe Koberg
    Commented Jul 29, 2013 at 18:55
  • This only shows one monitor when running from a service, is there a work around? Commented Jul 9, 2015 at 17:19
  • 1
    This only reports 1 if there are 2 monitors connected and the display is mirrored. Commented Dec 28, 2016 at 21:58
7

Use the Screen class.

You can see all of the monitors in the Screen.AllScreens array, and check the resolution and position of each one using the Bounds property.

Note that some video cards will merge two monitors into a single very wide screen, so that Windows thinks that there is only one monitor. If you want to, you could check whether the width of a screen is more than twice its height; if so, it's probably a horizontal span and you can treat it as two equal screens. However, this is more complicated and you don't need to do it. Vertical spans are also supported but less common.

4

http://msdn.microsoft.com/en-us/magazine/cc301462.aspx

GetSystemMetrics is a handy function you can use to get all sorts of global dimensions, like the size of an icon or height of a window caption. In Windows 2000, there are new parameters like SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN to get the virtual size of the screen for multiple monitor systems. Windows newbies—and pros, too—should check out the documentation for GetSystemMetrics to see all the different system metrics (dimensions) you can get. See the Platform SDK for the latest at http://msdn.microsoft.com/library/en-us/sysinfo/sysinfo_8fjn.asp. GetSystemMetrics is a handy function you frequently need to use, and new stuff appears with every version of Windows.

1
  • This is very cool. There is managed code for most of this stuff... For instance, the System.Windows.Forms.SystemInformation class likely contains a majority.
    – brandeded
    Commented Oct 22, 2013 at 17:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.