简体   繁体   English

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

[英]How do I determine which monitor my .NET Windows Forms program is running on?

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

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. 我想确定窗口所在的监视器,以便我可以进行适当的边界检查,以防用户意外地将窗口放在查看区域之外或更改屏幕大小,使得窗体不再完全可见。

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链接 ,没有多少..我建议你自己搞乱代码。

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! 请享用!

You can get the current Screen with 你可以获得当前的屏幕

var s = Screen.FromControl(this);

where this is the Form (or any control on the Form). this是表格(或表格上的任何控件)。 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. 至于如何记住这有点棘手,但我会选择Screen.AllScreens数组中的索引,或者s.DeviceName。 In either case, check before using the settings on startup, to prevent using a monitor that was disconnected. 在任何一种情况下,请在启动时使用设置之前进行检查,以防止使用已断开连接的监视器。

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. 每个屏幕都有一个Bounds属性,返回一个Rectangle。 You can use the IntersectsWith() function to determine if the form is within the screen. 您可以使用IntersectsWith()函数来确定表单是否在屏幕内。

Also, they basically provide a function that does this as well on the Screen class 此外,它们基本上提供了一个在Screen类中执行此操作的功能

Screen screen = Screen.FromControl(form);

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 它可以满足您的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测 ASP.NET 程序在哪个监视器上运行? - How do I detect which monitor an ASP.NET program is running on? 如何确定winform所在的监视器? - How do I determine which monitor my winform is in? 如何在 .NET 中确定我的监视器的真实像素大小? - How do I determine the true pixel size of my Monitor in .NET? 如何确定 Windows Mobile 中的程序中断 - How do I determine program interupt in Windows Mobile 如何在没有可见窗口的情况下启动.net Windows窗体应用程序? - How do I launch .net windows forms application with no visible windows? 运行查询时,Windows 窗体程序突然关闭。 如何在错误停止/崩溃之前捕获错误? - Windows Forms program suddenly closes when running a query. How do I catch the error before it stops/crashes? 如何允许我的.net程序在运行时编译C#代码? - How do I allow my .net program to compile C# code while running? 我的程序如何确定它是在32位还是64位Windows上运行? - How can my program determine if it is running on 32-bit or 64-bit Windows? 如何确定我的计算机正在使用哪个网络适配器? - How do I determine which network adapter my computer is using? 如何使用第三方程序、PowerShell 或 C# 监视 Microsoft Outlook 以确定加载时间和相关因素? - How do I monitor Microsoft Outlook to determine load time and factors involved using a third party program, PowerShell, or C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM