简体   繁体   中英

How can I tell if my form is displayed beyond the screen?

So I have two forms, mainform and extraform.
extraform is set always moved to the right of mainform when mainform initializes
Sometimes mainform takes up both monitors and extraform is pushed off the screen never to be seen again. I would like to prevent this if possible. How can I do so? It must support dual monitors, that may or may not have distance between them (ie screen 1 is 20px to the left of screen 2).

How can I do this?

You can use the Screen class to work out where your window is relative to the desktop. The Screen class has a FromRectangle method, so you can figure out which screen you should be positioning your Form on (by passing your form's Bounds property in).

Each Screen object has a Bounds property, which you can use to compare to the location and size of your window, and adjust them accordingly.

It depends what you want should happen when extraform is pushed beyond the bounds of the screen(s).

However, to find out whether or not it's being pushed off, it's quite simple using the System.Windows.Forms.Screens class. Then you can do bounds checking like so:

        foreach (var screen in Screen.AllScreens)
        {
            if(screen.Bounds.Contains(this.Bounds))
            {
                Console.WriteLine("Device "+screen.DeviceName+" contains form!");
            }
        }

Code assumes being in a form. Note that this code only prints that a screen contains the form if the form is completely contained on the screen. But this should be rather simple to fix, depending on your needs.

表单中的DesktopLocation属性也许可以为您提供有关它们发生的情况的线索

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM