Advertisement
Guest User

Untitled

a guest
Nov 9th, 2023
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.61 KB | Software | 0 0
  1. function Set-ScreenRefreshRate
  2. {
  3.     param (
  4.         [Parameter(Mandatory=$true)]
  5.         [int] $Frequency
  6.     )
  7.  
  8.     $pinvokeCode = @"        
  9.        using System;
  10.        using System.Runtime.InteropServices;
  11.  
  12.        namespace Display
  13.        {
  14.            [StructLayout(LayoutKind.Sequential)]
  15.            public struct DEVMODE1
  16.            {
  17.                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  18.                public string dmDeviceName;
  19.                public short dmSpecVersion;
  20.                public short dmDriverVersion;
  21.                public short dmSize;
  22.                public short dmDriverExtra;
  23.                public int dmFields;
  24.  
  25.                public short dmOrientation;
  26.                public short dmPaperSize;
  27.                public short dmPaperLength;
  28.                public short dmPaperWidth;
  29.  
  30.                public short dmScale;
  31.                public short dmCopies;
  32.                public short dmDefaultSource;
  33.                public short dmPrintQuality;
  34.                public short dmColor;
  35.                public short dmDuplex;
  36.                public short dmYResolution;
  37.                public short dmTTOption;
  38.                public short dmCollate;
  39.                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  40.                public string dmFormName;
  41.                public short dmLogPixels;
  42.                public short dmBitsPerPel;
  43.                public int dmPelsWidth;
  44.                public int dmPelsHeight;
  45.  
  46.                public int dmDisplayFlags;
  47.                public int dmDisplayFrequency;
  48.  
  49.                public int dmICMMethod;
  50.                public int dmICMIntent;
  51.                public int dmMediaType;
  52.                public int dmDitherType;
  53.                public int dmReserved1;
  54.                public int dmReserved2;
  55.  
  56.                public int dmPanningWidth;
  57.                public int dmPanningHeight;
  58.            };
  59.  
  60.            class User_32
  61.            {
  62.                [DllImport("user32.dll")]
  63.                public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
  64.                [DllImport("user32.dll")]
  65.                public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
  66.  
  67.                public const int ENUM_CURRENT_SETTINGS = -1;
  68.                public const int CDS_UPDATEREGISTRY = 0x01;
  69.                public const int CDS_TEST = 0x02;
  70.                public const int DISP_CHANGE_SUCCESSFUL = 0;
  71.                public const int DISP_CHANGE_RESTART = 1;
  72.                public const int DISP_CHANGE_FAILED = -1;
  73.            }
  74.  
  75.            public class PrimaryScreen  
  76.            {
  77.                static public string ChangeRefreshRate(int frequency)
  78.                {
  79.                    DEVMODE1 dm = GetDevMode1();
  80.  
  81.                    if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
  82.                    {
  83.                        dm.dmDisplayFrequency = frequency;
  84.  
  85.                        int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
  86.  
  87.                        if (iRet == User_32.DISP_CHANGE_FAILED)
  88.                        {
  89.                            return "Unable to process your request. Sorry for this inconvenience.";
  90.                        }
  91.                        else
  92.                        {
  93.                            iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
  94.                            switch (iRet)
  95.                            {
  96.                                case User_32.DISP_CHANGE_SUCCESSFUL:
  97.                                {
  98.                                    return "Success";
  99.                                }
  100.                                case User_32.DISP_CHANGE_RESTART:
  101.                                {
  102.                                    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.";
  103.                                }
  104.                                default:
  105.                                {
  106.                                    return "Failed to change the resolution";
  107.                                }
  108.                            }
  109.                        }
  110.                    }
  111.                    else
  112.                    {
  113.                        return "Failed to change the resolution.";
  114.                    }
  115.                }
  116.  
  117.                private static DEVMODE1 GetDevMode1()
  118.                {
  119.                    DEVMODE1 dm = new DEVMODE1();
  120.                    dm.dmDeviceName = new String(new char[32]);
  121.                    dm.dmFormName = new String(new char[32]);
  122.                    dm.dmSize = (short)Marshal.SizeOf(dm);
  123.                    return dm;
  124.                }
  125.            }
  126.        }
  127. "@ # don't indend this line
  128.  
  129.     Add-Type $pinvokeCode -ErrorAction SilentlyContinue
  130.  
  131.     [Display.PrimaryScreen]::ChangeRefreshRate($frequency)
  132. }
  133.  
  134. function Get-ScreenRefreshRate
  135. {
  136.     $frequency = Get-WmiObject -Class "Win32_VideoController" | Select-Object -ExpandProperty "CurrentRefreshRate"
  137.  
  138.     return $frequency
  139. }
  140.  
  141. Displayswitch.exe /external
  142. cp c:\ProgramData\Parsec\config.json.fromwork c:\ProgramData\Parsec\config.json
  143. sleep 1
  144. Set-DisplayResolution -Width 1920 -Height 1080
  145. Set-ScreenRefreshRate -Frequency 60
  146. Stop-Process -name Parsecd -Force
  147. Start-Process -Verb Open -filepath "C:\Program Files\Parsec\parsecd.exe"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement