Advertisement
Guest User

Change Windows resolution

a guest
Dec 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. Function Set-ScreenResolution {
  2.  
  3. <#
  4. .Synopsis
  5. Sets the Screen Resolution of the primary monitor
  6. .Description
  7. Uses Pinvoke and ChangeDisplaySettings Win32API to make the change
  8. .Example
  9. Set-ScreenResolution -Width 1024 -Height 768
  10. #>
  11. param (
  12. [Parameter(Mandatory=$true,
  13. Position = 0)]
  14. [int]
  15. $Width,
  16.  
  17. [Parameter(Mandatory=$true,
  18. Position = 1)]
  19. [int]
  20. $Height
  21. )
  22.  
  23. $pinvokeCode = @"
  24.  
  25. using System;
  26. using System.Runtime.InteropServices;
  27.  
  28. namespace Resolution
  29. {
  30.  
  31. [StructLayout(LayoutKind.Sequential)]
  32. public struct DEVMODE1
  33. {
  34. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  35. public string dmDeviceName;
  36. public short dmSpecVersion;
  37. public short dmDriverVersion;
  38. public short dmSize;
  39. public short dmDriverExtra;
  40. public int dmFields;
  41.  
  42. public short dmOrientation;
  43. public short dmPaperSize;
  44. public short dmPaperLength;
  45. public short dmPaperWidth;
  46.  
  47. public short dmScale;
  48. public short dmCopies;
  49. public short dmDefaultSource;
  50. public short dmPrintQuality;
  51. public short dmColor;
  52. public short dmDuplex;
  53. public short dmYResolution;
  54. public short dmTTOption;
  55. public short dmCollate;
  56. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  57. public string dmFormName;
  58. public short dmLogPixels;
  59. public short dmBitsPerPel;
  60. public int dmPelsWidth;
  61. public int dmPelsHeight;
  62.  
  63. public int dmDisplayFlags;
  64. public int dmDisplayFrequency;
  65.  
  66. public int dmICMMethod;
  67. public int dmICMIntent;
  68. public int dmMediaType;
  69. public int dmDitherType;
  70. public int dmReserved1;
  71. public int dmReserved2;
  72.  
  73. public int dmPanningWidth;
  74. public int dmPanningHeight;
  75. };
  76.  
  77.  
  78.  
  79. class User_32
  80. {
  81. [DllImport("user32.dll")]
  82. public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
  83. [DllImport("user32.dll")]
  84. public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
  85.  
  86. public const int ENUM_CURRENT_SETTINGS = -1;
  87. public const int CDS_UPDATEREGISTRY = 0x01;
  88. public const int CDS_TEST = 0x02;
  89. public const int DISP_CHANGE_SUCCESSFUL = 0;
  90. public const int DISP_CHANGE_RESTART = 1;
  91. public const int DISP_CHANGE_FAILED = -1;
  92. }
  93.  
  94.  
  95.  
  96. public class PrmaryScreenResolution
  97. {
  98. static public string ChangeResolution(int width, int height)
  99. {
  100.  
  101. DEVMODE1 dm = GetDevMode1();
  102.  
  103. if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
  104. {
  105.  
  106. dm.dmPelsWidth = width;
  107. dm.dmPelsHeight = height;
  108.  
  109. int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
  110.  
  111. if (iRet == User_32.DISP_CHANGE_FAILED)
  112. {
  113. return "Unable To Process Your Request. Sorry For This Inconvenience.";
  114. }
  115. else
  116. {
  117. iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
  118. switch (iRet)
  119. {
  120. case User_32.DISP_CHANGE_SUCCESSFUL:
  121. {
  122. return "Success";
  123. }
  124. case User_32.DISP_CHANGE_RESTART:
  125. {
  126. return "You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode.";
  127. }
  128. default:
  129. {
  130. return "Failed To Change The Resolution";
  131. }
  132. }
  133.  
  134. }
  135.  
  136.  
  137. }
  138. else
  139. {
  140. return "Failed To Change The Resolution.";
  141. }
  142. }
  143.  
  144. private static DEVMODE1 GetDevMode1()
  145. {
  146. DEVMODE1 dm = new DEVMODE1();
  147. dm.dmDeviceName = new String(new char[32]);
  148. dm.dmFormName = new String(new char[32]);
  149. dm.dmSize = (short)Marshal.SizeOf(dm);
  150. return dm;
  151. }
  152. }
  153. }
  154.  
  155. "@
  156.  
  157. Add-Type $pinvokeCode -ErrorAction SilentlyContinue
  158. [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height)
  159. }
  160.  
  161. set-screenresolution -width 1920 -height 1080
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement