如何在C#中找到运行应用程序的屏幕

时间:2022-09-20 20:56:59

How do I determine what screen my application is running on?

如何确定运行应用程序的屏幕?

4 个解决方案

#1


This should get you started. Get a Button and a listbox on a Form and put this in the Button_Click:

这应该让你开始。在窗体上获取一个Button和一个列表框,并将其放在Button_Click中:

listBox1.Items.Clear();
foreach (var screen in Screen.AllScreens)
{
    listBox1.Items.Add(screen);
}
listBox1.SelectedItem = Screen.FromControl(this);            

The answer is in the last line, remember that a Form is a Control too.

答案是在最后一行,记住Form也是一个控件。

#2


The System.Windows.Forms.Screen class provides this functionaility.

System.Windows.Forms.Screen类提供此功能。

For example:

Screen s = Screen.FromPoint(p);

屏幕s = Screen.FromPoint(p);

where p is a Point somewhere on your application (in screen coordinates).

其中p是应用程序某处的Point(在屏幕坐标中)。

#3


Hmm, I don't think there is a built in way to get this, but it shouldn't be too hard to determine. Use the Screen class to find all the screens, loop through that list and compare its bounds with the location of the form.

嗯,我认为没有一种内置的方法可以实现这一点,但不应该太难确定。使用Screen类查找所有屏幕,遍历该列表并将其边界与表单的位置进行比较。

Here is some untested code

这是一些未经测试的代码

Screen [] screens = Screen.AllScreens;

for(index = 0; index < screens.Length; index++) {
     if (screens[index].Contains(this.Bounds))
        return screens[index];
}

#4


Take a look at these links:

看看这些链接:

These are in WinAPI. There may be .NET multiple monitor libraries/api calls, but if not, with these you can write your own.

这些是在WinAPI中。可能有.NET多个监视器库/ api调用,但如果没有,使用这些可以编写自己的。

#1


This should get you started. Get a Button and a listbox on a Form and put this in the Button_Click:

这应该让你开始。在窗体上获取一个Button和一个列表框,并将其放在Button_Click中:

listBox1.Items.Clear();
foreach (var screen in Screen.AllScreens)
{
    listBox1.Items.Add(screen);
}
listBox1.SelectedItem = Screen.FromControl(this);            

The answer is in the last line, remember that a Form is a Control too.

答案是在最后一行,记住Form也是一个控件。

#2


The System.Windows.Forms.Screen class provides this functionaility.

System.Windows.Forms.Screen类提供此功能。

For example:

Screen s = Screen.FromPoint(p);

屏幕s = Screen.FromPoint(p);

where p is a Point somewhere on your application (in screen coordinates).

其中p是应用程序某处的Point(在屏幕坐标中)。

#3


Hmm, I don't think there is a built in way to get this, but it shouldn't be too hard to determine. Use the Screen class to find all the screens, loop through that list and compare its bounds with the location of the form.

嗯,我认为没有一种内置的方法可以实现这一点,但不应该太难确定。使用Screen类查找所有屏幕,遍历该列表并将其边界与表单的位置进行比较。

Here is some untested code

这是一些未经测试的代码

Screen [] screens = Screen.AllScreens;

for(index = 0; index < screens.Length; index++) {
     if (screens[index].Contains(this.Bounds))
        return screens[index];
}

#4


Take a look at these links:

看看这些链接:

These are in WinAPI. There may be .NET multiple monitor libraries/api calls, but if not, with these you can write your own.

这些是在WinAPI中。可能有.NET多个监视器库/ api调用,但如果没有,使用这些可以编写自己的。