Skip to content

Instantly share code, notes, and snippets.

@Raicuparta
Last active September 14, 2023 09:50
Show Gist options
  • Save Raicuparta/e7567dbef315cf58c93fc8f2b0fea2bd to your computer and use it in GitHub Desktop.
Save Raicuparta/e7567dbef315cf58c93fc8f2b0fea2bd to your computer and use it in GitHub Desktop.
Change / Toggle resolution
# Toggle between high and low resolutions, or set specific resolution and refresh rate.
# Adapted this to be easily used by Sunshine stream thing.
#
# Example Sunshine "Do Command" for changing resolution:
# powershell.exe -File "C:\Apps\Scripts\toggle-resolution.ps1" -Width 1920 -Height 1080 -Freq 60
# And the "Undo Command":
# powershell.exe -File "C:\Apps\Scripts\toggle-resolution.ps1" -Width 3440 -Height 1440 -Freq 144
# And for a shortcut that just toggles between high and low resolutions (set the values below):
# powershell.exe -File "C:\Apps\Scripts\toggle-resolution.ps1"
param(
[int]$Width,
[int]$Height,
[int]$Freq,
[bool]$Toggle
)
Function Set-ScreenResolution {
param (
[Parameter(Mandatory=$true,
Position = 0)]
[int]
$Width,
[Parameter(Mandatory=$true,
Position = 1)]
[int]
$Height,
[Parameter(Mandatory=$true,
Position = 2)]
[int]
$Freq
)
$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;
};
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 ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
public const int ENUM_CURRENT_SETTINGS = -1;
public const int CDS_UPDATEREGISTRY = 0x01;
public const int CDS_TEST = 0x02;
public const int DISP_CHANGE_SUCCESSFUL = 0;
public const int DISP_CHANGE_RESTART = 1;
public const int DISP_CHANGE_FAILED = -1;
}
public class PrmaryScreenResolution
{
static public string ChangeResolution(int width, int height, int freq)
{
DEVMODE1 dm = GetDevMode1();
if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
{
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmDisplayFrequency = freq;
int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
if (iRet == User_32.DISP_CHANGE_FAILED)
{
return "Unable to process your request. Sorry for this inconvenience.";
}
else
{
iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
switch (iRet)
{
case User_32.DISP_CHANGE_SUCCESSFUL:
{
return "Success";
}
case User_32.DISP_CHANGE_RESTART:
{
return "You need to reboot for the change to happen.\n If you feel any problems after rebooting your machine\nThen try to change resolution in Safe Mode.";
}
default:
{
return "Failed to change the resolution";
}
}
}
}
else
{
return "Failed to change the resolution.";
}
}
private static DEVMODE1 GetDevMode1()
{
DEVMODE1 dm = new DEVMODE1();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
return dm;
}
}
}
"@
Add-Type $pinvokeCode -ErrorAction SilentlyContinue
[Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height,$freq)
}
Function Get-ScreenHeight
{
$frequency = Get-WmiObject -Class "Win32_VideoController" | Select-Object -ExpandProperty "CurrentVerticalResolution"
return $frequency
}
if (($Width -eq 0) -or ($Height -eq 0) -or ($Freq -eq 0)) {
# I was to lazy to make params for these.
# Change the values for your prefered high/low resolutions.
# If one of the width / height / freq params is missing, it will just toggle high and low.
$isLowResolution = (Get-ScreenHeight) -eq 1080
$newWidth = if ($isLowResolution) { 3440 } else { 1920 }
$newHeight = if ($isLowResolution) { 1440 } else { 1080 }
$newFrequency = if ($isLowResolution) { 144 } else { 60 }
Set-ScreenResolution -Width $newWidth -Height $newHeight -Freq $newFrequency
} else {
Set-ScreenResolution -Width $Width -Height $Height -Freq $Freq
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment