Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Swap Primary Monitors (for 2 monitor setups)

Description
This script will make the opposite monitor Primary in 2 monitor setups.
Language
C#.net
Minimum Version
Created By
MaoYanwen298975
Contributors
-
Date Created
May 13, 2015
Date Last Modified
May 13, 2015

Scripted Function (Macro) Code

using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System;
using System.Drawing;
using System.Windows.Forms;

public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		//find a screen that isn't the primary screen
		Screen unPrimary = null;
		foreach (Screen screen in Screen.AllScreens)
		{
			if (screen.Primary)
				continue;

			unPrimary = screen;
			break;
		}

		//if this is null, the computer only has one screen
		if (unPrimary == null)
			return;

		ScreenHelper.SwitchPrimaryScreen(unPrimary, Screen.PrimaryScreen);
	}
}

public class WinApi
{
	public const Int32 CCHDEVICENAME = 32;
	public const Int32 CCHFORMNAME = 32;

	public enum DEVMODE_SETTINGS
	{
		ENUM_CURRENT_SETTINGS = (-1),
		ENUM_REGISTRY_SETTINGS = (-2)
	}
	
	[Flags()]
	public enum DisplayDeviceStateFlags : int
	{
		AttachedToDesktop = 0x1,
		MultiDriver = 0x2,
		PrimaryDevice = 0x4,
		MirroringDriver = 0x8,
		VGACompatible = 0x10,
		Removable = 0x20,
		ModesPruned = 0x8000000,
		Remote = 0x4000000,
		Disconnect = 0x2000000
	}

	public enum Display_Device_Stateflags
	{
		DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x1,
		DISPLAY_DEVICE_MIRRORING_DRIVER = 0x8,
		DISPLAY_DEVICE_MODESPRUNED = 0x8000000,
		DISPLAY_DEVICE_MULTI_DRIVER = 0x2,
		DISPLAY_DEVICE_PRIMARY_DEVICE = 0x4,
		DISPLAY_DEVICE_VGA_COMPATIBLE = 0x10
	}

	public enum DeviceFlags
	{
		CDS_FULLSCREEN = 0x4,
		CDS_GLOBAL = 0x8,
		CDS_NORESET = 0x10000000,
		CDS_RESET = 0x40000000,
		CDS_SET_PRIMARY = 0x10,
		CDS_TEST = 0x2,
		CDS_UPDATEREGISTRY = 0x1,
		CDS_VIDEOPARAMETERS = 0x20,
	}

	public enum DEVMODE_Flags
	{
		DM_BITSPERPEL = 0x40000,
		DM_DISPLAYFLAGS = 0x200000,
		DM_DISPLAYFREQUENCY = 0x400000,
		DM_PELSHEIGHT = 0x100000,
		DM_PELSWIDTH = 0x80000,
		DM_POSITION = 0x20
	}

	public enum DisplaySetting_Results
	{
		DISP_CHANGE_BADFLAGS = -4,
		DISP_CHANGE_BADMODE = -2,
		DISP_CHANGE_BADPARAM = -5,
		DISP_CHANGE_FAILED = -1,
		DISP_CHANGE_NOTUPDATED = -3,
		DISP_CHANGE_RESTART = 1,
		DISP_CHANGE_SUCCESSFUL = 0
	}

	[StructLayout(LayoutKind.Sequential)]
	public struct POINTL
	{
		[MarshalAs(UnmanagedType.I4)] public int x;
		[MarshalAs(UnmanagedType.I4)] public int y;
	}

	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
	public struct DEVMODE
	{
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmDeviceName;
		[MarshalAs(UnmanagedType.U2)] public UInt16 dmSpecVersion;
		[MarshalAs(UnmanagedType.U2)] public UInt16 dmDriverVersion;
		[MarshalAs(UnmanagedType.U2)] public UInt16 dmSize;
		[MarshalAs(UnmanagedType.U2)] public UInt16 dmDriverExtra;
		[MarshalAs(UnmanagedType.U4)] public DEVMODE_Flags dmFields;
		public POINTL dmPosition;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmDisplayOrientation;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmDisplayFixedOutput;
		[MarshalAs(UnmanagedType.I2)] public Int16 dmColor;
		[MarshalAs(UnmanagedType.I2)] public Int16 dmDuplex;
		[MarshalAs(UnmanagedType.I2)] public Int16 dmYResolution;
		[MarshalAs(UnmanagedType.I2)] public Int16 dmTTOption;
		[MarshalAs(UnmanagedType.I2)] public Int16 dmCollate;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmFormName;
		[MarshalAs(UnmanagedType.U2)] public UInt16 dmLogPixels;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmBitsPerPel;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmPelsWidth;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmPelsHeight;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmDisplayFlags;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmDisplayFrequency;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmICMMethod;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmICMIntent;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmMediaType;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmDitherType;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmReserved1;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmReserved2;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmPanningWidth;
		[MarshalAs(UnmanagedType.U4)] public UInt32 dmPanningHeight;
	}

	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
	public struct DISPLAY_DEVICE
	{
		[MarshalAs(UnmanagedType.U4)] public int cb;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString;
		[MarshalAs(UnmanagedType.U4)] public DisplayDeviceStateFlags StateFlags;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey;
	}

	public class User_32
	{
		[DllImport("user32.dll")]
		public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, [In] ref DEVMODE lpDevMode, IntPtr hwnd, int dwFlags, IntPtr lParam);
	}

}

public class ScreenHelper
{
	public static void SwitchPrimaryScreen(Screen newPrimary, Screen oldPrimary)
	{
		MoveOldPrimary(newPrimary, oldPrimary);
		MoveNewPrimary(newPrimary, oldPrimary);
		CommitChange(newPrimary, oldPrimary);
	}

	private static void MoveOldPrimary(Screen newPrimary, Screen oldPrimary)
	{
		WinApi.DEVMODE ndm3 = NewDevMode();
		ndm3.dmFields = WinApi.DEVMODE_Flags.DM_POSITION;
		ndm3.dmPosition.x = newPrimary.Bounds.Width;
		ndm3.dmPosition.y = 0;
		WinApi.User_32.ChangeDisplaySettingsEx(oldPrimary.DeviceName, ref ndm3, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY | (int)WinApi.DeviceFlags.CDS_NORESET, IntPtr.Zero);
	}

	private static void MoveNewPrimary(Screen newPrimary, Screen oldPrimary)
	{
		WinApi.DEVMODE ndm4 = NewDevMode();
		ndm4.dmFields = WinApi.DEVMODE_Flags.DM_POSITION;
		ndm4.dmPosition.x = 0;
		ndm4.dmPosition.y = 0;
		WinApi.User_32.ChangeDisplaySettingsEx(newPrimary.DeviceName, ref ndm4, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY | (int)WinApi.DeviceFlags.CDS_NORESET, IntPtr.Zero);
	}

	private static void CommitChange(Screen newPrimary, Screen oldPrimary)
	{
		WinApi.DEVMODE ndm5 = NewDevMode();
		WinApi.User_32.ChangeDisplaySettingsEx(oldPrimary.DeviceName, ref ndm5, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, (IntPtr)null);

		WinApi.DEVMODE ndm6 = NewDevMode();
		WinApi.User_32.ChangeDisplaySettingsEx(newPrimary.DeviceName, ref ndm6, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
	}

	private static WinApi.DEVMODE NewDevMode()
	{
		WinApi.DEVMODE dm = new WinApi.DEVMODE();
		dm.dmDeviceName = new String(new char[31]);
		dm.dmFormName = new String(new char[31]);
		dm.dmSize = (ushort)Marshal.SizeOf(dm);
		return dm;
	}
}