Introduction
This has been an ongoing topic for a while and I keep on getting question(s) on how can we launch Windows or in our case application(s) in different screens. In the place that I currently work, City of Dreams, we have many different types of adapters for many A/V systems and the applications like Lucky Draw, Slot Results, etc. have to be automatically launched on the correct AV to have effects like Animation(s) on one type, random numbers on other, etc.
Launcher
Launched
The AllScreens
This is all it takes to get the information of all the screens currently hosted in the system System.Windows.Forms.Screen
.
So to populate our dropdown; loop through the:
Screen[] scr = Screen.AllScreens
this.cmbDevices.Items.Clear();
foreach (System.Windows.Forms.Screen s in scr)
{
strDeviceName = s.DeviceName.Replace("\\\\.\\", "");
this.cmbDevices.Items.Add(strDeviceName);
if (s.Primary) this.cmbDevices.Items.Add(">" + strDeviceName);
else this.cmbDevices.Items.Add(strDeviceName);
}
this.cmbDevices.SelectedIndex = 0;
Launch
There are only two properties that have to be set correctly in order to ensure it is properly launched into the correct display. Form.StartPosition
& Form.Location
.
Screen[] scr = Screen.AllScreens
Form oForm = new Form()
oForm.Left = scr[0].Bounds.Width;
oForm.Top = scr[0].Bounds.Height;
oForm.StartPosition = FormStartPosition.Manual;
oForm.Location = scr[0].Bounds.Location;
oForm.Show();
Putting It All Together
Here we go, this is all it takes to launch your forms into user selected display.
public partial class frmMain : Form
{
Form[] aryForms = new Form[5]; int currentPointer = 0; Screen[] scr = Screen.AllScreens;
public frmMain()
{
InitializeComponent();
LoadScreen();
}
private void LoadScreen()
{
String strDeviceName = String.Empty;
this.cmbDevices.Items.Clear();
foreach (Screen s in scr)
{
strDeviceName = s.DeviceName.Replace("\\\\.\\", "");
this.cmbDevices.Items.Add(strDeviceName);
}
this.cmbDevices.SelectedIndex = 0;
}
private void btnLaunchIn_Click(object sender, EventArgs e)
{
int intLaunchScreen = getScreenNumber(this.cmbDevices.SelectedItem.ToString());
if (currentPointer <= 4)
{
aryForms[currentPointer] = new frmLaunchedWindow();
aryForms[currentPointer].Text =
aryForms[currentPointer].Text + currentPointer;
aryForms[currentPointer].Left = scr[intLaunchScreen].Bounds.Width;
aryForms[currentPointer].Top = scr[intLaunchScreen].Bounds.Height;
aryForms[currentPointer].StartPosition = FormStartPosition.Manual;
aryForms[currentPointer].Location = scr[intLaunchScreen].Bounds.Location;
aryForms[currentPointer].Show();
currentPointer += 1;
}
}
private int getScreenNumber(String DeviceID)
{
int i = 0;
foreach (Screen s in scr)
{
if (s.DeviceName == "\\\\.\\"+DeviceID) return i;
i += 1;
}
return 0;
}
}
History
- 13th September, 2010: Initial post