Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / Win32

Launch Application Windows in MultiScreen

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
13 Sep 2010CPOL 30.6K   700   25   2
Launch Application Forms in MultiScreen

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

MultiScreen01.JPG

Launched

MultiScreen02.JPG

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:

C#
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);
    // you can check the device is primary or not this way
    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.

C#
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.

C#
public partial class frmMain : Form
{
    Form[] aryForms = new Form[5];                      // Allow 5 total forms.
    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);
            /* Enable this section, if you want to point to 
	   user this is the default screen.
            if (s.Primary) this.cmbDevices.Items.Add(">" + strDeviceName);
            else 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;
            //Point p = new Point(scr[0].Bounds.Location.X, scr[0].Bounds.Location.Y);
            //aryForms[currentPointer].Location = p;
            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;
        }
        // if cannot find the device reset to the default 0
        return 0;
    }
}

History

  • 13th September, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AdhiMarg Technologies Ltd.
Hong Kong Hong Kong
Resident of Hong Kong, TOGAF 8 certified architect with over 15 years of international experience in the IT industry. Exceptional exposure to the marine / shipping technologies and knowledge of large scale database system architecture, enterprise systems design and work flow implementation.

Comments and Discussions

 
GeneralNice, but short Pin
BillW3314-Sep-10 14:04
professionalBillW3314-Sep-10 14:04 
GeneralToo small for an article Pin
Dmitri Nеstеruk13-Sep-10 21:02
Dmitri Nеstеruk13-Sep-10 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.