首页  编辑  

Powershell更改分辨率脚本

Tags: /计算机文档/脚本,批处理/   Date Created:
Powershell中更改分辨率的脚本,支持多显示器
Change display resolution with multi-monitor supported
Function Set-Resolution
{
    param
    (
        [Parameter(Mandatory = $true, Position = 0)] [int] $Width, 
        [Parameter(Mandatory = $true, Position = 1)] [int] $Height,
        [Parameter(Mandatory = $true, Position = 2)] [int] $Index
    )
    
    #设置分辨率
    $pinvokeCode = @" 
using System;
using System.Runtime.InteropServices;

namespace Resolution
{
    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE1
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public short dmOrientation;
        public short dmPaperSize;
        public short dmPaperLength;
        public short dmPaperWidth;
        public short dmScale;
        public short dmCopies;
        public short dmDefaultSource;
        public short dmPrintQuality;
        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 short 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 struct DISPLAY_DEVICE
    {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string deviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string deviceString;
        public uint StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string deviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string deviceKey;
    }

    class User_32
    {
        [DllImport("user32.dll")]
        public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
        [DllImport("user32.dll")]
        public static extern int ChangeDisplaySettingsEx(string deviceName, ref DEVMODE1 devMode, int hwnd, int flags, int lParam);
        [DllImport("User32.dll")]
        public static extern int EnumDisplayDevices(string lpszDeviceName, int iDevNum, ref DISPLAY_DEVICE device, uint dwFlags);
    }

    public class PrmaryScreenResolution
    {
        static public string ChangeResolution(int width, int height, int index)
        {
            DEVMODE1 dm = new DEVMODE1();
            dm.dmDeviceName = new String(new char[32]); ;
            dm.dmFormName = new String(new char[32]);
            dm.dmSize = (short)Marshal.SizeOf(dm);

            DISPLAY_DEVICE dd = new DISPLAY_DEVICE();
            dd.cb = (short)Marshal.SizeOf(dd);

            User_32.EnumDisplayDevices(null, index, ref dd, 0);
            if (0 != User_32.EnumDisplaySettings(dd.deviceName, -1, ref dm))  // 获取成功,则只修改分辨率,其余不变
            {
                dm.dmPelsWidth = width;
                dm.dmPelsHeight = height;

                int iRet = User_32.ChangeDisplaySettingsEx(dd.deviceName, ref dm, 0, 2, 0);
                if (iRet == -1) {
                    return "Test change resolution failed";
                } else {
                    switch (User_32.ChangeDisplaySettingsEx(dd.deviceName, ref dm, 0, 1, 0))
                    {
                        case -2: return "Bad mode";
                        case -1: return "Change failed";
                        case 0: return "Ok"; 
                        case 1: return "Reboot needed";
                        default: return "Unknown error";
                    }
                }
            } else {
                return "Failed To Change The Resolution";
            }
        }
    }
}
"@ 
    Add-Type $pinvokeCode -ErrorAction SilentlyContinue 
    [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height,$index) 
}
Set-Resolution 1920 1050 1
查询多显示器分辨率脚本:
Add-Type -AssemblyName System.Windows.Forms 
$Monitors = [System.Windows.Forms.Screen]::AllScreens

foreach ($Monitor in $Monitors)
{
	$DeviceName = (($Monitor.DeviceName).replace("\", "")).replace(".", "")
	$Width = $Monitor.bounds.Width
	$Height = $Monitor.bounds.Height
	Write-Host "$DeviceName - $Width x $height"
}