How do I determine which monitor my .NET Windows Forms program is running on?

C#.NetWindowsWinformsScreen

C# Problem Overview


I have a C# Windows application that I want to ensure will show up on a second monitor if the user moves it to one. I need to save the main form's size, location and window state - which I've already handled - but I also need to know which screen it was on when the user closed the application.

I'm using the Screen class to determine the size of the current screen but I can't find anything on how to determine which screen the application was running on.

Edit: Thanks for the responses, everyone! I wanted to determine which monitor the window was on so I could do proper bounds checking in case the user accidentally put the window outside the viewing area or changed the screen size such that the form wouldn't be completely visible anymore.

C# Solutions


Solution 1 - C#

You can get an array of Screens that you have using this code.

Screen[] screens = Screen.AllScreens;

You can also figure out which screen you are on, by running this code (this is the windows form you are on)

Screen screen = Screen.FromControl(this); //this is the Form class

in short check out the Screen class and static helper methods, they might help you.

MSDN Link, doesn't have much..I suggest messing around in the code by yourself.

Solution 2 - C#

If you remember the window's location and size, that will be enough. When you set the position to the previously used position, if it happened to be on the second monitor it will go back there.

For example, if you have 2 monitors, both sized 1280x1024 and you set your window's left position to be 2000px, it will appear on the second monitor (assuming the second monitor is to the right of the first.) :)

If you are worried about the second monitor not being there when the application is started the next time, you can use this method to determine if your window intersects any of the screens:

private bool isWindowVisible(Rectangle rect)
{
	foreach (Screen screen in Screen.AllScreens)
	{
		if (screen.Bounds.IntersectsWith(rect))
			return true;
	}
	return false;
}

Just pass in your window's desired location and it will tell you if it will be visible on one of the screens. Enjoy!

Solution 3 - C#

You can get the current Screen with

var s = Screen.FromControl(this);

where this is the Form (or any control on the Form). As about how to remember that is a little tricky, but I would go for the index in the Screen.AllScreens array, or maybe s.DeviceName. In either case, check before using the settings on startup, to prevent using a monitor that was disconnected.

Solution 4 - C#

The location of the form will tell you which screen the form is on. I don't really understand why you'd need to know what screen it is on, because if you restore it using the location you saved it should just restore to the same location (maybe you can expand as to why).

Otherwise you can do something like this:

Screen[] scr = Screen.AllScreens;
scr[i].Bounds.IntersectsWith(form.Bounds);

Each screen has a Bounds property which returns a Rectangle. You can use the IntersectsWith() function to determine if the form is within the screen.

Also, they basically provide a function that does this as well on the Screen class

Screen screen = Screen.FromControl(form);

Solution 5 - C#

You can use the 'Screen' object: System.Windows.Forms.Screen

Start playing with something like this:

        Screen[] screens = Screen.AllScreens;
        for (int i = 0; i < screens.Length ; i++)
        {
            Debug.Print(screens[i].Bounds.ToString());
            Debug.Print(screens[i].DeviceName);
            Debug.Print(screens[i].WorkingArea.ToString());
        }

It may get you what you need

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen FletcherView Question on Stackoverflow
Solution 1 - C#Stan R.View Answer on Stackoverflow
Solution 2 - C#Jon TackaburyView Answer on Stackoverflow
Solution 3 - C#Henk HoltermanView Answer on Stackoverflow
Solution 4 - C#ChapView Answer on Stackoverflow
Solution 5 - C#The SavageView Answer on Stackoverflow