Search code examples
wpfwindowsmonitor

How can I open a new WPF windows in second monitor?


I'm trying to open a new WPF Window in a second monitor from a Windows located in the primary monitor. Following what I founded in other threads and discussions I wrote this:

 System.Windows.Forms.Screen s1 = System.Windows.Forms.Screen.AllScreens[1];
 System.Drawing.Rectangle r1 = s1.WorkingArea;
 form.WindowState = System.Windows.WindowState.Normal;
 form.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
 form.Top = r1.Top;
 form.Left = r1.Left;
 form.Show();
 form.WindowState = System.Windows.WindowState.Maximized;

but this code not work: the new window is shown in the primary monitor. How I can resolve this problem? Thanks.


Solution

  • The solution was to use r.X and r.Y instead of r.top and r.left:

    System.Windows.Forms.Screen s1 =    System.Windows.Forms.Screen.AllScreens[1];
    System.Drawing.Rectangle r1 = s1.WorkingArea;
    form.WindowState = System.Windows.WindowState.Normal;
    form.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
    form.Top = r1.Y;
    form.Left = r1.X;
    form.Show();
    form.WindowState = System.Windows.WindowState.Maximized;
    

    now it works.