如何轻松地找到在多显示器环境形式位置的屏幕位置? [英] How to easily find screen location of form Location in multi-monitor environment?

查看:217
本文介绍了如何轻松地找到在多显示器环境形式位置的屏幕位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在在多显示器环境中运行,(台式机是在2个或3台显示器拉伸)一个C#WinForm应用程序,一个表单的位置属性表示形式的跨区桌面,而不是形式的位置上的物理上的位置屏幕。有一种简单的方法来找到在屏幕坐标形式的位置,该屏幕的形式呢?因此,如果形式是在第二或第三显示屏的左上角位置是(0,0)?


解决方案

  ///&所述;总结>返回相对于屏幕的左上角
///形式包含的所述左上角的位置形式,或NULL,如果形式的
///左上角是关屏< /总结>
公共点? GetLocationWithinScreen(表格形式)
{
的foreach(在Screen.AllScreens屏幕屏幕)
如果(screen.Bounds.Contains(form.Location))
返回新点(形式。 Location.X - screen.Bounds.Left,
form.Location.Y - screen.Bounds.Top);

返回NULL;
}


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

这篇关于如何轻松地找到在多显示器环境形式位置的屏幕位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆