Advertisement
briannovius

ScreenChange

Mar 30th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. Function Set-ScreenResolutionAndOrientation {
  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-ScreenResolutionAndOrientation
  10. #>
  11.  
  12. $pinvokeCode = @"
  13.  
  14. using System;
  15. using System.Runtime.InteropServices;
  16.  
  17. namespace Resolution
  18. {
  19.  
  20. [StructLayout(LayoutKind.Sequential)]
  21. public struct DEVMODE
  22. {
  23. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
  24. public string dmDeviceName;
  25.  
  26. public short dmSpecVersion;
  27. public short dmDriverVersion;
  28. public short dmSize;
  29. public short dmDriverExtra;
  30. public int dmFields;
  31. public int dmPositionX;
  32. public int dmPositionY;
  33. public int dmDisplayOrientation;
  34. public int dmDisplayFixedOutput;
  35. public short dmColor;
  36. public short dmDuplex;
  37. public short dmYResolution;
  38. public short dmTTOption;
  39. public short dmCollate;
  40.  
  41. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  42. public string dmFormName;
  43.  
  44. public short dmLogPixels;
  45. public short dmBitsPerPel;
  46. public int dmPelsWidth;
  47. public int dmPelsHeight;
  48. public int dmDisplayFlags;
  49. public int dmDisplayFrequency;
  50. public int dmICMMethod;
  51. public int dmICMIntent;
  52. public int dmMediaType;
  53. public int dmDitherType;
  54. public int dmReserved1;
  55. public int dmReserved2;
  56. public int dmPanningWidth;
  57. public int dmPanningHeight;
  58. };
  59.  
  60. class NativeMethods
  61. {
  62. [DllImport("user32.dll")]
  63. public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
  64. [DllImport("user32.dll")]
  65. public static extern int ChangeDisplaySettings(ref DEVMODE 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. public const int DMDO_DEFAULT = 0;
  74. public const int DMDO_90 = 1;
  75. public const int DMDO_180 = 2;
  76. public const int DMDO_270 = 3;
  77. }
  78.  
  79.  
  80.  
  81. public class PrmaryScreenResolution
  82. {
  83. static public string ChangeResolution()
  84. {
  85.  
  86. DEVMODE dm = GetDevMode();
  87.  
  88. if (0 != NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
  89. {
  90.  
  91. // swap width and height
  92. int temp = dm.dmPelsHeight;
  93. dm.dmPelsHeight = dm.dmPelsWidth;
  94. dm.dmPelsWidth = temp;
  95.  
  96. // determine new orientation based on the current orientation
  97. switch(dm.dmDisplayOrientation)
  98. {
  99. case NativeMethods.DMDO_DEFAULT:
  100. dm.dmDisplayOrientation = NativeMethods.DMDO_270;
  101. break;
  102. case NativeMethods.DMDO_270:
  103. dm.dmDisplayOrientation = NativeMethods.DMDO_180;
  104. break;
  105. case NativeMethods.DMDO_180:
  106. dm.dmDisplayOrientation = NativeMethods.DMDO_90;
  107. break;
  108. case NativeMethods.DMDO_90:
  109. dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT;
  110. break;
  111. default:
  112. // unknown orientation value
  113. // add exception handling here
  114. break;
  115. }
  116.  
  117.  
  118. int iRet = NativeMethods.ChangeDisplaySettings(ref dm, NativeMethods.CDS_TEST);
  119.  
  120. if (iRet == NativeMethods.DISP_CHANGE_FAILED)
  121. {
  122. return "Unable To Process Your Request. Sorry For This Inconvenience.";
  123. }
  124. else
  125. {
  126. iRet = NativeMethods.ChangeDisplaySettings(ref dm, NativeMethods.CDS_UPDATEREGISTRY);
  127. switch (iRet)
  128. {
  129. case NativeMethods.DISP_CHANGE_SUCCESSFUL:
  130. {
  131. return "Success";
  132. }
  133. case NativeMethods.DISP_CHANGE_RESTART:
  134. {
  135. 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.";
  136. }
  137. default:
  138. {
  139. return "Failed To Change The Resolution";
  140. }
  141. }
  142.  
  143. }
  144.  
  145.  
  146. }
  147. else
  148. {
  149. return "Failed To Change The Resolution.";
  150. }
  151. }
  152.  
  153. private static DEVMODE GetDevMode()
  154. {
  155. DEVMODE dm = new DEVMODE();
  156. dm.dmDeviceName = new String(new char[32]);
  157. dm.dmFormName = new String(new char[32]);
  158. dm.dmSize = (short)Marshal.SizeOf(dm);
  159. return dm;
  160. }
  161. }
  162. }
  163.  
  164. "@
  165.  
  166. Add-Type $pinvokeCode -ErrorAction SilentlyContinue
  167. [Resolution.PrmaryScreenResolution]::ChangeResolution()
  168. }
  169.  
  170. Set-ScreenResolutionAndOrientation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement