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

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

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