WPF使用扩展屏幕
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ScreenShadow { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Window1 window = new Window1(); window.Show(); } private void Button_Click_1(object sender, RoutedEventArgs e) { Window2 window = new Window2(); window.Show(); } List<Window> openWindows = new List<Window>(); private void Button_Click_2(object sender, RoutedEventArgs e) { openWindows.Clear(); Screen[] screens = Screen.AllScreens; if (screens.Length == 1) { lblmsg.Content = "已经投射1个显示器"; } else if (screens.Length == 2) { lblmsg.Content = "已经投射2个显示器"; } else if (screens.Length == 4) { lblmsg.Content = "已经投射4个显示器"; } else { lblmsg.Content = "目前仅支持1,2,4个显示器模式"; } //主屏幕显示 Screen mainScreen = screens.FirstOrDefault(x=>x.Primary==true); //主屏幕显示window1 Window1 win1 = new Window1(); win1.WindowState = WindowState.Maximized; win1.WindowStartupLocation = WindowStartupLocation.Manual; System.Drawing.Rectangle mswa = mainScreen.WorkingArea; win1.Left = mswa.Left; win1.Top = mswa.Top; win1.Width = mswa.Width; win1.Height = mswa.Height; openWindows.Add(win1); win1.Show(); //其他屏幕显示,这里假设有2个 var subScreen = (from o in screens where o.Primary == false select o).ToList<Screen>(); if (subScreen.Count > 0) { var subscreen1 = subScreen[0]; Window2 win2 = new Window2(); win1.WindowState = WindowState.Maximized; win1.WindowStartupLocation = WindowStartupLocation.Manual; System.Drawing.Rectangle mswa2 = subscreen1.WorkingArea; win2.Left = mswa2.Left; win2.Top = mswa2.Top; win2.Width = mswa2.Width; win2.Height = mswa2.Height; openWindows.Add(win2); win2.Show(); } } private void BtnClose_Click(object sender, RoutedEventArgs e) { if (openWindows.Count > 0) { foreach (var item in openWindows) { item.Close(); } } } } }