/// <summary> /// Changes the current display settings with the new settings provided. May throw InvalidOperationException if failed. Check the exception message for more details. /// </summary> /// <param name="set">The new settings.</param> /// <remarks> /// Internally calls ChangeDisplaySettings() native function. /// </remarks> public static void SetDisplaySettings(DisplaySettings set) { SafeNativeMethods.DEVMODE mode = GetDeviceMode(); mode.dmPelsWidth = (uint)set.Width; mode.dmPelsHeight = (uint)set.Height; mode.dmDisplayOrientation = (uint)set.Orientation; mode.dmBitsPerPel = (uint)set.BitCount; mode.dmDisplayFrequency = (uint)set.Frequency; DisplayChangeResult result = (DisplayChangeResult)SafeNativeMethods.ChangeDisplaySettings(ref mode, 0); string msg = null; Assembly asm = Assembly.GetExecutingAssembly(); switch (result) { case DisplayChangeResult.BadDualView: msg = InvalidOperation_Disp_Change_BadDualView; break; case DisplayChangeResult.BadParam: msg = InvalidOperation_Disp_Change_BadParam; break; case DisplayChangeResult.BadFlags: msg = InvalidOperation_Disp_Change_BadFlags; break; case DisplayChangeResult.NotUpdated: msg = InvalidOperation_Disp_Change_NotUpdated; break; case DisplayChangeResult.BadMode: msg = InvalidOperation_Disp_Change_BadMode; break; case DisplayChangeResult.Failed: msg = InvalidOperation_Disp_Change_Failed; break; case DisplayChangeResult.Restart: msg = InvalidOperation_Disp_Change_Restart; break; } if (msg != null) { throw new InvalidOperationException(msg); } }
/// <summary> /// Changes the current display settings with the new settings provided. May throw InvalidOperationException if failed. Check the exception message for more details. /// </summary> /// <param name="set">The new settings.</param> /// <remarks> /// Internally calls ChangeDisplaySettings() native function. /// </remarks> public static void SetDisplaySettings(DisplaySettings set) { SafeNativeMethods.DEVMODE mode = GetDeviceMode(); mode.dmPelsWidth = (uint)set.Width; mode.dmPelsHeight = (uint)set.Height; mode.dmDisplayOrientation = (uint)set.Orientation; mode.dmBitsPerPel = (uint)set.BitCount; mode.dmDisplayFrequency = (uint)set.Frequency; DisplayChangeResult result = (DisplayChangeResult)SafeNativeMethods.ChangeDisplaySettings(ref mode, 0); string msg = null; switch (result) { case DisplayChangeResult.BadDualView: msg = "The settings change was unsuccessful because system is DualView capable."; break; case DisplayChangeResult.BadParam: msg = "An invalid parameter was passed in. This can include an invalid flag or combination of flags."; break; case DisplayChangeResult.BadFlags: msg = "An invalid set of flags was passed in."; break; case DisplayChangeResult.NotUpdated: msg = "Unable to write settings to the registry."; break; case DisplayChangeResult.BadMode: msg = "The graphics mode is not supported."; break; case DisplayChangeResult.Failed: msg = "The display driver failed the specified graphics mode."; break; case DisplayChangeResult.Restart: msg = "The computer must be restarted in order for the graphics mode to work."; break; } if (msg != null) { throw new InvalidOperationException(msg); } }
private string ChangeMainDisplayPosition(string name, int x, int y) { DEVMODE dm = GetDEVMODE(); //if (0 != Native.Methods.EnumDisplaySettings(@"\\.\DISPLAY1", Native.Methods.ENUM_CURRENT_SETTINGS, ref dm)) if (0 != Native.Methods.EnumDisplaySettings(name, Native.Methods.ENUM_CURRENT_SETTINGS, ref dm)) { dm.dmPositionX = x; dm.dmPositionY = y; DisplayChangeResult iRet = Native.Methods.ChangeDisplaySettings(ref dm, ChangeDisplaySettingsFlags.CDS_TEST); if (iRet == DisplayChangeResult.Failed) { return("Unable To Process Your Request. Sorry For This Inconvenience."); } else { iRet = Native.Methods.ChangeDisplaySettings(ref dm, 0); switch (iRet) { case DisplayChangeResult.Successful: { return("Success"); } case DisplayChangeResult.Restart: { 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."); } default: { return("Failed To Change The Position"); } } } } else { return("Failed To Change The Position."); } }