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

前端 未结 5 558
一生所求
一生所求 2020-12-11 06:05

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

相关标签:
5条回答
  • 2020-12-11 06:46

    Take a look at these links:

    • MFC program for multiple monitors
    • Original MSDN article for multiple monitors

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

    0 讨论(0)
  • 2020-12-11 06:58

    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.

    0 讨论(0)
  • 2020-12-11 07:00

    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];
    }
    
    0 讨论(0)
  • 2020-12-11 07:08

    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).

    0 讨论(0)
  • 2020-12-11 07:10

    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);
    }
    
    0 讨论(0)
提交回复
热议问题