@echo off
set h=wmic desktopmonitor, get screenheight
set w=wmic desktopmonitor, get screenwidth
echo %h%
echo %w%
pause

而不是得到 -

1600
2560

我明白了 -

echo wmic desktopmonitor,获取屏幕宽度

echo wmic 桌面监视器,获取屏幕高度

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

最佳答案

使用 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%

如果您愿意,我还可以添加一个 dpi 分辨率 getter ?
如果有多个显示器,我将不得不修改脚本...

另一种允许您获得更多显示器分辨率的方法是使用 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

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

编辑:

一个 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

关于batch-file - BATCH - 从 Windows 命令行获取显示分辨率并设置变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25594848/

10-16 10:43