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

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

不是获取 -

1600 2560

我得到的是 -

echo wmic desktopmonitor, get screenwidth

echo wmic desktopmonitor, get screenheight

我想让这个批处理脚本能够获取我的显示器分辨率大小并将其设置为高度和宽度变量,并能以数字形式输出。
但似乎不起作用。


1
这不是有效的批处理文件语法。你可能想从这里开始阅读:http://www.dostips.com/ - ths
5个回答

8

使用 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分辨率获取器?但是如果有多个显示器,则需要修改脚本...

另一种获取多个显示器分辨率的方法是使用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

如何获取多个显示器的分辨率? - str11
1
@str11 - 最好还是提出一个新问题。 - npocmaka

1
 @echo off
 title  Detect Screen Resolution
 REM Detect Screen Resolution in Windows 7 - 10 - 32/64 bit compatible.
 REM You must have the proper graphics driver installed in your system.

 set loop#=2
 set /a loop=1     

 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && goto next
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && echo win 10 && goto next

      :next   

 for /f "tokens=*" %%c in ('reg query "%Resolution-path%\0000"  /v "DefaultSettings.XResolution"  ^| find "0"') do @set "XResolution-HEX=%%c" 
 for /f "tokens=*" %%c in ('reg query "%Resolution-path%\0000"  /v "DefaultSettings.YResolution"  ^| find "0"') do @set "YResolution-HEX=%%c" 

 if %XResolution-HEX:~44% == 0x400 goto not1
 if 0 == %XResolution-HEX:~44,-4%  goto Y1
 goto not1

      :Y1
   ver | find "6.1" > nul 
      if %ERRORLEVEL% == 0 echo %Resolution-path:~84% win 7 && goto next2
  ver | find "6.2" > nul  
      if %ERRORLEVEL% == 0 echo %Resolution-path:~84% win 8 && goto next2    
 echo %Resolution-path:~74% win 10  
       :next2
 echo %XResolution-HEX:~44%
 echo %YResolution-HEX:~44%
 if %XResolution-HEX:~44% == 0x780 echo . && if %YResolution-HEX:~44% == 0x438  set screen-resolution=1920x1080
 if %XResolution-HEX:~44% == 0x6f0 echo . && if %YResolution-HEX:~44% == 0x3e8  set screen-resolution=1776x1000 
 if %XResolution-HEX:~44% == 0x690 echo . && if %YResolution-HEX:~44% == 0x41a  set screen-resolution=1680x1050 
 if %XResolution-HEX:~44% == 0x640 echo . && if %YResolution-HEX:~44% == 0x384  set screen-resolution=1600x900 
 if %XResolution-HEX:~44% == 0x556 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1366x768 
 if %XResolution-HEX:~44% == 0x550 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1360x768
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x400  set screen-resolution=1280x1024
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x3c0  set screen-resolution=1280x960
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1280x768
 if %XResolution-HEX:~44% == 0x500 echo . && if %YResolution-HEX:~44% == 0x2d0  set screen-resolution=1280x720
 if %XResolution-HEX:~44% == 0x480 echo . && if %YResolution-HEX:~44% == 0x288  set screen-resolution=1152x648
 if %XResolution-HEX:~44% == 0x400 echo . && if %YResolution-HEX:~44% == 0x300  set screen-resolution=1024x768
 if %XResolution-HEX:~44% == 0x320 echo . && if %YResolution-HEX:~44% == 0x258  set screen-resolution=800x600
 setx screen-resolution "%screen-resolution%"
 echo Detected screen resolution.
 echo %screen-resolution%
 echo.
 echo.
 pause
 exit
      :not1
 if %loop% == %loop#% goto not     
 set /a loop=loop+1     
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" 
 for /f "tokens=*" %%c in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\UnitedVideo\CONTROL\VIDEO"  ^| find "{"') do @set "Resolution-path=%%c" && echo win 10 
       goto next

      :not
 setlocal
 color 4f
 cd /d %~dp0     
 echo  failed exiting
 timeout /t 05  
 exit 

仅提供代码答案是不被鼓励的。请添加一些解释,说明如何解决问题,或者这与现有答案有何不同。来自审核 - Nick
有趣的是,在我的情况下(AMD驱动程序),注册表仅针对2个显示器而不是3个具有数据(0000和0001)。第二个hkey丢失了。你的答案是不准确的。首先,CurrentControlSet键包含当前配置。其次,有多个非活动的GUID,您必须通过扫描其他地方的注册表来找到活动的GUID,例如:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\VideoHKEY_USERS\.DEFAULT\Software\ATI\ACE\Settings\ADL\MaxCofuncPaths - Andry
我建议扫描HKLM\SYSTEM\CurrentControlSet\Control\Video键的VolatileSettings子键,并收集所有这些最新更改时间(节点键具有时间)。我已经测试过,在Windows XP/7 x64上使用VirtualBox驱动程序的方式是相同的。在找到桌面监视器GUID之后,您可以继续使用HKLM\SYSTEM\CurrentControlSet\Hardware Profiles\Current\System\CurrentControlSet\Control\VIDEO键查找监视器分辨率。HKLM\SYSTEM\CurrentControlSet\Hardware Profiles\UnitedVideo\CONTROL\VIDEO似乎包含监视器模式,并且在VirtualBox上不存在。 - Andry

0

使用一次调用的wmic“一站式”解决方案(2019年1月2日的350毫秒对比下的200毫秒)

@echo off
  set "zzTmp=desktopmonitor get ScreenWidth^,ScreenHeight"
  for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
  echo Version: %version%
  if version GTR 62 set "zzTmp=path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution"

  for /f %%s in ('wmic %zzTmp% /value ^|find "="') do set "zZ%%s"1>nul 2>nul
  if defined zZCurrentHorizontalResolution set "zZScreenWidth=%zZCurrentHorizontalResolution%"
  if defined zZCurrentVerticalResolution set "zZScreenHeight=%zZCurrentVerticalResolution%"
  if not defined zZScreenWidth set "zZScreenWidth=1024" & set "zZScreenHeight=768"
  set /a zzSsW=%zZScreenWidth%
  set /a zzSsH=%zZScreenHeight%
  set "zzTmp="
  set "zZScreenWidth="
  set "zZScreenHeight="
  set "zZCurrentHorizontalResolution="
  set "zZCurrentVerticalResolution="

  echo. Resolution : [%zzSsW%]x[%zzSsH%]%zz%

0

干得好 :-) 为了防止空集命令,我建议使用find进行这个小添加:

FOR /f "delims=" %%a IN ('%comspec% /c "wmic desktopmonitor get ScreenWidth,ScreenHeight" /value ^| find "="') DO (SET %%a)

0

我很好奇这段代码在另一个Windows系统上是否能正常工作(已在Windows 10专业版x64上进行了测试):

:: in command line ::
@for /f delims^= %I in ('wmic.exe path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution^|findstr /b [0-9]')do @for /f tokens^=1-2 %a in ('echo\%I')do @set "h=%a" && @set "w=%b" && call echo=%h%x%w%

:: in bat/cmd file ::
@echo off && setlocal enabledelayedexpansion

for /f delims^= %%I in ('
wmic.exe path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution^|findstr /b [0-9]
')do for /f tokens^=1-2 %%a in ('echo\%%I')do set "h=%%a" && set "w=%%b" && call echo=!h!x!w!

  • 输出:

1920x1080

变量%h%%w%echo=%h% %w%中返回1920 1080

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接