简体   繁体   中英

How to easily find screen location of form Location in multi-monitor environment?

In a C# winform application running in a multimonitor environment (desktop is stretched across 2 or 3 monitors), the Location property of a Form represents the location of the form on the spanned desktop instead of the location of the form on the physical screen. Is there an easy way to find the Location of the form in screen coordinates, for the screen that the form is on? So if the form is in the top left corner of the 2nd or 3rd display, the location would be (0,0)?

/// <summary>Returns the location of the form relative to the top-left corner
/// of the screen that contains the top-left corner of the form, or null if the
/// top-left corner of the form is off-screen.</summary>
public Point? GetLocationWithinScreen(Form form)
{
    foreach (Screen screen in Screen.AllScreens)
        if (screen.Bounds.Contains(form.Location))
            return new Point(form.Location.X - screen.Bounds.Left,
                             form.Location.Y - screen.Bounds.Top);

    return null;
}

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