public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
public bool SwitchPrimaryDisplay(string deviceName) { var displays = Enumerate(); var device = displays.Find(d => d.DeviceName.Equals(deviceName)); if (!device.DeviceName.Equals(deviceName)) { return(false); } var deviceMode = new DEVMODE(); if (!EnumDisplaySettings(device.DeviceName, -1, ref deviceMode)) { return(false); } var offsetx = deviceMode.dmPosition.x; var offsety = deviceMode.dmPosition.y; if (offsetx == 0 && offsety == 0) { return(true); } deviceMode.dmPosition.x = 0; deviceMode.dmPosition.y = 0; if (ChangeDisplaySettingsEx( device.DeviceName, ref deviceMode, (IntPtr)null, (ChangeDisplaySettingsFlags.CDS_SET_PRIMARY | ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY | ChangeDisplaySettingsFlags.CDS_NORESET), IntPtr.Zero) != DISP_CHANGE.Successful) { return(false); } var otherDisplays = displays.FindAll(d => !d.DeviceName.Equals(deviceName)); foreach (var otherDisplay in otherDisplays) { var otherMode = new DEVMODE(); if (!EnumDisplaySettings(otherDisplay.DeviceName, -1, ref otherMode)) { return(false); } otherMode.dmPosition.x -= offsetx; otherMode.dmPosition.y -= offsety; if (ChangeDisplaySettingsEx( otherDisplay.DeviceName, ref otherMode, (IntPtr)null, (ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY | ChangeDisplaySettingsFlags.CDS_NORESET), IntPtr.Zero) != DISP_CHANGE.Successful) { return(false); } } return(ChangeDisplaySettingsEx(null, IntPtr.Zero, (IntPtr)null, ChangeDisplaySettingsFlags.CDS_NONE, (IntPtr)null) == DISP_CHANGE.Successful); }
public static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);