private static string changeSettings(DisplaySettings set, DisplayManager.DisplayDevice dev, bool prim) { return DisplayManager.SetDisplaySettings(set, dev, prim); }
/// <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 string SetDisplaySettings(DisplaySettings set, DisplayDevice dev, bool primary) { SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE(); 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; if (primary) { mode.dmPosition.x = 0; mode.dmPosition.y = 0; } else { mode.dmPosition.x = -set.Width -1; mode.dmPosition.y = 0; } uint flags; if (primary) flags = (uint)(CDSFlags.CDS_RESET | CDSFlags.CDS_UPDATEREGISTRY | CDSFlags.CDS_SET_PRIMARY ); else flags = (uint)(CDSFlags.CDS_RESET | CDSFlags.CDS_UPDATEREGISTRY); mode.dmFields = (uint)(SafeNativeMethods.DM.Position | SafeNativeMethods.DM.PelsHeight | SafeNativeMethods.DM.PelsWidth| SafeNativeMethods.DM.DisplayFrequency | SafeNativeMethods.DM.DisplayOrientation| SafeNativeMethods.DM.BitsPerPixel); mode.dmSize = (ushort)Marshal.SizeOf(mode); DisplayChangeResult result = (DisplayChangeResult) SafeNativeMethods.ChangeDisplaySettingsEx(dev.DeviceName, ref mode, IntPtr.Zero, flags, IntPtr.Zero); string msg = "Successfully changed the settings!"; switch (result) { case DisplayChangeResult.BadDualView: msg = Properties.Resources.InvalidOperation_Disp_Change_BadDualView; break; case DisplayChangeResult.BadParam: msg = Properties.Resources.InvalidOperation_Disp_Change_BadParam; break; case DisplayChangeResult.BadFlags: msg = Properties.Resources.InvalidOperation_Disp_Change_BadFlags; break; case DisplayChangeResult.NotUpdated: msg = Properties.Resources.InvalidOperation_Disp_Change_NotUpdated; break; case DisplayChangeResult.BadMode: msg = Properties.Resources.InvalidOperation_Disp_Change_BadMode; break; case DisplayChangeResult.Failed: msg = Properties.Resources.InvalidOperation_Disp_Change_Failed; break; case DisplayChangeResult.Restart: msg = Properties.Resources.InvalidOperation_Disp_Change_Restart; break; } return msg; }