如何在多显示器环境中轻松找到表单位置的屏幕位置?

时间:2010-09-08 13:18:05

标签: c# winforms multiple-monitors

在多监视器环境中运行的C#winform应用程序(桌面跨越2或3个监视器),Form的Location属性表示表单在跨区桌面上的位置,而不是表单在物理上的位置屏幕。是否有一种简单的方法可以在屏幕坐标中找到表单的位置,以获取表单所在的屏幕?因此,如果表单位于第2或第3个显示的左上角,则位置为(0,0)?

1 个答案:

答案 0 :(得分:4)

/// <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;
}