C# 如何确定我的 .NET Windows 窗体程序在哪个监视器上运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1121600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I determine which monitor my .NET Windows Forms program is running on?
提问by Stephen Fletcher
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.
编辑:谢谢大家的回复!我想确定窗口在哪个监视器上,以便我可以进行适当的边界检查,以防用户不小心将窗口放在查看区域之外或更改了屏幕大小,从而使表单不再完全可见。
采纳答案by Stan R.
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 (thisis the windows form you are on)
您还可以通过运行此代码(这是您所在的窗口窗体)来确定您在哪个屏幕上
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 链接,没有太多。我建议自己在代码中乱搞。
回答by Jon Tackabury
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 并且您将窗口的左侧位置设置为 2000 像素,它将出现在第二台显示器上(假设第二台显示器在第一台显示器的右侧。):)
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!
只需传入您窗口的所需位置,它就会告诉您它是否会在其中一个屏幕上可见。享受!
回答by Jon Tackabury
You can use the 'Screen' object: System.Windows.Forms.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
它可能会为您提供所需的东西
回答by Henk Holterman
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.
this
表单(或表单上的任何控件)在哪里。至于如何记住这有点棘手,但我会选择 Screen.AllScreens 数组中的索引,或者 s.DeviceName。无论哪种情况,请在启动时使用设置之前进行检查,以防止使用已断开连接的显示器。
回答by Chap
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);