public static void SetPrimaryTV() { WinApi.DisplaySetting_Results result = 0; string tv = GetScreenName(3); string monBottom = GetScreenName(2); string monTop = GetScreenName(0); //------------------------------------------------------------------------------------------------- result = SetScreenPosition(monBottom, 1920, 0, false); Console.WriteLine(String.Format("Action for {0} result: {1}", monBottom, result.ToString())); result = SetScreenPosition(monTop, 1920, -1152, false); Console.WriteLine(String.Format("Action for {0} result: {1}", monTop, result.ToString())); result = SetScreenPosition(tv, 0, 0, true); Console.WriteLine(String.Format("Action for {0} result: {1}", tv, result.ToString())); }
static void Main(string[] args) { List <WinApi.DISPLAY_DEVICE> devices = new List <WinApi.DISPLAY_DEVICE>(); List <WinApi.DEVMODE> devModes = new List <WinApi.DEVMODE>(); bool done = false; int curID = 0; do { WinApi.DISPLAY_DEVICE ddOne = new WinApi.DISPLAY_DEVICE(); WinApi.DISPLAY_DEVICE ddTwo = new WinApi.DISPLAY_DEVICE(); ddOne.cb = Marshal.SizeOf(ddOne); ddTwo.cb = Marshal.SizeOf(ddTwo); bool t = WinApi.User_32.EnumDisplayDevices(null, curID, ref ddOne, 0); if (!t) { break; } t = WinApi.User_32.EnumDisplayDevices(ddOne.DeviceName, 0, ref ddTwo, 0); ddOne.DeviceString = ddTwo.DeviceString; WinApi.DEVMODE test = new WinApi.DEVMODE(); WinApi.User_32.EnumDisplaySettings("Test", 4, ref test); int succ = WinApi.User_32.EnumDisplaySettings(ddOne.DeviceName, (-1), ref test); devModes.Add(test); devices.Add(ddOne); curID++; } while (!done); for (int i = 0; i < devices.Count; i++) { Console.WriteLine(devices.ElementAt(i)); Console.WriteLine(devModes.ElementAt(i)); } foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens) { Console.WriteLine(screen.DeviceName); } Console.WriteLine("---------------------------------------------------------------------------"); Console.WriteLine("Changing: " + devices.ElementAt(1)); Console.WriteLine(devModes.ElementAt(1)); Console.WriteLine("---------------------------------------------------------------------------"); Console.ReadKey(); WinApi.DEVMODE d = devModes.ElementAt(1); //d.dmFields = WinApi.DEVMODE_Flags.DM_PELSWIDTH | WinApi.DEVMODE_Flags.DM_PELSHEIGHT; d.dmPelsHeight = 0; d.dmPelsWidth = 0; //d.dmFields |= WinApi.DEVMODE_Flags.DM_POSITION; d.dmPosition.x = 0; Console.WriteLine("Changing the location to: " + d.dmPosition); WinApi.DisplaySetting_Results success = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(devices.ElementAt(1).DeviceName, ref d, (IntPtr)null, ((int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY | (int)WinApi.DeviceFlags.CDS_NORESET), (IntPtr)null); Console.WriteLine("Success: " + success); printValues(); Console.ReadKey(); }
public static void SetPrimary(int dn) { string[] devName = new string[3]; if (dn == 1 || dn == 2) { if (dn == 1) { devName[1] = "\\\\.\\DISPLAY" + 1; devName[2] = "\\\\.\\DISPLAY" + 2; } if (dn == 2) { devName[1] = "\\\\.\\DISPLAY" + 2; devName[2] = "\\\\.\\DISPLAY" + 1; } Console.Write("CHANGE PRIMARY DISPLAY TO \\\\.\\DISPLAY" + dn + "\n\n\n"); int deviceID; WinApi.DisplaySetting_Results result = 0; //manual gather - NewPrimary name ---------------------------------------------------- WinApi.DISPLAY_DEVICE ddOne = new WinApi.DISPLAY_DEVICE(); ddOne.cb = Marshal.SizeOf(ddOne); deviceID = 1; WinApi.User_32.EnumDisplayDevices(null, deviceID, ref ddOne, 0); string NewPrimary = devName[1];//ddOne.DeviceName; //manual gather - OldPrimary name ---------------------------------------------------- WinApi.DISPLAY_DEVICE ddThree = new WinApi.DISPLAY_DEVICE(); ddThree.cb = Marshal.SizeOf(ddThree); deviceID = 2; WinApi.User_32.EnumDisplayDevices(null, deviceID, ref ddThree, 0); string OldPrimary = devName[2]; //ACTION 1 start ---------------------------------------------------------------------------- WinApi.DEVMODE ndm1 = NewDevMode(); WinApi.User_32.EnumDisplaySettings(NewPrimary, (int)WinApi.DEVMODE_SETTINGS.ENUM_REGISTRY_SETTINGS, ref ndm1); WinApi.DEVMODE ndm3 = NewDevMode(); ndm3.dmFields = WinApi.DEVMODE_Flags.DM_POSITION; ndm3.dmPosition.x = (int)ndm1.dmPelsWidth; ndm3.dmPosition.y = 0; result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(OldPrimary, ref ndm3, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY | (int)WinApi.DeviceFlags.CDS_NORESET, IntPtr.Zero); Console.Write("Action 1 result:" + result.ToString()); //ACTION 1 end ---------------------------------------------------------------------------- //ACTION 2 start ---------------------------------------------------------------------------- WinApi.DEVMODE ndm2 = NewDevMode(); WinApi.User_32.EnumDisplaySettings(NewPrimary, (int)WinApi.DEVMODE_SETTINGS.ENUM_REGISTRY_SETTINGS, ref ndm2); WinApi.DEVMODE ndm4 = NewDevMode(); ndm4.dmFields = WinApi.DEVMODE_Flags.DM_POSITION; ndm4.dmPosition.x = 0; ndm4.dmPosition.y = 0; result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(NewPrimary, ref ndm4, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY | (int)WinApi.DeviceFlags.CDS_NORESET, IntPtr.Zero); Console.Write("Action 2 result:" + result.ToString()); //ACTION 2 end ---------------------------------------------------------------------------- //ACTION 3 start ---------------------------------------------------------------------------- WinApi.DEVMODE ndm5 = NewDevMode(); result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(OldPrimary, ref ndm5, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, (IntPtr)null); Console.Write("Action 3.1 result:" + result.ToString()); WinApi.DEVMODE ndm6 = NewDevMode(); result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(NewPrimary, ref ndm6, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); Console.Write("Action 3.2 result:" + result.ToString()); //ACTION 3 end ---------------------------------------------------------------------------- } else { Console.Write("Error, Invailed\n"); return; } }