简体   繁体   中英

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. This is the same functionality that Windows uses.

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

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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