Win11下通过PowerShell获取多显示器真实分辨率的问题
如何在Win11中通过PowerShell获取多显示器真实分辨率?
我在Win11系统下做应用开发,需要获取所有显示器的真实屏幕分辨率,但尝试多个PowerShell命令都没得到正确结果:
尝试过的方法及问题
方法1:使用System.Windows.Forms.Screen
Add-Type -AssemblyName System.Windows.Forms $result = [System.Windows.Forms.Screen]::AllScreens $result
输出的WorkingArea信息不符合预期:
WorkingArea={X=-3840,Y=512,Width=2560,Height=1528}(预期为2560 x 1600)
WorkingArea={X=0,Y=0,Width=2880,Height=1728}(预期为1920 x 1200)
方法2:改用Bounds属性
Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Screen]::AllScreens | ForEach-Object { '{0} x {1}' -f $_.Bounds.Width, $_.Bounds.Height }
输出:
2560 x 1600
2880 x 1620
副显示器结果正确,但主显示器仍与真实值不符。
方法3:使用Win32_VideoController
$height = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription -split "`n")[0] -split ' ')[2] $width = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription -split "`n")[0] -split ' ')[0] echo $width, $height
输出:
1920 1200
仅能获取主显示器分辨率,副显示器信息丢失。
真实分辨率(系统推荐值):
- 主显示器:1920 x 1200
- 副显示器:2560 x 1600
正确解决方案
方案1:使用Win32_DesktopMonitor(需管理员权限)
该类可直接返回每个显示器的硬件原生分辨率,需以管理员身份运行PowerShell:
Get-CimInstance -ClassName Win32_DesktopMonitor | ForEach-Object { Write-Host "$($_.DeviceID) 真实分辨率:$($_.ScreenWidth) x $($_.ScreenHeight)" }
方案2:调用Windows API EnumDisplaySettingsEx
通过直接调用系统API枚举注册表中存储的推荐分辨率,精准获取真实值:
Add-Type @" using System; using System.Runtime.InteropServices; public class DisplayInfo { public const int ENUM_CURRENT_SETTINGS = -1; public const int ENUM_REGISTRY_SETTINGS = -2; [StructLayout(LayoutKind.Sequential)] public struct DEVMODE { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; public short dmSize; public short dmDriverExtra; public int dmFields; public int dmPositionX; public int dmPositionY; public int dmDisplayOrientation; public int dmDisplayFixedOutput; public short dmColor; public short dmDuplex; public short dmYResolution; public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string dmFormName; public short dmLogPixels; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; public int dmICMMethod; public int dmICMIntent; public int dmMediaType; public int dmDitherType; public int dmReserved1; public int dmReserved2; public int dmPanningWidth; public int dmPanningHeight; } [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern bool EnumDisplaySettingsEx(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode, int dwFlags); } "@ $devMode = New-Object DisplayInfo+DEVMODE $devMode.dmSize = [System.Runtime.InteropServices.Marshal]::SizeOf($devMode) $deviceIndex = 0 while ([DisplayInfo]::EnumDisplaySettingsEx([IntPtr]::Zero, [DisplayInfo]::ENUM_REGISTRY_SETTINGS, [ref]$devMode, 0)) { if ($devMode.dmPelsWidth -gt 0 -and $devMode.dmPelsHeight -gt 0) { Write-Host "显示器 $deviceIndex 真实分辨率:$($devMode.dmPelsWidth) x $($devMode.dmPelsHeight)" $deviceIndex++ } }
内容的提问来源于stack exchange,提问作者TheRob87
相关产品推荐
相关产品推荐

