Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine which monitor my winform is in?

I have been up and down this site and found a lot of info on the Screen class and how to count the number of monitors and such but how do I determine which montitor a form is currently in?

like image 587
jvberg Avatar asked Dec 02 '08 18:12

jvberg


3 Answers

A simpler method than using the bounds is to use the Screen.FromControl() method. This is the same functionality that Windows uses.

Screen.FromControl(this)

will return the screen object for the screen that contains most of the form that you call it from.

like image 109
Dan R Avatar answered Oct 23 '22 12:10

Dan R


This should do the trick for you:

private Screen FindCurrentMonitor(Form form) 
{ 
    return Windows.Forms.Screen.FromRectangle(new Rectangle( _
        form.Location, form.Size)); 
} 

It will return the screen that has the majority of the form in it. Alternativley, you can use

return Windows.Forms.Screen.FromPoint(Form.Location);

to return the screen that has the top left corner of the form in it.

like image 31
Matt Hanson Avatar answered Oct 23 '22 11:10

Matt Hanson


I did notice that but I was hoping for something more elequent (from .net not from you.) So based on your advice I did this:

    foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
    {
        if (screen.Bounds.Contains(this.Location))
        {
            this.textBox1.Text = screen.DeviceName;
        }
    }
like image 1
jvberg Avatar answered Oct 23 '22 12:10

jvberg