Created
May 23, 2011 12:44
-
-
Save umq/986635 to your computer and use it in GitHub Desktop.
Rotate secondary display with ChangeDisplaySettingsEx()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace RotateDisplayTest | |
{ | |
class RotateDisplay | |
{ | |
static void Main(string[] args) | |
{ | |
uint deviceID = 1; // zero origin (i.e. 1 means DISPLAY2) | |
DISPLAY_DEVICE d = new DISPLAY_DEVICE(); | |
DEVMODE dm = new DEVMODE(); | |
d.cb = Marshal.SizeOf(d); | |
NativeMethods.EnumDisplayDevices(null, deviceID, ref d, 0); | |
if (0 != NativeMethods.EnumDisplaySettings( | |
d.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) | |
{ | |
int temp = dm.dmPelsHeight; | |
dm.dmPelsHeight = dm.dmPelsWidth; | |
dm.dmPelsWidth = temp; | |
switch (dm.dmDisplayOrientation) | |
{ | |
case NativeMethods.DMDO_DEFAULT: | |
dm.dmDisplayOrientation = NativeMethods.DMDO_270; | |
break; | |
case NativeMethods.DMDO_270: | |
dm.dmDisplayOrientation = NativeMethods.DMDO_180; | |
break; | |
case NativeMethods.DMDO_180: | |
dm.dmDisplayOrientation = NativeMethods.DMDO_90; | |
break; | |
case NativeMethods.DMDO_90: | |
dm.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT; | |
break; | |
default: | |
break; | |
} | |
DISP_CHANGE iRet = NativeMethods.ChangeDisplaySettingsEx( | |
d.DeviceName, ref dm, IntPtr.Zero, | |
DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); | |
// if (iRet != DISP_CHANGE.Successful) handle error | |
} | |
} | |
} | |
internal class NativeMethods | |
{ | |
[DllImport("user32.dll")] | |
internal static extern DISP_CHANGE ChangeDisplaySettingsEx( | |
string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, | |
DisplaySettingsFlags dwflags, IntPtr lParam); | |
[DllImport("user32.dll")] | |
internal static extern bool EnumDisplayDevices( | |
string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, | |
uint dwFlags); | |
[DllImport("user32.dll", CharSet = CharSet.Ansi)] | |
internal static extern int EnumDisplaySettings( | |
string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); | |
public const int DMDO_DEFAULT = 0; | |
public const int DMDO_90 = 1; | |
public const int DMDO_180 = 2; | |
public const int DMDO_270 = 3; | |
public const int ENUM_CURRENT_SETTINGS = -1; | |
} | |
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)] | |
internal struct DEVMODE | |
{ | |
public const int CCHDEVICENAME = 32; | |
public const int CCHFORMNAME = 32; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)] | |
[System.Runtime.InteropServices.FieldOffset(0)] | |
public string dmDeviceName; | |
[System.Runtime.InteropServices.FieldOffset(32)] | |
public Int16 dmSpecVersion; | |
[System.Runtime.InteropServices.FieldOffset(34)] | |
public Int16 dmDriverVersion; | |
[System.Runtime.InteropServices.FieldOffset(36)] | |
public Int16 dmSize; | |
[System.Runtime.InteropServices.FieldOffset(38)] | |
public Int16 dmDriverExtra; | |
[System.Runtime.InteropServices.FieldOffset(40)] | |
public DM dmFields; | |
[System.Runtime.InteropServices.FieldOffset(44)] | |
Int16 dmOrientation; | |
[System.Runtime.InteropServices.FieldOffset(46)] | |
Int16 dmPaperSize; | |
[System.Runtime.InteropServices.FieldOffset(48)] | |
Int16 dmPaperLength; | |
[System.Runtime.InteropServices.FieldOffset(50)] | |
Int16 dmPaperWidth; | |
[System.Runtime.InteropServices.FieldOffset(52)] | |
Int16 dmScale; | |
[System.Runtime.InteropServices.FieldOffset(54)] | |
Int16 dmCopies; | |
[System.Runtime.InteropServices.FieldOffset(56)] | |
Int16 dmDefaultSource; | |
[System.Runtime.InteropServices.FieldOffset(58)] | |
Int16 dmPrintQuality; | |
[System.Runtime.InteropServices.FieldOffset(44)] | |
public POINTL dmPosition; | |
[System.Runtime.InteropServices.FieldOffset(52)] | |
public Int32 dmDisplayOrientation; | |
[System.Runtime.InteropServices.FieldOffset(56)] | |
public Int32 dmDisplayFixedOutput; | |
[System.Runtime.InteropServices.FieldOffset(60)] | |
public short dmColor; | |
[System.Runtime.InteropServices.FieldOffset(62)] | |
public short dmDuplex; | |
[System.Runtime.InteropServices.FieldOffset(64)] | |
public short dmYResolution; | |
[System.Runtime.InteropServices.FieldOffset(66)] | |
public short dmTTOption; | |
[System.Runtime.InteropServices.FieldOffset(68)] | |
public short dmCollate; | |
[System.Runtime.InteropServices.FieldOffset(72)] | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)] | |
public string dmFormName; | |
[System.Runtime.InteropServices.FieldOffset(102)] | |
public Int16 dmLogPixels; | |
[System.Runtime.InteropServices.FieldOffset(104)] | |
public Int32 dmBitsPerPel; | |
[System.Runtime.InteropServices.FieldOffset(108)] | |
public Int32 dmPelsWidth; | |
[System.Runtime.InteropServices.FieldOffset(112)] | |
public Int32 dmPelsHeight; | |
[System.Runtime.InteropServices.FieldOffset(116)] | |
public Int32 dmDisplayFlags; | |
[System.Runtime.InteropServices.FieldOffset(116)] | |
public Int32 dmNup; | |
[System.Runtime.InteropServices.FieldOffset(120)] | |
public Int32 dmDisplayFrequency; | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | |
internal 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; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
struct POINTL | |
{ | |
long x; | |
long y; | |
} | |
enum DISP_CHANGE : int | |
{ | |
Successful = 0, | |
Restart = 1, | |
Failed = -1, | |
BadMode = -2, | |
NotUpdated = -3, | |
BadFlags = -4, | |
BadParam = -5, | |
BadDualView = -6 | |
} | |
[Flags()] | |
enum DisplayDeviceStateFlags : int | |
{ | |
AttachedToDesktop = 0x1, | |
MultiDriver = 0x2, | |
PrimaryDevice = 0x4, | |
MirroringDriver = 0x8, | |
VGACompatible = 0x16, | |
Removable = 0x20, | |
ModesPruned = 0x8000000, | |
Remote = 0x4000000, | |
Disconnect = 0x2000000 | |
} | |
[Flags()] | |
enum DisplaySettingsFlags : int | |
{ | |
CDS_UPDATEREGISTRY = 1, | |
CDS_TEST = 2, | |
CDS_FULLSCREEN = 4, | |
CDS_GLOBAL = 8, | |
CDS_SET_PRIMARY = 0x10, | |
CDS_RESET = 0x40000000, | |
CDS_NORESET = 0x10000000 | |
} | |
[Flags()] | |
enum DM : int | |
{ | |
Orientation = 0x1, | |
PaperSize = 0x2, | |
PaperLength = 0x4, | |
PaperWidth = 0x8, | |
Scale = 0x10, | |
Position = 0x20, | |
NUP = 0x40, | |
DisplayOrientation = 0x80, | |
Copies = 0x100, | |
DefaultSource = 0x200, | |
PrintQuality = 0x400, | |
Color = 0x800, | |
Duplex = 0x1000, | |
YResolution = 0x2000, | |
TTOption = 0x4000, | |
Collate = 0x8000, | |
FormName = 0x10000, | |
LogPixels = 0x20000, | |
BitsPerPixel = 0x40000, | |
PelsWidth = 0x80000, | |
PelsHeight = 0x100000, | |
DisplayFlags = 0x200000, | |
DisplayFrequency = 0x400000, | |
ICMMethod = 0x800000, | |
ICMIntent = 0x1000000, | |
MediaType = 0x2000000, | |
DitherType = 0x4000000, | |
PanningWidth = 0x8000000, | |
PanningHeight = 0x10000000, | |
DisplayFixedOutput = 0x20000000 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment