如何确定运行.NET Windows Forms程序的监视器?

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

I have a C# Windows application that I want to ensure will show up on a second monitor if the user moves it to one. I need to save the main form's size, location and window state - which I've already handled - but I also need to know which screen it was on when the user closed the application.

我有一个C#Windows应用程序,如果用户将其移动到第二个监视器上,我希望该应用程序将显示在第二个监视器上。我需要保存主窗体的大小,位置和窗口状态 - 我已经处理过了 - 但是当用户关闭应用程序时,我还需要知道它所在的屏幕。

I'm using the Screen class to determine the size of the current screen but I can't find anything on how to determine which screen the application was running on.

我正在使用Screen类来确定当前屏幕的大小,但我找不到任何关于如何确定运行应用程序的屏幕的信息。

Edit: Thanks for the responses, everyone! I wanted to determine which monitor the window was on so I could do proper bounds checking in case the user accidentally put the window outside the viewing area or changed the screen size such that the form wouldn't be completely visible anymore.

编辑:感谢您的回复,大家好!我想确定窗口所在的监视器,以便我可以进行适当的边界检查,以防用户意外地将窗口放在查看区域之外或更改屏幕大小,使得窗体不再完全可见。

5 个解决方案

#1


46  

You can get an array of Screens that you have using this code.

您可以使用此代码获取一系列屏幕。

Screen[] screens = Screen.AllScreens;

You can also figure out which screen you are on, by running this code (this is the windows form you are on)

您还可以通过运行此代码找出您所在的屏幕(这是您所使用的Windows窗体)

Screen screen = Screen.FromControl(this); //this is the Form class

in short check out the Screen class and static helper methods, they might help you.

简而言之,请查看Screen类和静态帮助器方法,它们可能对您有所帮助。

MSDN Link, doesn't have much..I suggest messing around in the code by yourself.

MSDN链接,没有多少..我建议你自己搞乱代码。

#2


6  

If you remember the window's location and size, that will be enough. When you set the position to the previously used position, if it happened to be on the second monitor it will go back there.

如果您还记得窗口的位置和大小,那就足够了。当您将位置设置为先前使用的位置时,如果它恰好位于第二个监视器上,它将返回到那里。

For example, if you have 2 monitors, both sized 1280x1024 and you set your window's left position to be 2000px, it will appear on the second monitor (assuming the second monitor is to the right of the first.) :)

例如,如果您有2个显示器,两个都是1280x1024,并且您将窗口的左侧位置设置为2000px,它将出现在第二个显示器上(假设第二个显示器位于第一个显示器的右侧。):)

If you are worried about the second monitor not being there when the application is started the next time, you can use this method to determine if your window intersects any of the screens:

如果您担心下次启动应用程序时第二台显示器不在那里,您可以使用此方法确定您的窗口是否与任何屏幕相交:

private bool isWindowVisible(Rectangle rect)
{
    foreach (Screen screen in Screen.AllScreens)
    {
        if (screen.Bounds.IntersectsWith(rect))
            return true;
    }
    return false;
}

Just pass in your window's desired location and it will tell you if it will be visible on one of the screens. Enjoy!

只需传入窗口所需的位置,它就会告诉您它是否会在其中一个屏幕上显示。请享用!

#3


4  

You can get the current Screen with

你可以获得当前的屏幕

var s = Screen.FromControl(this);

where this is the Form (or any control on the Form). As about how to remember that is a little tricky, but I would go for the index in the Screen.AllScreens array, or maybe s.DeviceName. In either case, check before using the settings on startup, to prevent using a monitor that was disconnected.

这是表格(或表格上的任何控件)。至于如何记住这有点棘手,但我会选择Screen.AllScreens数组中的索引,或者s.DeviceName。在任何一种情况下,请在启动时使用设置之前进行检查,以防止使用已断开连接的监视器。

#4


3  

The location of the form will tell you which screen the form is on. I don't really understand why you'd need to know what screen it is on, because if you restore it using the location you saved it should just restore to the same location (maybe you can expand as to why).

表单的位置将告诉您表单所在的屏幕。我真的不明白为什么你需要知道它是什么屏幕,因为如果你使用你保存的位置恢复它,它应该只恢复到相同的位置(也许你可以扩展为什么)。

Otherwise you can do something like this:

否则你可以这样做:

Screen[] scr = Screen.AllScreens;
scr[i].Bounds.IntersectsWith(form.Bounds);

Each screen has a Bounds property which returns a Rectangle. You can use the IntersectsWith() function to determine if the form is within the screen.

每个屏幕都有一个Bounds属性,它返回一个Rectangle。您可以使用IntersectsWith()函数来确定表单是否在屏幕内。

Also, they basically provide a function that does this as well on the Screen class

此外,它们基本上提供了一个在Screen类中执行此操作的功能

Screen screen = Screen.FromControl(form);

#5


0  

You can use the 'Screen' object: System.Windows.Forms.Screen

您可以使用'Screen'对象:System.Windows.Forms.Screen

Start playing with something like this:

开始玩这样的事情:

        Screen[] screens = Screen.AllScreens;
        for (int i = 0; i < screens.Length ; i++)
        {
            Debug.Print(screens[i].Bounds.ToString());
            Debug.Print(screens[i].DeviceName);
            Debug.Print(screens[i].WorkingArea.ToString());
        }

It may get you what you need

它可以为您提供所需的产品

#1


46  

You can get an array of Screens that you have using this code.

您可以使用此代码获取一系列屏幕。

Screen[] screens = Screen.AllScreens;

You can also figure out which screen you are on, by running this code (this is the windows form you are on)

您还可以通过运行此代码找出您所在的屏幕(这是您所使用的Windows窗体)

Screen screen = Screen.FromControl(this); //this is the Form class

in short check out the Screen class and static helper methods, they might help you.

简而言之,请查看Screen类和静态帮助器方法,它们可能对您有所帮助。

MSDN Link, doesn't have much..I suggest messing around in the code by yourself.

MSDN链接,没有多少..我建议你自己搞乱代码。

#2


6  

If you remember the window's location and size, that will be enough. When you set the position to the previously used position, if it happened to be on the second monitor it will go back there.

如果您还记得窗口的位置和大小,那就足够了。当您将位置设置为先前使用的位置时,如果它恰好位于第二个监视器上,它将返回到那里。

For example, if you have 2 monitors, both sized 1280x1024 and you set your window's left position to be 2000px, it will appear on the second monitor (assuming the second monitor is to the right of the first.) :)

例如,如果您有2个显示器,两个都是1280x1024,并且您将窗口的左侧位置设置为2000px,它将出现在第二个显示器上(假设第二个显示器位于第一个显示器的右侧。):)

If you are worried about the second monitor not being there when the application is started the next time, you can use this method to determine if your window intersects any of the screens:

如果您担心下次启动应用程序时第二台显示器不在那里,您可以使用此方法确定您的窗口是否与任何屏幕相交:

private bool isWindowVisible(Rectangle rect)
{
    foreach (Screen screen in Screen.AllScreens)
    {
        if (screen.Bounds.IntersectsWith(rect))
            return true;
    }
    return false;
}

Just pass in your window's desired location and it will tell you if it will be visible on one of the screens. Enjoy!

只需传入窗口所需的位置,它就会告诉您它是否会在其中一个屏幕上显示。请享用!

#3


4  

You can get the current Screen with

你可以获得当前的屏幕

var s = Screen.FromControl(this);

where this is the Form (or any control on the Form). As about how to remember that is a little tricky, but I would go for the index in the Screen.AllScreens array, or maybe s.DeviceName. In either case, check before using the settings on startup, to prevent using a monitor that was disconnected.

这是表格(或表格上的任何控件)。至于如何记住这有点棘手,但我会选择Screen.AllScreens数组中的索引,或者s.DeviceName。在任何一种情况下,请在启动时使用设置之前进行检查,以防止使用已断开连接的监视器。

#4


3  

The location of the form will tell you which screen the form is on. I don't really understand why you'd need to know what screen it is on, because if you restore it using the location you saved it should just restore to the same location (maybe you can expand as to why).

表单的位置将告诉您表单所在的屏幕。我真的不明白为什么你需要知道它是什么屏幕,因为如果你使用你保存的位置恢复它,它应该只恢复到相同的位置(也许你可以扩展为什么)。

Otherwise you can do something like this:

否则你可以这样做:

Screen[] scr = Screen.AllScreens;
scr[i].Bounds.IntersectsWith(form.Bounds);

Each screen has a Bounds property which returns a Rectangle. You can use the IntersectsWith() function to determine if the form is within the screen.

每个屏幕都有一个Bounds属性,它返回一个Rectangle。您可以使用IntersectsWith()函数来确定表单是否在屏幕内。

Also, they basically provide a function that does this as well on the Screen class

此外,它们基本上提供了一个在Screen类中执行此操作的功能

Screen screen = Screen.FromControl(form);

#5


0  

You can use the 'Screen' object: System.Windows.Forms.Screen

您可以使用'Screen'对象:System.Windows.Forms.Screen

Start playing with something like this:

开始玩这样的事情:

        Screen[] screens = Screen.AllScreens;
        for (int i = 0; i < screens.Length ; i++)
        {
            Debug.Print(screens[i].Bounds.ToString());
            Debug.Print(screens[i].DeviceName);
            Debug.Print(screens[i].WorkingArea.ToString());
        }

It may get you what you need

它可以为您提供所需的产品