繁体   English   中英

如何确定winform所在的监视器?

[英]How do I determine which monitor my winform is in?

我一直在这个网站上下,发现了很多关于Screen类的信息,以及如何计算监视器的数量等,但我如何确定一个表单目前在哪个监视器?

比使用边界更简单的方法是使用Screen.FromControl()方法。 这与Windows使用的功能相同。

Screen.FromControl(this)

将返回包含您调用它的大部分表单的屏幕的屏幕对象。

这应该是你的诀窍:

private Screen FindCurrentMonitor(Form form) 
{ 
    return Windows.Forms.Screen.FromRectangle(new Rectangle( _
        form.Location, form.Size)); 
} 

它将返回其中包含大部分表单的屏幕。 Alternativley,你可以使用

return Windows.Forms.Screen.FromPoint(Form.Location);

返回窗体左上角的屏幕。

我确实注意到了,但是我希望有更多的东西(来自.net而不是来自你。)所以基于你的建议,我这样做了:

    foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
    {
        if (screen.Bounds.Contains(this.Location))
        {
            this.textBox1.Text = screen.DeviceName;
        }
    }

每个Screen对象都有一个Bounds属性,您可以使用它来查找屏幕占用的坐标,只需检查表单的位置即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM