/// <summary> /// Set the screen resolution to specified size. Will throw a <see cref="ResolutionChangeException" /> if unable to set to desired resolution. /// </summary> /// <exception cref="ResolutionChangeException" /> /// <param name="size">Desired screen resolution</param> public static void SetScreenResolution(Size size) { DEVMODE1 dm = new DEVMODE1(); dm.dmDeviceName = new String(new char[32]); dm.dmFormName = new String(new char[32]); dm.dmSize = (short)Marshal.SizeOf(dm); if (pInvoke.EnumDisplaySettings(null, DisplaySettingsConstants.ENUM_CURRENT_SETTINGS, ref dm) != 0) { dm.dmPelsWidth = size.Width; dm.dmPelsHeight = size.Height; int iRet = pInvoke.ChangeDisplaySettings(ref dm, DisplaySettingsConstants.CDS_TEST); if (iRet == DisplaySettingsConstants.DISP_CHANGE_FAILED) throw new ResolutionChangeException( "Call to ChangeDisplaySettings with CDS_TEST failed"); else { iRet = pInvoke.ChangeDisplaySettings(ref dm, DisplaySettingsConstants.CDS_UPDATEREGISTRY); switch (iRet) { case DisplaySettingsConstants.DISP_CHANGE_SUCCESSFUL: return; // change successful case DisplaySettingsConstants.DISP_CHANGE_RESTART: throw new ResolutionChangeException( "A reboot must take place for the changes to take affect"); default: throw new ResolutionChangeException( "Call to ChangeDisplaySettings with CDS_UPDATEREGISTRY failed"); } } } throw new ResolutionChangeException("Error on call to Win32Support.EnumDisplaySettings"); }
/// <summary> /// Set the screen resolution to specified size. Will throw a <see cref="ResolutionChangeException" /> if unable to set to desired resolution. /// </summary> /// <exception cref="ResolutionChangeException" /> /// <param name="size">Desired screen resolution</param> public static void SetScreenResolution(Size size) { DEVMODE1 dm = new DEVMODE1(); dm.dmDeviceName = new String(new char[32]); dm.dmFormName = new String(new char[32]); dm.dmSize = (short)Marshal.SizeOf(dm); if (pInvoke.EnumDisplaySettings(null, DisplaySettingsConstants.ENUM_CURRENT_SETTINGS, ref dm) != 0) { dm.dmPelsWidth = size.Width; dm.dmPelsHeight = size.Height; int iRet = pInvoke.ChangeDisplaySettings(ref dm, DisplaySettingsConstants.CDS_TEST); if (iRet == DisplaySettingsConstants.DISP_CHANGE_FAILED) { throw new ResolutionChangeException( "Call to ChangeDisplaySettings with CDS_TEST failed"); } else { iRet = pInvoke.ChangeDisplaySettings(ref dm, DisplaySettingsConstants.CDS_UPDATEREGISTRY); switch (iRet) { case DisplaySettingsConstants.DISP_CHANGE_SUCCESSFUL: return; // change successful case DisplaySettingsConstants.DISP_CHANGE_RESTART: throw new ResolutionChangeException( "A reboot must take place for the changes to take affect"); default: throw new ResolutionChangeException( "Call to ChangeDisplaySettings with CDS_UPDATEREGISTRY failed"); } } } throw new ResolutionChangeException("Error on call to Win32Support.EnumDisplaySettings"); }
public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);