我现在的开发电脑有两个显示器,两个显示器的分辨率是一样的,一个主显示器,一个附加显示器。
Q:请问,我想让我程序 Run 起来之后,Form 显示在指定的显示器屏幕上,而这个屏幕也是 Visual Studio 所在屏幕,请问有什么好的方法?谢谢。显示器分配率指定屏幕

解决方案 »

  1.   

    设置Form的Location,比如:
    public Form1()
    {
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width + 100, 0);
    }
      

  2.   

    this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
    this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);获取显示屏数量,计算location。
      

  3.   


    我的意思是:我需要显示在和 Visual Studio 同一个 Screen 当中!
    这里的代码是指定放到哪个显示器去,但是问题是我怎么确定这个显示器就是我 Visual Studio 所在的显示器?
      

  4.   


    随意指定哪个 Screen 我知道怎么做,即便遍历 Screens 都行,但是哪个显示器是我应该要的(Visual Studio 所在的)我无法确定。
      

  5.   

    你可以问“请问如何确定Visual Studio所在的显示器?”private void button1_Clicked(object sender, EventArgs e)
    {
        var visualStudio = Process.GetProcessesByName("devenv").FirstOrDefault();
        Rectangle rect = new Rectangle();
        if (visualStudio != null && GetWindowRect(visualStudio.MainWindowHandle, ref rect))
        {
            MessageBox.Show("visual studio @" + rect);
        }
    }
    [DllImport("User32")]
    static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);当然可能有多个Visual Studio在运行,也可能一个VS跨越好几个显示器。