【发布时间】:2010-09-08 13:18:04
【问题描述】:
在多显示器环境中运行的 C# winform 应用程序中(桌面横跨 2 或 3 个显示器),Form 的 Location 属性表示窗体在跨区桌面上的位置,而不是窗体在物理上的位置屏幕。对于表单所在的屏幕,是否有一种简单的方法可以在屏幕坐标中找到表单的位置?那么如果表单在第二个或第三个显示的左上角,位置应该是(0,0)?
【问题讨论】:
标签: c# winforms multiple-monitors
在多显示器环境中运行的 C# winform 应用程序中(桌面横跨 2 或 3 个显示器),Form 的 Location 属性表示窗体在跨区桌面上的位置,而不是窗体在物理上的位置屏幕。对于表单所在的屏幕,是否有一种简单的方法可以在屏幕坐标中找到表单的位置?那么如果表单在第二个或第三个显示的左上角,位置应该是(0,0)?
【问题讨论】:
标签: c# winforms multiple-monitors
/// <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;
}
【讨论】: