C# Screen 類(多螢幕開發)

來源:互聯網
上載者:User

標籤:winform   style   class   blog   code   http   

Screen 類

下面的程式碼範例示範如何使用 Screen 類的各種方法和屬性。 該樣本調用 AllScreens 屬性來檢索串連到系統的所有螢幕的數組。 對於每個返回的 Screen,該樣本將裝置名稱、邊界、類型、工作區和主畫面添加到 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   }

所謂的分屏或多屏軟體,就是把軟體中的多個表單,在主畫面運行,但是把各個表單(座標)移動到各個擴充螢幕位置上如所示:

 

主畫面
(MainForm)
index=0
擴充螢幕1
(Form1)
index=1
擴充螢幕2
(Form2)
index=...
擴充螢幕3
(Form3)
index=...

 

 

以下介紹最常用的雙螢幕顯示,也就是左右模式的螢幕顯示的方法。

WinForm 的實現辦法:

利用WinForm中的Screen類,即可比較方便地實現多表單分別在多個螢幕上顯示。

 

  • 擷取當前系統串連的螢幕數量: Screen.AllScreens.Count();
  • 擷取當前螢幕的名稱:string CurrentScreenName = Screen.FromControl(this).DeviceName;
  • 擷取當前螢幕對象:Screen CurrentScreen = Screen.FromControl(this);
  • 擷取當前滑鼠所在的螢幕:Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
  • 讓表單在第2個螢幕上顯示:
     this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
     this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);把任何表單顯示在任何螢幕的方法: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         }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.