batch-file - 获取显示器分辨率的命令行参数仅返回一台显示器

标签 batch-file cmd screen-resolution

我有一台 Windows 7 笔记本电脑,连接到两个额外的显示器。为了将任务栏扩展到这些额外的监视器(与 Win10 不同,Win7 本身不具备此功能),我使用了一个名为 DualMonitor.exe 的程序。

我的问题是,当我断开笔记本电脑与这些额外显示器的连接时,DualMonitor 管理的程序会被隐藏。为了解决这个问题,我制作了一个简单的批处理文件来重新启动 explorerDualMonitor

@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/

相关文章:

javascript - 通过滚动点触发的更稳定版本(jQuery/JS)

android - 在 Android 上创建分辨率独立游戏

windows - 永久设置环境路径

c# - 在服务器上远程执行批处理文件

node.js - nodejs throw er;//未处理的 'error'事件

batch-file - 在 cmd 中使用 |find 查找多行

command - Java jar-Archive 工具 - 设置包含内容的文件夹的路径

jquery - 根据屏幕分辨率更改 JavaScript 中 CSS 类的高度和宽度

batch-file - 如何将封面艺术添加到 MP4?

powershell - 如何关闭所有窗口