screenres.ps1 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # http://blogs.technet.com/b/heyscriptingguy/archive/2010/07/07/hey-scripting-guy-how-can-i-change-my-desktop-monitor-resolution-via-windows-powershell.aspx
  2. Function Set-ScreenResolution {
  3. param (
  4. [Parameter(Mandatory=$true,
  5. Position = 0)]
  6. [int]
  7. $Width,
  8. [Parameter(Mandatory=$true,
  9. Position = 1)]
  10. [int]
  11. $Height
  12. )
  13. $pinvokeCode = @"
  14. using System;
  15. using System.Runtime.InteropServices;
  16. namespace Resolution
  17. {
  18. [StructLayout(LayoutKind.Sequential)]
  19. public struct DEVMODE1
  20. {
  21. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  22. public string dmDeviceName;
  23. public short dmSpecVersion;
  24. public short dmDriverVersion;
  25. public short dmSize;
  26. public short dmDriverExtra;
  27. public int dmFields;
  28. public short dmOrientation;
  29. public short dmPaperSize;
  30. public short dmPaperLength;
  31. public short dmPaperWidth;
  32. public short dmScale;
  33. public short dmCopies;
  34. public short dmDefaultSource;
  35. public short dmPrintQuality;
  36. public short dmColor;
  37. public short dmDuplex;
  38. public short dmYResolution;
  39. public short dmTTOption;
  40. public short dmCollate;
  41. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  42. public string dmFormName;
  43. public short dmLogPixels;
  44. public short dmBitsPerPel;
  45. public int dmPelsWidth;
  46. public int dmPelsHeight;
  47. public int dmDisplayFlags;
  48. public int dmDisplayFrequency;
  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. public int dmPanningWidth;
  56. public int dmPanningHeight;
  57. };
  58. class User_32
  59. {
  60. [DllImport("user32.dll")]
  61. public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
  62. [DllImport("user32.dll")]
  63. public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
  64. public const int ENUM_CURRENT_SETTINGS = -1;
  65. public const int CDS_UPDATEREGISTRY = 0x01;
  66. public const int CDS_TEST = 0x02;
  67. public const int DISP_CHANGE_SUCCESSFUL = 0;
  68. public const int DISP_CHANGE_RESTART = 1;
  69. public const int DISP_CHANGE_FAILED = -1;
  70. }
  71. public class PrmaryScreenResolution
  72. {
  73. static public string ChangeResolution(int width, int height)
  74. {
  75. DEVMODE1 dm = GetDevMode1();
  76. if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
  77. {
  78. dm.dmPelsWidth = width;
  79. dm.dmPelsHeight = height;
  80. int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
  81. if (iRet == User_32.DISP_CHANGE_FAILED)
  82. {
  83. return "Unable To Process Your Request. Sorry For This Inconvenience.";
  84. }
  85. else
  86. {
  87. iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
  88. switch (iRet)
  89. {
  90. case User_32.DISP_CHANGE_SUCCESSFUL:
  91. {
  92. return "Success";
  93. }
  94. case User_32.DISP_CHANGE_RESTART:
  95. {
  96. 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.";
  97. }
  98. default:
  99. {
  100. return "Failed To Change The Resolution";
  101. }
  102. }
  103. }
  104. }
  105. else
  106. {
  107. return "Failed To Change The Resolution.";
  108. }
  109. }
  110. private static DEVMODE1 GetDevMode1()
  111. {
  112. DEVMODE1 dm = new DEVMODE1();
  113. dm.dmDeviceName = new String(new char[32]);
  114. dm.dmFormName = new String(new char[32]);
  115. dm.dmSize = (short)Marshal.SizeOf(dm);
  116. return dm;
  117. }
  118. }
  119. }
  120. "@
  121. Add-Type $pinvokeCode -ErrorAction SilentlyContinue
  122. [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height)
  123. }
  124. Set-ScreenResolution 1920 1080