How to easily find the screen layout in the Layout form in a multi-monitor environment?

In a winform C # application running in a multi-monitor environment (the desktop is stretched across 2 or 3 monitors), the Location property of the form represents the location of the form on the expanded 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 the coordinates of the screen for the screen on which the form is located? So, if the form is in the upper left corner of the second or third display, then the location will be (0,0)?

+3
source share
1 answer
/// <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;
}
+3
source

Source: https://habr.com/ru/post/1763794/


All Articles