// ReSharper restore InconsistentNaming public RotateResult RotateScreen(RotateDegress pRotateDegrees, string deviceName = null) { var dm = new DEVMODE { dmDeviceName = new string((char)0, 32), dmFormName = new string((char)0, 32) }; dm.dmSize = Convert.ToInt16(Marshal.SizeOf(dm)); if ((UnsafeNativeMethods.EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref dm) != 0)) { // determine unrotated pixel sizes switch ((RotateDegress)dm.dmDisplayOrientation) { case RotateDegress.Degrees90: case RotateDegress.Degrees270: SwapExtents(ref dm.dmPelsHeight, ref dm.dmPelsWidth); break; } // set rotation parameters dm.dmDisplayOrientation = Convert.ToInt16(pRotateDegrees); switch (pRotateDegrees) { case RotateDegress.Degrees90: case RotateDegress.Degrees270: SwapExtents(ref dm.dmPelsHeight, ref dm.dmPelsWidth); break; } dm.dmFields = DM_DISPLAYORIENTATION | DM_PELSHEIGHT | DM_PELSWIDTH; // query change var rotRet = (RotateResult)UnsafeNativeMethods.ChangeDisplaySettings(ref dm, CDS_TEST); if (rotRet == RotateResult.RotateFailed) { return RotateResult.RotateFailed; } //enact change rotRet = (RotateResult)UnsafeNativeMethods.ChangeDisplaySettings(ref dm, CDS_UPDATEREGISTRY); switch (rotRet) { case RotateResult.RotateSuccessful: case RotateResult.RotateRequiresRestart: return rotRet; default: return RotateResult.RotateFailed; } } else { return RotateResult.RotateFailed; } }
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
public static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);