3 Replies - 16546 Views - Last Post: 27 July 2007 - 07:58 PM Rate Topic: -----

#1 hfield  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 07

Multiple Displays

Posted 03 July 2007 - 07:39 AM

I am trying to specify which display to open a form on. I have some test code that tells which display its' form is running on but when I click on "button2" I want to open 'form2' on the other display.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace testscreen
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(92, 44);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(76, 136);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(544, 95);
			this.listBox1.TabIndex = 1;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(228, 44);
			this.button2.Name = "button2";
			this.button2.TabIndex = 2;
			this.button2.Text = "button2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(372, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(260, 68);
			this.label1.TabIndex = 3;
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(668, 262);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Locationchanged += new System.EventHandler(this.Form1_Locationchanged);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			int index;
			int upperBound; 

			// Gets an array of all the screens connected to the system.

			Screen [] screens = Screen.AllScreens;
			upperBound = screens.GetUpperBound(0);

			for(index = 0; index <= upperBound; index++)
			{

				// For each screen, add the screen properties to a list box.

				listBox1.Items.Add("Device Name: " + screens[index].DeviceName);
				listBox1.Items.Add("Bounds: " + screens[index].Bounds.ToString());
				listBox1.Items.Add("Type: " + screens[index].GetType().ToString());
				listBox1.Items.Add("Working Area: " + screens[index].WorkingArea.ToString());
				listBox1.Items.Add("Primary Screen: " + screens[index].Primary.ToString());
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			int index;
			int upperBound;

			Screen[] screens = Screen.AllScreens;
			if (screens.Length == 2)
			{
				Point pt = new Point(1281,0);
				if (Screen.FromControl(this).DeviceName == @"\\.\DISPLAY1")
					return;
				Form myform = new Form2();
//				myform.StartPosition =  pt; //.DesktopLocation = pt;
				myform.Show();
			}
		}

		private void Form1_Locationchanged(object sender, System.EventArgs e)
		{
			Screen[] screens = Screen.AllScreens;
			label1.Text = "There are "+screens.Length+" displays";
			label1.Text += "\n" + Screen.FromControl(this).DeviceName;
		}
	}
}



This is just a sample app for me to learn how to do this. I have read that with 2 monitors running a 1280x1024, I should be able to open the window at (1281,0) and it would be on the second display. But, I don't know how to specify that as the top left corner of its window when I open 'form2' (myform in the program above).

Thanks for any suggestions.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Displays

#2 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Multiple Displays

Posted 03 July 2007 - 03:56 PM

Isn't it just

myForm.Top = 0;
myForm.Left = 1281;



--serializer
Was This Post Helpful? 0
  • +
  • -

#3 deng_mam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-July 07

Re: Multiple Displays

Posted 05 July 2007 - 07:10 AM

feck deng mam
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Multiple Displays

Posted 27 July 2007 - 07:58 PM

Showing something on the 2nd monitor is simple, just show it in x-y coordinate of the 2nd monitor. Assuming your primary monitor has a resolution of 1024768, then the 2nd monitor starts from 1024,0 to 2048,768, that is if both screens have the same resolution.

Use Screen.AllScreens.Length to find out how many monitors the system has. If its greater 1, use 0 for the primary monitor and 1 for the 2nd monitor (2 for the 3rd monitor, etc).

To get the Bounds of the 2nd monitor use Screens.AllScreens[1].Bounds. This will give the coordinates of the 2nd monitor. Set your Form.Bounds to it and your stuff will appear on the 2nd monitor.

Hope this helps a little :)
Was This Post Helpful? 1

Page 1 of 1