开发者

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

开发者 https://www.devze.com 2023-01-15 12:44 出处:网络
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 desk

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 s开发者_运维百科creen 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消