BATCH - 从Windows命令行获取显示分辨率并设置变量

l1g*_*t3d 5 resolution batch-file

@echo off
set h=wmic desktopmonitor, get screenheight
set w=wmic desktopmonitor, get screenwidth
echo %h%
echo %w%
pause
Run Code Online (Sandbox Code Playgroud)

而不是得到 -

1600 2560

我明白了 -

echo wmic desktopmonitor,获取screenwidth

echo wmic desktopmonitor,获取屏幕高度

我希望这个批处理脚本能够获得我的显示分辨率大小并将其设置为高度和宽度变量,并且能够回显数量.
但它似乎没有起作用.

npo*_*aka 7

desktopmonitor你只能获得dpi.对于你需要的像素分辨率Win32_VideoController:

@echo off

for /f "delims=" %%# in  ('"wmic path Win32_VideoController  get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
  set "%%#">nul
)

echo %CurrentHorizontalResolution%
echo %CurrentVerticalResolution%
Run Code Online (Sandbox Code Playgroud)

如果你想我也可以添加一个dpi分辨率吸气剂?如果有多个显示器,我将不得不修改脚本......

另一种允许你获得更多监视器分辨率的方法是使用DxDiag(虽然它会创建一个临时文件并且会更慢):

使用dxdiag:

@echo off

del ~.txt /q /f >nul 2>nul
start "" /w dxdiag /t ~
setlocal enableDelayedExpansion
set currmon=1 
for /f "tokens=2 delims=:" %%a in ('find "Current Mode:" ~.txt') do (
    echo Monitor !currmon! : %%a
    set /a currmon=currmon+1

)
endlocal
del ~.txt /q /f >nul 2>nul
Run Code Online (Sandbox Code Playgroud)

这将打印所有显示器的分辨率.

编辑:

一个wmic脚本,它将检测Windows的版本,并在需要时使用不同的wmi类:

@echo off

setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"


if version lss 62 (
    ::set "wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value"
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenwidth /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenheight /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

) else (
    ::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentHorizontalResolution  /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

)

echo Resolution %x%x%y%

endlocal
Run Code Online (Sandbox Code Playgroud)