WPF 多屏幕显示实现显示窗体到第二个实体屏幕上面

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.14.5
.NET 6.0
Prism Template Pack 2.4.1 本项目基于该扩展创建
Prism.DryIoc 8.1.97 项目依赖
MaterialDesignThemes 5.2.1 项目依赖
硬件/设备 版本 说明
屏幕1 优派24VX2478-4K-HD/3840*2160/200%缩放 主屏幕
屏幕2 科诺华(不确定尺寸,10寸左右)/1366*768/100%缩放

正文

主屏幕开启多个虚拟桌面后,不影响显示窗体显示到第二个实体屏幕上面

  1. 修改 【项目名】.csproj
     <PropertyGroup>
    	<!--添加这一行-->
    	<UseWindowsForms>true</UseWindowsForms>
      </PropertyGroup>
    
  2. 获取屏幕信息代码
    void GetAllScreens()
    {
    	// 获取所有屏幕
    	var screens = System.Windows.Forms.Screen.AllScreens;
    	foreach (var screen in screens)
    	{
    		Debug.WriteLine($"====屏幕信息====");
    		Debug.WriteLine($"设备名称: {screen.DeviceName}");
    		Debug.WriteLine($"是否主屏幕: {screen.Primary}");
    		Debug.WriteLine($"WorkingArea: {screen.WorkingArea}");
    		Debug.WriteLine($"Bounds: {screen.Bounds}");
    	}
    }
    
  3. 显示到第二个屏幕窗体的代码
    using System.Linq;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace WPFDualScreenDemo.Views
    {
    	/// <summary>
    	/// Window1.xaml 的交互逻辑
    	/// </summary>
    	public partial class Window1 : Window
    	{
    		public Window1()
    		{
    			InitializeComponent();
    			this.Loaded += (s, e) =>
    			{
    				// https://blog.csdn.net/msst1234/article/details/76145959
    				if (Screen.AllScreens.Length > 1)
    				{
    					this.Left = Screen.PrimaryScreen.WorkingArea.Width;
    					this.Top = 0;
    					this.WindowState = WindowState.Maximized;
    				}
    				else
    				{
    					this.WindowState = WindowState.Maximized;
    				}
    			};
    		}
    	}
    }
    
    
posted @ 2025-06-15 18:02  夏秋初  阅读(84)  评论(0)    收藏  举报