1

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

Tom
  • 527
  • 1
  • 8
  • 28
  • Read the notes here: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103). You can determine the current screen in different ways. Note the values returned by `SystemInformations.VirtualScreen` and `SystemInformation.PrimaryMonitorSize`. See what different tools are available to determine each screen's size and coordinates. and in what screen your application's Main Window is currently shown (or is about to). – Jimi Apr 14 '19 at 19:12
  • Thanks I will take a look. – Tom Apr 14 '19 at 19:17
  • Ok, This has thrown me off for a long time... The screen function will return the presence of a screen even if it is off. It has to be actually completely disconnected for it to work. This is why none of the examples worked for me! – Tom Apr 14 '19 at 19:43
  • What *screen function*? You can get the current screen in many different ways. Using your Main Window handle: `Screen currentScreen = Screen.FromHandle(this.Handle);`. The Screen where the Cursor is shown: `Screen currentScreen = Screen.FromPoint(Cursor.Position)` and so on. Many possibilities. What is throwing you off? (e.g., when you show your Main Form, get the current screen and offset the position you stored using the current screen coordinates, if the screen is a different one. Maybe, store the coordinates of the last Screen your app's Main Form was last run in) – Jimi Apr 14 '19 at 19:49
  • If you look at the original example, Screens.AllScreens will say that a monitor is present even if it is off. It has to be physically removed from the computer by disconnecting the cable for it to not report it. So when the above code ran, it returned two monitors as present even though one was off. Once it had the cable removed, it worked. – Tom Apr 14 '19 at 19:57
  • Yes, well, don't use that code. You really don't need to iterate all the screens and see whether the old position of your form was in one on them. You need to run your Main Form, see in what Screen is currently shown and adapt the coordinates you stored to the current (what I meant with *offset it*). That's all. Use one of the methods you can find in the sample I linked. Or one of those I posted here. – Jimi Apr 14 '19 at 20:09

0 Answers0