仅获取显示器分辨率的命令行参数 returns 一台显示器
Command line argument to get monitor resolution only returns one monitor
我有一台 Windows 7 笔记本电脑连接到两个额外的显示器。为了将任务栏扩展到这些额外的显示器(与 Win10 不同,Win7 本身不具备此功能)我使用了一个名为 DualMonitor.exe
.
的程序
我的问题是,当我断开笔记本电脑与这些额外显示器的连接时,DualMonitor
管理的程序会隐藏起来。为了解决这个问题,我制作了一个简单的批处理文件来重新启动 explorer
和 DualMonitor
.
@echo off
taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul
taskkill /f /im explorer.exe
ping localhost -n 3 > nul
echo.
echo restarting explorer
start explorer.exe
ping localhost -n 6 > nul
echo restarting dual monitor
start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"
上面的批处理工作正常,但我想对此进行调整,以便它仅在连接了额外监视器的情况下打开 DualMonitor
。我做了一些研究,发现这些命令可以修改为 计算连接到笔记本电脑的额外显示器的数量:
wmic desktopmonitor get screenwidth, screenheight
wmic path Win32_VideoController get CurrentHorizontalResolution, CurrentVerticalResolution
...但对我来说 尽管连接了两个额外的显示器,但这些 return 笔记本电脑显示器的分辨率 。我没有这台机器的管理员权限,所以我无法在我的解决方案中使用 dxdiag
。
最终结果
@echo off
taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul
taskkill /f /im explorer.exe
ping localhost -n 3 > nul
echo.
echo restarting explorer
start explorer.exe
ping localhost -n 3 > nul
for /F %%M in ('
wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
^| find /C "="
') do set count=%%M
if %count% GTR 1 (
echo restarting dual monitor.
start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"
ping localhost -n 4 > nul
)
echo.
echo all done.
ping localhost -n 2 > nul
像您一样使用 wmic
,但仅 select 一个 属性 并计算出现次数:
for /f %%a in ('wmic desktopmonitor get DeviceID /value ^| find /c "="') do set "monitors=%%a"
echo there are %monitors% monitors.
要确定连接的监视器的数量,您可以使用以下代码:
for /F %%M in ('
wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
^| find /C "="
') do echo There are %%M monitors.
请参阅文章 Win32_PnPEntity class 了解 WMI class Win32_PnPEntity
。
我有一台 Windows 7 笔记本电脑连接到两个额外的显示器。为了将任务栏扩展到这些额外的显示器(与 Win10 不同,Win7 本身不具备此功能)我使用了一个名为 DualMonitor.exe
.
我的问题是,当我断开笔记本电脑与这些额外显示器的连接时,DualMonitor
管理的程序会隐藏起来。为了解决这个问题,我制作了一个简单的批处理文件来重新启动 explorer
和 DualMonitor
.
@echo off
taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul
taskkill /f /im explorer.exe
ping localhost -n 3 > nul
echo.
echo restarting explorer
start explorer.exe
ping localhost -n 6 > nul
echo restarting dual monitor
start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"
上面的批处理工作正常,但我想对此进行调整,以便它仅在连接了额外监视器的情况下打开 DualMonitor
。我做了一些研究,发现这些命令可以修改为 计算连接到笔记本电脑的额外显示器的数量:
wmic desktopmonitor get screenwidth, screenheight
wmic path Win32_VideoController get CurrentHorizontalResolution, CurrentVerticalResolution
...但对我来说 尽管连接了两个额外的显示器,但这些 return 笔记本电脑显示器的分辨率 。我没有这台机器的管理员权限,所以我无法在我的解决方案中使用 dxdiag
。
最终结果
@echo off
taskkill /f /im dualmonitor.exe
ping localhost -n 1 > nul
taskkill /f /im explorer.exe
ping localhost -n 3 > nul
echo.
echo restarting explorer
start explorer.exe
ping localhost -n 3 > nul
for /F %%M in ('
wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
^| find /C "="
') do set count=%%M
if %count% GTR 1 (
echo restarting dual monitor.
start "" "C:\Users\X\AppData\Local\Dual Monitor\DualMonitor.exe"
ping localhost -n 4 > nul
)
echo.
echo all done.
ping localhost -n 2 > nul
像您一样使用 wmic
,但仅 select 一个 属性 并计算出现次数:
for /f %%a in ('wmic desktopmonitor get DeviceID /value ^| find /c "="') do set "monitors=%%a"
echo there are %monitors% monitors.
要确定连接的监视器的数量,您可以使用以下代码:
for /F %%M in ('
wmic path Win32_PnPEntity where "Service='monitor' and Status='OK'" get DeviceID /VALUE ^
^| find /C "="
') do echo There are %%M monitors.
请参阅文章 Win32_PnPEntity class 了解 WMI class Win32_PnPEntity
。