public static bool Rotate(uint DisplayNumber, ScreenOrientation orientation) { if (DisplayNumber == 0) { throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "First display is 1."); } bool result = false; DISPLAY_DEVICE d = new DISPLAY_DEVICE(); DEVMODE dm = new DEVMODE(); d.cb = Marshal.SizeOf(d); if (!NativeMethods.EnumDisplayDevices(null, DisplayNumber - 1, ref d, 0)) { throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "Number is greater than connected displays."); } if (0 != NativeMethods.EnumDisplaySettings( d.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) { if ((dm.dmDisplayOrientation + (int)orientation) % 2 == 1) // Need to swap height and width? { int temp = dm.dmPelsHeight; dm.dmPelsHeight = dm.dmPelsWidth; dm.dmPelsWidth = temp; } switch (orientation) { case ScreenOrientation.Angle90: dm.dmDisplayOrientation = NativeMethods.DMDO_270; break; case ScreenOrientation.Angle180: dm.dmDisplayOrientation = NativeMethods.DMDO_180; break; case ScreenOrientation.Angle270: dm.dmDisplayOrientation = NativeMethods.DMDO_90; break; case ScreenOrientation.Angle0: dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT; break; default: break; } DISP_CHANGE ret = NativeMethods.ChangeDisplaySettingsEx( d.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); result = ret == 0; } return(result); }
public static bool Rotate(uint DisplayNumber, Orientations Orientation) { bool result = false; DISPLAY_DEVICE d = new DISPLAY_DEVICE(); DEVMODE dm = new DEVMODE(); d.cb = Marshal.SizeOf(d); if (!Display.EnumDisplayDevices(null, DisplayNumber, ref d, 0)) { throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "Number is greater than connected displays."); } if (Display.EnumDisplaySettings( d.DeviceName, Display.ENUM_CURRENT_SETTINGS, ref dm) != 0) { if ((dm.dmDisplayOrientation + (int)Orientation) % 2 == 1) // Need to swap height and width? { int temp = dm.dmPelsHeight; dm.dmPelsHeight = dm.dmPelsWidth; dm.dmPelsWidth = temp; } switch (Orientation) { case Orientations.DEGREES_CW_90: dm.dmDisplayOrientation = 3; break; case Orientations.DEGREES_CW_180: dm.dmDisplayOrientation = 2; break; case Orientations.DEGREES_CW_270: dm.dmDisplayOrientation = 1; break; case Orientations.DEGREES_CW_0: dm.dmDisplayOrientation = 0; break; default: break; } DISP_CHANGE ret = Display.ChangeDisplaySettingsEx( d.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); result = ret == 0; } return(result); }
public static bool Rotate(uint DisplayNumber, Orientations Orientation) { if (DisplayNumber == 0) { throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "Invalid num of Display."); } DISPLAY_DEVICE d = new DISPLAY_DEVICE(); DEVMODE dm = new DEVMODE(); d.cb = Marshal.SizeOf(d); if (!NativeMethods.EnumDisplayDevices(null, DisplayNumber - 1, ref d, 0)) { throw new ArgumentOutOfRangeException("DisplayNumber", DisplayNumber, "Too big num."); } if (0 != NativeMethods.EnumDisplaySettings(d.DeviceName, -1, ref dm)) { if ((dm.dmDisplayOrientation + (int)Orientation) % 2 == 1) { int temp = dm.dmPelsHeight; dm.dmPelsHeight = dm.dmPelsWidth; dm.dmPelsWidth = temp; } switch (Orientation) { case Orientations.DEGREES_0: dm.dmDisplayOrientation = 0; break; case Orientations.DEGREES_90: dm.dmDisplayOrientation = 1; break; case Orientations.DEGREES_180: dm.dmDisplayOrientation = 2; break; case Orientations.DEGREES_270: dm.dmDisplayOrientation = 3; break; default: break; } DISP_CHANGE ret = NativeMethods.ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); return(ret == 0); } return(false); }
static void Aux(string[] args) { uint deviceID = 1; // zero origin (i.e. 1 means DISPLAY2) DISPLAY_DEVICE d = new DISPLAY_DEVICE(); DEVMODE dm = new DEVMODE(); d.cb = Marshal.SizeOf(d); d.StateFlags = DisplayDeviceStateFlags.MirroringDriver; NativeMethods.EnumDisplayDevices(null, deviceID, ref d, 0); /* * if (0 != NativeMethods.EnumDisplaySettings( * d.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) * { * int temp = dm.dmPelsHeight; * dm.dmPelsHeight = dm.dmPelsWidth; * dm.dmPelsWidth = temp; * * * * switch (dm.dmDisplayOrientation) * { * case NativeMethods.DMDO_DEFAULT: * dm.dmDisplayOrientation = NativeMethods.DMDO_270; * break; * case NativeMethods.DMDO_270: * dm.dmDisplayOrientation = NativeMethods.DMDO_180; * break; * case NativeMethods.DMDO_180: * dm.dmDisplayOrientation = NativeMethods.DMDO_90; * break; * case NativeMethods.DMDO_90: * dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT; * break; * default: * break; * } */ DISP_CHANGE iRet = NativeMethods.ChangeDisplaySettingsEx( d.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); // if (iRet != DISP_CHANGE.Successful) handle error // } }
public static bool Rotate(string DeviceName, Orientations Orientation) { bool result = false; DEVMODE dm = new DEVMODE(); if (0 != NativeMethods.EnumDisplaySettings( DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) { if ((dm.dmDisplayOrientation + (int)Orientation) % 2 == 1) // Need to swap height and width? { int temp = dm.dmPelsHeight; dm.dmPelsHeight = dm.dmPelsWidth; dm.dmPelsWidth = temp; } switch (Orientation) { case Orientations.DEGREES_CW_90: dm.dmDisplayOrientation = NativeMethods.DMDO_270; break; case Orientations.DEGREES_CW_180: dm.dmDisplayOrientation = NativeMethods.DMDO_180; break; case Orientations.DEGREES_CW_270: dm.dmDisplayOrientation = NativeMethods.DMDO_90; break; case Orientations.DEGREES_CW_0: dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT; break; default: break; } DISP_CHANGE ret = NativeMethods.ChangeDisplaySettingsEx( DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); result = ret == 0; } return(result); }
private static string GetMessageForCode(DISP_CHANGE code) { string message; switch (code) { case DISP_CHANGE.Successful: message = "Resolution updated."; break; case DISP_CHANGE.Restart: message = "A restart is required for this resolution to take effect."; break; case DISP_CHANGE.BadMode: message = $"resolution is not valid."; break; case DISP_CHANGE.BadDualView: message = "The settings change was unsuccessful because system is DualView capable."; break; case DISP_CHANGE.BadFlags: message = "An invalid set of flags was passed in."; break; case DISP_CHANGE.BadParam: message = "An invalid parameter was passed in. This can include an invalid flag or combination of flags."; break; case DISP_CHANGE.Failed: message = "Resolution failed to update."; break; case DISP_CHANGE.NotUpdated: message = "Unable to write settings to the registry."; break; default: message = "Unknown return value from ChangeDisplaySettings API."; break; } return(message); }
public static DISP_CHANGE RotateAllScreens(Orientations orientation) { //if one fails, we return the result of that failed rotation DISP_CHANGE result = DISP_CHANGE.Successful; try { uint i = 0; while (++i <= 64) { var tempResult = Rotate(i, orientation); if (tempResult != DISP_CHANGE.Successful) { result = tempResult; } } } catch (ArgumentOutOfRangeException) { // Everything is fine, just reached the last display } return(result); }
public static bool ChangeResolution(IntPtr windowHandle, Resolution res, bool permanent) { int i = 0; DEVMODE cDm = new DEVMODE(); //cDm.dmDeviceName = new String(new char[32]); //cDm.dmFormName = new String(new char[32]); cDm.dmSize = (short)Marshal.SizeOf(cDm); //FileLogger.Log("DEVMODE Size: {0}", Marshal.SizeOf(cDm)); Screen hostScreen = GetScreenFromHandle(windowHandle); NativeMethods.EnumDisplaySettings(hostScreen.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref cDm); if (cDm.dmBitsPerPel == res.PixelDepth && cDm.dmPelsWidth == res.Width && cDm.dmPelsHeight == res.Height && cDm.dmDisplayFrequency == res.Rate && ((cDm.dmDisplayFlags & NativeMethods.DM_INTERLACED) == NativeMethods.DM_INTERLACED) == res.Interlaced && cDm.dmDisplayFixedOutput == res.FixedOutput) { return(false); } DEVMODE dm = new DEVMODE(); //dm.dmDeviceName = new String(new char[32]); //dm.dmFormName = new String(new char[32]); dm.dmSize = (short)Marshal.SizeOf(dm); while (0 != NativeMethods.EnumDisplaySettings(hostScreen.DeviceName, i, ref dm)) { if (dm.dmBitsPerPel == res.PixelDepth && dm.dmPelsWidth == res.Width && dm.dmPelsHeight == res.Height && dm.dmDisplayFrequency == res.Rate && ((dm.dmDisplayFlags & NativeMethods.DM_INTERLACED) == NativeMethods.DM_INTERLACED) == res.Interlaced && dm.dmDisplayFixedOutput == res.FixedOutput) { DISP_CHANGE iRet = NativeMethods.ChangeDisplaySettingsEx(hostScreen.DeviceName, ref dm, IntPtr.Zero, CDS.Test, IntPtr.Zero); if (iRet == DISP_CHANGE.Successful) { if (permanent) { iRet = NativeMethods.ChangeDisplaySettingsEx(hostScreen.DeviceName, ref dm, IntPtr.Zero, CDS.UpdateRegistry, IntPtr.Zero); } else { iRet = NativeMethods.ChangeDisplaySettingsEx(hostScreen.DeviceName, ref dm, IntPtr.Zero, CDS.Dynamic, IntPtr.Zero); //IntPtr nRes = IntPtr.Zero; //try //{ // nRes = Marshal.AllocHGlobal(Marshal.SizeOf(dm)); // Marshal.StructureToPtr(dm, nRes, true); // iRet = NativeMethods.ChangeDisplaySettings(nRes, CDS.Dynamic); //} //finally //{ // if (nRes != IntPtr.Zero) // Marshal.FreeHGlobal(nRes); //} } } return(iRet == DISP_CHANGE.Successful); } i++; } return(false); }
private static string GetMessageForCode(DISP_CHANGE code) { string message; switch (code) { case DISP_CHANGE.Successful: message = "Resolution updated."; break; case DISP_CHANGE.Restart: message = "A restart is required for this resolution to take effect."; break; case DISP_CHANGE.BadMode: message = $"resolution is not valid."; break; case DISP_CHANGE.BadDualView: message = "The settings change was unsuccessful because system is DualView capable."; break; case DISP_CHANGE.BadFlags: message = "An invalid set of flags was passed in."; break; case DISP_CHANGE.BadParam: message = "An invalid parameter was passed in. This can include an invalid flag or combination of flags."; break; case DISP_CHANGE.Failed: message = "Resolution failed to update."; break; case DISP_CHANGE.NotUpdated: message = "Unable to write settings to the registry."; break; default: message = "Unknown return value from ChangeDisplaySettings API."; break; } return message; }