How do I find what screen the application is running on in C#
Asked Answered
S

5

6

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

Sangsanger answered 14/2, 2009 at 20:34 Comment(0)
P
6

This should get you started. Get a Button and a listbox on a Form and put this in the 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.

Pain answered 14/2, 2009 at 20:53 Comment(0)
L
1

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

For example:

Screen s = Screen.FromPoint(p);

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

Lacerate answered 14/2, 2009 at 20:52 Comment(0)
H
1

Well, many years have passed, and it is derivative of the accepted answer, but this worked for me. This method is a member of the Form class. The screen variable contains the properties of the screen that the upper left corner of the form is on when the method is called.

private void ClsFormFoo_Load(object sender, EventArgs e)
{
    Screen screen = Screen.FromControl(this);
}
Hulky answered 23/7, 2019 at 14:41 Comment(0)
D
0

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.

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];
}
Dramamine answered 14/2, 2009 at 20:51 Comment(1)
screens.Contains() -is what you are after and if so you dont need to look though them....Exact
D
0

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.

Diu answered 14/2, 2009 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.