views:

29

answers:

1

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)?

+1  A: 
/// <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;
}
Timwi