Guest User

Untitled

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