简体   繁体   English

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

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

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

A simpler method than using the bounds is to use the Screen.FromControl() method. 比使用边界更简单的方法是使用Screen.FromControl()方法。 This is the same functionality that Windows uses. 这与Windows使用的功能相同。

Screen.FromControl(this)

will return the screen object for the screen that contains most of the form that you call it from. 将返回包含您调用它的大部分表单的屏幕的屏幕对象。

This should do the trick for you: 这应该是你的诀窍:

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

It will return the screen that has the majority of the form in it. 它将返回其中包含大部分表单的屏幕。 Alternativley, you can use Alternativley,你可以使用

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

to return the screen that has the top left corner of the form in it. 返回窗体左上角的屏幕。

I did notice that but I was hoping for something more elequent (from .net not from you.) So based on your advice I did this: 我确实注意到了,但是我希望有更多的东西(来自.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