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

时间:2023-01-28 09:33:23

I have been up and down this site and found a lot of info on the Screen class and how to count the number of monitors and such but how do I determine which montitor a form is currently in?

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

4 个解决方案

#1


14  

A simpler method than using the bounds is to use the Screen.FromControl() method. This is the same functionality that Windows uses.

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

Screen.FromControl(this)

will return the screen object for the screen that contains most of the form that you call it from.

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

#2


4  

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.

返回窗体左上角的屏幕。

#3


1  

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

#4


0  

Each Screen object has a Bounds property you can use to find the coordinates the screen occupies, just check where the form is.

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

#1


14  

A simpler method than using the bounds is to use the Screen.FromControl() method. This is the same functionality that Windows uses.

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

Screen.FromControl(this)

will return the screen object for the screen that contains most of the form that you call it from.

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

#2


4  

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.

返回窗体左上角的屏幕。

#3


1  

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

#4


0  

Each Screen object has a Bounds property you can use to find the coordinates the screen occupies, just check where the form is.

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