0

i have a C# application that should open a process on the second screen if there are more than one screen. I just cannot get it working. Any ideas?

Array screens = Screen.AllScreens;
if (screens.Length > 1)
{
    // open in the second monitor
}
else
{
    System.Diagnostics.Process.Start(InputData);
}

It should be the same data as in else, but just open this on the second monitor in the now empty if.

T.B Ygg
  • 116
  • 10
  • what type of application wpf or winforms – BRAHIM Kamel Jun 26 '14 at 08:58
  • it is a winforms application – T.B Ygg Jun 26 '14 at 09:00
  • Specify this in the startup info sent to the native API `CreateProcess`. And hope that the other program takes not. If not, find the handle to the main window after the program starts up and call `SetWindowPos` to move the window. But the best solution is to change the other program, the one that you are starting. – David Heffernan Jun 26 '14 at 09:00
  • Some helpful ideas for winform positioning: http://stackoverflow.com/questions/10733165/how-to-open-form2-on-the-second-monitor – PiotrWolkowski Jun 26 '14 at 09:02

3 Answers3

3

There is no standard way to specify on which screen the application will appear when you launch it. However, you could either:

  • handle that in the started app itself (if you have its code of course)
  • find the handle of the main window after the process has started, and move it to the second screen using Win32 interop
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
2

you can achieve this by changing your MainForm of the input process like the following

public Form1()
        {
            this.StartPosition =FormStartPosition.Manual;
            InitializeComponent();

            Screen sndSc = Screen.AllScreens[1];

            if (sndSc.Primary)
            {
                sndSc = Screen.AllScreens[0];            
            }
            this.Height = sndSc.WorkingArea.Height;
            this.Width = sndSc.WorkingArea.Width; 
            this.Location = sndSc.WorkingArea.Location;   
        }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
1

You should do the same thing you do when you position your window.

To display a window on any monitor, you should reposition it according to that monitor's coordinates.

Lets say you have two monitors, both of 1920x1080 resultion and they are arranged side by side. In this situation, to show a form on the second screen, you must set form's X coordinate (or Left property, my guess) more than 1920.

AgentFire
  • 8,944
  • 8
  • 43
  • 90