Getting screen information comes from the Screen class, which is a part of the System.Windows.Forms namespace.

In this class you are able to gather all the information you need about available screens including the device names, bounds, work area and how many screens are available.

You can explore MSDN for all of the information available to you, but I will go through the most prominent here.

Screen Information

For each of the monitors in the array you are able to get the following information:

Bits Per Pixel: The pixel bit depth.
Bounds: This will return a rectangle, the size being that of the resolution of the screen.
Device Name: The name of the screen.
Primary: A boolean value as to whether the screen is the primary screen or not.
Working Area: The area of the screen minus the taskbar or any other docked windows or toolbars.

Screen Count

Screen.AllScreens contains an array of screen objects representing all of the available screens on your system. To get the count of these you can use

Screen.AllScreens.Count();

or

Screen.AllScreens.Length;

Current Screen

You can retrieve whichever screen your Windows Form is on by using this code:

string CurrentScreenName = Screen.FromControl(this).DeviceName;

Or you can retrieve a screen object:

Screen CurrentScreen = Screen.FromControl(this);

You could also retrieve which screen the mouse is on:

Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));

Make Use Of It

There are many ways you can use this information to make your application multi-monitor friendly, here is an example:

Let’s say you want your form to show up in the the middle of the second available screen. So, this could be the second monitor in a two monitor setup:

this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);

Warnings

  • If you are saving and loading the position of your application in a multi screen setup, be sure to check each time that you set the window position that the screens are still available. Otherwise you may be setting your window position to a place where the user will never be able to see or interact with it if they remove the other screens.

Posted by 요지
,