I ran into the same problem, both with C #, and after I advised trying it in C ++. In the end, I found that what is not mentioned in the Microsoft documentation is that the request to install the main monitor will be ignored (but with a successful operation!), Unless you also set the monitor position (0, 0) to DEVMODE structure. Of course, this means that you also need to shift the positions of other monitors so that they stay in one place compared to the new main monitor. In the documentation ( http://msdn.microsoft.com/en-us/library/windows/desktop/dd183413%28v=vs.85%29.aspx ) call ChangeDisplaySettingsEx for each monitor with the CDS_NORESET flag, and then make the final call with all null.
The following code worked for me:
public static void SetAsPrimaryMonitor(uint id) { var device = new DISPLAY_DEVICE(); var deviceMode = new DEVMODE(); device.cb = Marshal.SizeOf(device); NativeMethods.EnumDisplayDevices(null, id, ref device, 0); NativeMethods.EnumDisplaySettings(device.DeviceName, -1, ref deviceMode); var offsetx = deviceMode.dmPosition.x; var offsety = deviceMode.dmPosition.y; deviceMode.dmPosition.x = 0; deviceMode.dmPosition.y = 0; NativeMethods.ChangeDisplaySettingsEx( device.DeviceName, ref deviceMode, (IntPtr)null, (ChangeDisplaySettingsFlags.CDS_SET_PRIMARY | ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY | ChangeDisplaySettingsFlags.CDS_NORESET), IntPtr.Zero); device = new DISPLAY_DEVICE(); device.cb = Marshal.SizeOf(device);
Note that the signature for ChangeDisplaySettingsEx with the DEVMODE construct as the second parameter will obviously not allow you to go into IntPtr.Zero. Create yourself two different signatures for the same external call, i.e.
[DllImport("user32.dll")] public static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam); [DllImport("user32.dll")] public static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, IntPtr lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
Adbailey
source share