I have been struggling with this for some time. I have a Windows form application, where the position of a Window is stored on closing. This form can then be opened the next time the application runs. It will work on multiple monitors as long as the monitor is on. However, sometimes people run the software on a computer and the next time they run it, it is without a second monitor or the monitor is off. When the software starts, the form is therefore not visible. Now there are a number of examples out there which are supposed to correct this, none of which work! They all use the Screens.AllScreens function. The problem is that this function will return that two screens are available, even if one is off. Here is an example from a stackoverflow post 10 years ago.
public bool IsOnScreen( Form form )
{
Screen[] screens = Screen.AllScreens;
foreach( Screen screen in screens )
{
Point formTopLeft = new Point( form.Left, form.Top );
if( screen.WorkingArea.Contains( formTopLeft ) )
{
return true;
}
}
return false;
}
Does anyone have any working examples? Thanks