Screen class
The following code example demonstrates how to use various methods and attributes of the screen class. This example calls the allscreens attribute to retrieve the array of all screens connected to the system. For each returned screen, this example adds the device name, border, type, workspace, and home screen to ListBox.
1 private void button1_Click(object sender, System.EventArgs e) 2 { 3 int index; 4 int upperBound; 5 6 // Gets an array of all the screens connected to the system. 7 8 9 Screen [] screens = Screen.AllScreens;10 upperBound = screens.GetUpperBound(0);11 12 for(index = 0; index <= upperBound; index++)13 {14 15 // For each screen, add the screen properties to a list box.16 17 18 listBox1.Items.Add("Device Name: " + screens[index].DeviceName);19 listBox1.Items.Add("Bounds: " + screens[index].Bounds.ToString());20 listBox1.Items.Add("Type: " + screens[index].GetType().ToString());21 listBox1.Items.Add("Working Area: " + screens[index].WorkingArea.ToString());22 listBox1.Items.Add("Primary Screen: " + screens[index].Primary.ToString());23 24 }25 26 }
The so-called split screen or multi-screen software is to run multiple forms in the software on the main screen, but move each form (coordinate) to the position of each extended screen, as shown in:
Home screen (Mainform) Index = 0 |
Extended Screen 1 (Form1) Index = 1 |
Extended screen 2 (Form2) Index =... |
Extended Screen 3 (Form3) Index =... |
The following describes the most common dual-screen display, that is, the screen display in left and right mode.
How to Implement winform:
By using the screen class in winform, You can conveniently display multiple forms on multiple screens.
- Obtain the number of screens connected to the current system: screen. allscreens. Count ();
- Get the name of the current screen: String currentscreenname = screen. fromcontrol (this). devicename;
- Get the current Screen Object: screen currentscreen = screen. fromcontrol (this );
- Screen currentscreen = screen. frompoint (new point (cursor. position. X, cursor. position. y ));
- Show the form on 2nd screens:
This. Left = (screen. allscreens [1]. bounds. Width-This. width)/2 );
This. Top = (screen. allscreens [1]. bounds. Height-This. Height)/2); display any form on any screen: winform:
1 Screen[] sc;2 sc = Screen.AllScreens;3 this.StartPosition = FormStartPosition.Manual;4 this.Location = new Point(sc[1].Bounds.Left, sc[1].Bounds.Top);5 // If you intend the form to be maximized, change it to normal then maximized.6 this.WindowState = FormWindowState.Normal;7 this.WindowState = FormWindowState.Maximized;8 MessageBox.Show(this.Handle.ToString());
WPF:
1 protected override void OnStartup(StartupEventArgs e) 2 { 3 base.OnStartup(e); 4 5 Window1 w1 = new Window1(); 6 Window2 w2 = new Window2(); 7 8 9 Screen s1 = Screen.AllScreens[0];10 Screen s2 = Screen.AllScreens[1];11 12 Rectangle r1 = s1.WorkingArea;13 Rectangle r2 = s2.WorkingArea;14 15 w1.Top = r1.Top;16 w1.Left = r1.Left;17 18 w2.Top = r2.Top;19 w2.Left = r2.Left;20 21 w1.Show();22 w2.Show();23 24 w2.Owner = w1;25 26 27 }