windows实现屏幕切换
在Windows中,你可以使用批处理文件配合PowerShell命令来实现多显示器设置。以下是几种方法:
方法1:使用纯批处理文件调用PowerShell
@echo off
echo 正在设置仅在显示器2上显示...
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize"
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $screens = [System.Windows.Forms.Screen]::AllScreens; if ($screens.Count -gt 1) { Write-Host '找到' $screens.Count '个显示器' } else { Write-Host '只有一个显示器,无法切换' }"
:: 使用DisplaySwitch命令切换到仅第二屏幕
DisplaySwitch /external
timeout /t 3
echo 显示设置已完成!
pause
方法2:使用更精确的PowerShell脚本
创建一个批处理文件,内容如下:
@echo off
echo 正在配置多显示器设置...
:: 调用PowerShell脚本来设置仅在显示器2显示
powershell -ExecutionPolicy Bypass -Command "
try {
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    \$screens = [System.Windows.Forms.Screen]::AllScreens
    if (\$screens.Count -lt 2) {
        Write-Host '错误:未检测到多个显示器'
        exit 1
    }
    
    Write-Host '检测到' \$screens.Count '个显示器'
    
    # 使用Windows API设置显示模式
    \$signature = @'
    [DllImport(\"user32.dll\")]
    public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
    
    [DllImport(\"user32.dll\")]
    public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam);
    
    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public int dmPositionX;
        public int dmPositionY;
        public int dmDisplayOrientation;
        public int dmDisplayFixedOutput;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;
        public short dmLogPixels;
        public int dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;
        public int dmDisplayFlags;
        public int dmDisplayFrequency;
        public int dmICMMethod;
        public int dmICMIntent;
        public int dmMediaType;
        public int dmDitherType;
        public int dmReserved1;
        public int dmReserved2;
        public int dmPanningWidth;
        public int dmPanningHeight;
    }
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct DISPLAY_DEVICE {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceString;
        public int StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceKey;
    }
'@
    
    Add-Type -MemberDefinition \$signature -Name DisplaySettings -Namespace Win32
    
    Write-Host '正在切换到仅第二显示器显示...'
}
catch {
    Write-Host '错误:' \$_.Exception.Message
}
"
echo 如果上述方法不工作,请尝试手动设置或使用以下方法...
pause
方法3:使用Windows内置的显示切换命令
@echo off
echo 多显示器设置工具
echo =================
echo.
echo 1. 复制模式
echo 2. 扩展模式
echo 3. 仅第一屏幕
echo 4. 仅第二屏幕
echo.
set /p choice="请选择显示模式 (1-4): "
if "%choice%"=="1" (
    DisplaySwitch /clone
) else if "%choice%"=="2" (
    DisplaySwitch /extend
) else if "%choice%"=="3" (
    DisplaySwitch /internal
) else if "%choice%"=="4" (
    DisplaySwitch /external
) else (
    echo 无效选择
    pause
    exit
)
echo 显示模式已更改!
timeout /t 2
方法4:使用第三方工具(推荐)
你可以使用更强大的第三方工具如:
- NirCmd(免费命令行工具)
 - MultiMonitorTool(NirSoft出品)
 
使用NirCmd的示例:
@echo off
echo 正在下载并配置NirCmd工具...
:: 首先下载nircmd.exe
powershell -Command "Invoke-WebRequest -Uri 'https://www.nirsoft.net/utils/nircmd.zip' -OutFile 'nircmd.zip'"
powershell -Command "Expand-Archive -Path 'nircmd.zip' -DestinationPath '.'"
:: 使用nircmd设置显示器
nircmd.exe setdisplay monitor:2 setprimary
nircmd.exe setdisplay monitor:1 disabled
echo 设置完成!
pause
最简单的解决方案
对于大多数用户,最简单有效的方法是使用Windows内置的DisplaySwitch命令:
@echo off
echo 正在切换到仅第二显示器...
DisplaySwitch /external
echo 完成!
pause
注意:
- 这些命令可能需要管理员权限
 - 显示器编号可能与你的物理连接顺序不同
 - 建议先备份当前显示设置
 - 某些命令可能需要Windows 10或更高版本
 
选择适合你系统环境的方法即可实现仅在第二个显示器上显示的需求。
                    
                
                
            
        
浙公网安备 33010602011771号