How do I determine which monitor my winform is in?

蹲街弑〆低调 提交于 2019-12-22 03:52:52

问题


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?


回答1:


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.




回答2:


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.




回答3:


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;
        }
    }



回答4:


Each Screen object has a Bounds property you can use to find the coordinates the screen occupies, just check where the form is.



来源:https://stackoverflow.com/questions/334928/how-do-i-determine-which-monitor-my-winform-is-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!