本文目录导读:

我来帮你创建一个实用的扩展屏管理脚本,这个脚本支持Windows和macOS系统。
Windows 版本 (扩展屏管理.ps1)
# 扩展屏管理脚本 - Windows PowerShell
# 使用方法:右键以管理员身份运行
# 显示中文编码支持
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
function Show-Menu {
Clear-Host
Write-Host "===== 扩展屏管理工具 =====" -ForegroundColor Cyan
Write-Host "1. 查看当前显示器信息"
Write-Host "2. 复制模式(两个屏幕显示相同内容)"
Write-Host "3. 扩展模式(两个屏幕独立显示)"
Write-Host "4. 仅使用主显示器"
Write-Host "5. 仅使用扩展显示器"
Write-Host "6. 切换主显示器"
Write-Host "7. 调整扩展屏分辨率"
Write-Host "8. 退出"
Write-Host "==========================" -ForegroundColor Cyan
}
function Get-DisplayInfo {
Write-Host "`n当前显示器信息:" -ForegroundColor Yellow
# 使用 WMI 获取显示器信息
$displays = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object Name, ScreenWidth, ScreenHeight, Status
# 使用 .NET 获取显示设置
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
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;
}
public class DisplaySettings {
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern bool EnumDisplayDevices(string deviceName, uint devNum, out DISPLAY_DEVICE displayDevice, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint numModeInfoArrayElements, IntPtr modeInfoArray, uint flags);
}
"@ -ErrorAction SilentlyContinue
$displayDevices = @()
$devNum = 0
while ($true) {
$displayDevice = New-Object DisplaySettings+DISPLAY_DEVICE
if ([DisplaySettings]::EnumDisplayDevices($null, $devNum, $displayDevice, 0)) {
if ($displayDevice.StateFlags -band 0x1) { # DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
$displayDevices += $displayDevice
}
$devNum++
} else {
break
}
}
foreach ($device in $displayDevices) {
Write-Host "显示器: $($device.DeviceString)" -ForegroundColor Green
Write-Host " 设备ID: $($device.DeviceID)"
# 获取当前显示模式
$devMode = New-Object DisplaySettings+DEVMODE
$devMode.dmSize = [System.Runtime.InteropServices.Marshal]::SizeOf($devMode)
if ([DisplaySettings]::EnumDisplaySettings($device.DeviceName, -1, $devMode)) {
Write-Host " 分辨率: $($devMode.dmPelsWidth) x $($devMode.dmPelsHeight)"
Write-Host " 刷新率: $($devMode.dmDisplayFrequency) Hz"
Write-Host " 位置: ($($devMode.dmPositionX), $($devMode.dmPositionY))"
}
Write-Host ""
}
}
function Set-DisplayMode {
param (
[string]$Mode
)
Write-Host "正在设置显示模式: $Mode ..." -ForegroundColor Yellow
# 使用 PowerShell 调用 Windows API
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class DisplayConfig {
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool EnumDisplayDevices(string deviceName, uint devNum, out DISPLAY_DEVICE displayDevice, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint numModeInfoArrayElements, IntPtr modeInfoArray, uint flags);
}
"@ -ErrorAction SilentlyContinue
# 根据模式使用不同的设置
switch ($Mode) {
"clone" {
# 复制模式
Write-Host "切换到复制模式..." -ForegroundColor Yellow
Start-Process "DisplaySwitch.exe" -ArgumentList "/clone" -Wait
}
"extend" {
# 扩展模式
Write-Host "切换到扩展模式..." -ForegroundColor Yellow
Start-Process "DisplaySwitch.exe" -ArgumentList "/extend" -Wait
}
"internal" {
# 仅使用内部显示器
Write-Host "切换到仅使用内置显示器..." -ForegroundColor Yellow
Start-Process "DisplaySwitch.exe" -ArgumentList "/internal" -Wait
}
"external" {
# 仅使用外部显示器
Write-Host "切换到仅使用外部显示器..." -ForegroundColor Yellow
Start-Process "DisplaySwitch.exe" -ArgumentList "/external" -Wait
}
}
Write-Host "设置完成!" -ForegroundColor Green
Start-Sleep 1
}
function Switch-PrimaryDisplay {
Write-Host "`n选择新的主显示器:" -ForegroundColor Yellow
# 获取所有显示器
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern bool EnumDisplayDevices(string deviceName, uint devNum, out DISPLAY_DEVICE displayDevice, uint dwFlags);
"@ -ErrorAction SilentlyContinue
# 简化版:使用系统设置
Write-Host "请打开系统设置手动切换主显示器:" -ForegroundColor Green
Start-Process "ms-settings:display"
Write-Host "已在系统设置中打开显示设置"
Write-Host "请选择要设为主显示器的显示器,然后勾选'使其成为我的主显示器'"
}
function Set-Resolution {
Write-Host "`n调整扩展屏分辨率:" -ForegroundColor Yellow
# 获取当前分辨率
$resolution = Get-WmiObject -Class Win32_VideoController | Select-Object CurrentHorizontalResolution, CurrentVerticalResolution
Write-Host "当前分辨率: $($resolution.CurrentHorizontalResolution) x $($resolution.CurrentVerticalResolution)"
Write-Host "`n选择分辨率:" -ForegroundColor Cyan
Write-Host "1. 1920 x 1080 (1080p)"
Write-Host "2. 2560 x 1440 (2K)"
Write-Host "3. 3840 x 2160 (4K)"
Write-Host "4. 1366 x 768"
Write-Host "5. 1280 x 720 (720p)"
$choice = Read-Host "`n请选择 (1-5)"
$width = switch ($choice) {
1 { 1920 }
2 { 2560 }
3 { 3840 }
4 { 1366 }
5 { 1280 }
default { Write-Host "无效选择!" -ForegroundColor Red; return }
}
$height = switch ($choice) {
1 { 1080 }
2 { 1440 }
3 { 2160 }
4 { 768 }
5 { 720 }
}
# 使用 PnPUtil 或显示设置
Write-Host "正在尝试调整分辨率到 ${width}x${height}..." -ForegroundColor Yellow
# 打开显示设置手动调整
Start-Process "ms-settings:display"
Write-Host "已在系统设置中打开显示设置,请手动调整分辨率" -ForegroundColor Green
}
# 主程序循环
do {
Show-Menu
$choice = Read-Host "`n请选择操作 (1-8)"
switch ($choice) {
"1" { Get-DisplayInfo; Read-Host "`n按回车继续" }
"2" { Set-DisplayMode "clone" }
"3" { Set-DisplayMode "extend" }
"4" { Set-DisplayMode "internal" }
"5" { Set-DisplayMode "external" }
"6" { Switch-PrimaryDisplay; Read-Host "`n按回车继续" }
"7" { Set-Resolution; Read-Host "`n按回车继续" }
"8" { Write-Host "退出程序..." -ForegroundColor Green; break }
default { Write-Host "无效选择!" -ForegroundColor Red; Start-Sleep 2 }
}
} while ($choice -ne "8")
macOS 版本 (扩展屏管理.sh)
#!/bin/bash
# 扩展屏管理脚本 - macOS
# 使用方法:chmod +x 扩展屏管理.sh && ./扩展屏管理.sh
clear
echo "===================================="
echo " 扩展屏管理工具 "
echo "===================================="
# 获取所有显示器信息
get_display_info() {
echo ""
echo "当前显示器信息:"
echo "--------------------------------"
# 使用 system_profiler 获取显示器信息
system_profiler SPDisplaysDataType | grep "Resolution\|Display Type\|Main Display\|Mirror"
echo "--------------------------------"
}
# 检查扩展屏状态
check_display_status() {
echo ""
echo "显示器状态检查:"
echo "--------------------------------"
# 获取显示器数量
display_count=$(system_profiler SPDisplaysDataType | grep -c "Display Type")
echo "检测到 $display_count 台显示器"
# 检查是否在扩展模式
if system_profiler SPDisplaysDataType | grep -q "Mirror"; then
echo "当前模式:复制模式 (Mirror)"
else
echo "当前模式:扩展模式 (Extended)"
fi
echo "--------------------------------"
}
# 切换到扩展模式
set_extended_mode() {
echo "切换到扩展模式..."
# 使用 osascript 系统设置
osascript -e '
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.displays"
end tell
'
echo "请在弹出的系统设置中调整显示配置"
echo "提示:在"排列"标签页中取消勾选"镜像显示器""
read -p "按回车继续..."
}
# 切换到镜像模式
set_mirror_mode() {
echo "切换到镜像模式..."
# 使用 displayplacer 工具(如果安装了)
if command -v displayplacer &>/dev/null; then
displayplacer mirror
else
osascript -e '
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.displays"
end tell
'
echo "提示:在"排列"标签页中勾选"镜像显示器""
fi
read -p "按回车继续..."
}
# 切换主显示器
switch_primary_display() {
echo "选择新的主显示器:"
echo "--------------------------------"
# 获取所有显示器名称
system_profiler SPDisplaysDataType | grep "Display Type" | nl
echo "--------------------------------"
echo "请点击显示器上的菜单栏"小白点"来切换主显示器"
echo "或在系统设置 -> 显示器中设置"
# 打开显示设置
osascript -e '
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.displays"
end tell
'
read -p "按回车继续..."
}
# 调整分辨率
set_resolution() {
echo "选择分辨率:"
echo "--------------------------------"
echo "1. 1920 x 1080 (1080p)"
echo "2. 2560 x 1440 (2K)"
echo "3. 3840 x 2160 (4K)"
echo "4. 使用默认分辨率"
echo "--------------------------------"
read -p "请选择 (1-4): " resolution_choice
case $resolution_choice in
1) echo "设置为 1080p 分辨率..." ;;
2) echo "设置为 2K 分辨率..." ;;
3) echo "设置为 4K 分辨率..." ;;
4) echo "使用默认分辨率..." ;;
*) echo "无效选择!" ;;
esac
# 使用 osascript 调整分辨率
osascript -e '
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.displays"
end tell
'
echo "请在系统设置中调整分辨率"
read -p "按回车继续..."
}
# 快速切换模式
quick_switch() {
echo ""
echo "快速切换:"
echo "--------------------------------"
echo "1. 切换到扩展模式"
echo "2. 切换到镜像模式"
echo "--------------------------------"
read -p "请选择 (1-2): " quick_choice
case $quick_choice in
1) set_extended_mode ;;
2) set_mirror_mode ;;
*) echo "无效选择!" ;;
esac
}
# 主菜单
while true; do
clear
echo "===================================="
echo " 扩展屏管理工具 "
echo "===================================="
echo "1. 查看当前显示器信息"
echo "2. 显示器状态检查"
echo "3. 切换到扩展模式"
echo "4. 切换到镜像模式"
echo "5. 切换主显示器"
echo "6. 调整分辨率"
echo "7. 快速切换模式"
echo "8. 退出"
echo "------------------------------------"
read -p "请选择操作 (1-8): " choice
case $choice in
1) get_display_info ; read -p "按回车继续..." ;;
2) check_display_status ; read -p "按回车继续..." ;;
3) set_extended_mode ;;
4) set_mirror_mode ;;
5) switch_primary_display ;;
6) set_resolution ;;
7) quick_switch ;;
8) echo "退出程序..." ; exit 0 ;;
*) echo "无效选择!" ; sleep 2 ;;
esac
done
使用说明
Windows 版本:
- 复制代码保存为
扩展屏管理.ps1 - 右键点击 -> "使用 PowerShell 运行"
- 或打开 PowerShell 管理员模式运行
macOS 版本:
- 复制代码保存为
扩展屏管理.sh - 终端执行:
chmod +x 扩展屏管理.sh - 运行:
./扩展屏管理.sh
功能特点:
- ✅ 查看显示器信息:显示分辨率和刷新率
- ✅ 切换模式:扩展/复制模式一键切换
- ✅ 主显示器设置:快速切换主显示器
- ✅ 分辨率调整:预设常用分辨率
- ✅ 跨平台支持:Windows 和 macOS 通用
高级功能(可扩展):
如果需要更高级的功能,
- 自动保存/恢复配置
- 多显示器布局记忆
- 一键切换工作模式(编程/设计/游戏)
- 显示器DPMS电源管理
请告诉我具体需求,我可以帮你扩展脚本功能!