WinUI3获取当前系统桌面尺寸
WinUI3项目基于WindowsAppSDK,适用版本是1.3.230502000以上,打开Visual Studio Installer 修改Visual Studio 2022
选择.NET桌面开发中的Windows 应用 SDK C# 模板即可使用!

由于WinUI3的API目前并不完善,并没有WPF和WindowsForms方便,获取系统桌面尺寸的方式比较麻烦,但优势是可以在同一个项目中混合使用UWP,WPF,WinForms,WIN32 API,其实是个混合技术。代码如下:

public async static Task<SizeInt32> GetMonitorSize()
{
var monitorSize = new SizeInt32();
var displayList = await DeviceInformation.FindAllAsync
(DisplayMonitor.GetDeviceSelector());
if (!displayList.Any())
return monitorSize;
var monitorInfo = await DisplayMonitor.FromInterfaceIdAsync(displayList[0].Id);
if (monitorInfo != null)
{
monitorSize= monitorInfo.NativeResolutionInRawPixels;
}
return monitorSize;
}
当然也可以使用其他方法。