我有一台 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
...但对我来说,尽管连接到两个额外的显示器,但它们都仅返回笔记本电脑显示器的分辨率。我没有这台机器的管理员权限,因此无法在我的解决方案中使用 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
最佳答案
要确定连接的监视器的数量,您可以使用以下代码:
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 类 Win32_PnPEntity
。
关于batch-file - 获取显示器分辨率的命令行参数仅返回一台显示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238920/