[DllImport("coredll.dll")]
static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
short dmColor;
short dmDuplex;
short dmYResolution;
short dmTTOption;
short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmDisplayOrientation;
}
Declare Function ChangeDisplaySettingsEx Lib "coredll.dll" (ByVal lpszDeviceName As String, ByVal lpDevMode As Byte(), ByVal hwnd As IntPtr, ByVal dwflags As CDSFlags, ByVal lParam As IntPtr) As CDSRet
Public Enum DMD
DMDO_0 = 0
DMDO_90 = 1
DMDO_180 = 2
DMDO_270 = 4
End Enum
Public Enum DM_Fields
DM_DISPLAYORIENTATION = 8388608
DM_DISPLAYQUERYORIENTATION = 16777216
End Enum
Public Enum DM_Orient
DMORIENT_PORTRAIT = 1
DMORIENT_LANDSCAPE = 2
End Enum
' Flags for ChangeDisplaySettings
Enum CDSFlags
CDS_VIDEOPARAMETERS = &H20
CDS_RESET = &H40000000
End Enum
' Return values for ChangeDisplaySettings
Enum CDSRet
DISP_CHANGE_SUCCESSFUL = 0
DISP_CHANGE_RESTART = 1
DISP_CHANGE_FAILED = -1
DISP_CHANGE_BADMODE = -2
DISP_CHANGE_NOTUPDATED = -3
DISP_CHANGE_BADFLAGS = -4
DISP_CHANGE_BADPARAM = -5
End Enum
Do you know one? Please contribute it!
Additional information is needed.
As for example how to rotate screen and stay in fullscreen mode.
Additional notes please add some!
Please add some!
C# Sample code for querying supported rotation modes, getting current mode & setting mode:
public partial class ScreenRotation
{
[DllImport("coredll.dll")]
static extern int ChangeDisplaySettingsEx(string lpszDeviceName,
ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
short dmColor;
short dmDuplex;
short dmYResolution;
short dmTTOption;
short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmDisplayOrientation;
}
private void GoodStuff(object sender, EventArgs e)
{
// modes are as follows: 0 = 0, 1 = 90, 2 = 180, 4 = 270 degrees
// supported modes query returns combination of these
// e.g. 0 = no rotation supported, 7 = all rotations supported
DEVMODE devmode = new DEVMODE();
devmode.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
DevM2.dmFields = 0x01000000; // query supported rotation modes
ChangeDisplaySettingsEx(null, ref DevM2, IntPtr.Zero, 2, IntPtr.Zero); // 2 = test
MessageBox.Show("Supported: " + DevM2.dmDisplayOrientation.ToString());
devmode.dmFields = 0x00800000; // query/set actual rotation modes
ChangeDisplaySettingsEx(null, ref devmode, IntPtr.Zero, 2, IntPtr.Zero); // 2 = test
MessageBox.Show("Current: " + DevM2.dmDisplayOrientation.ToString());
devmode.dmFields = 0x00800000; // query/set actual rotation modes
devmode.dmDisplayOrientation = 4; // switch to landscape
ChangeDisplaySettingsEx(null, ref devmode, IntPtr.Zero, 0, IntPtr.Zero); // 0 = set
}
}
VB Sample Code:
Public Class SelfMarshalledStruct
Public Sub New(ByVal size As Integer)
m_data = New Byte(size - 1) {}
End Sub
Public Function GetChar(ByVal offset As Integer) As Char
Return BitConverter.ToChar(m_data, offset)
End Function
Public Sub SetChar(ByVal offset As Integer, ByVal val As Char)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetInt32(ByVal offset As Integer) As Int32
Return BitConverter.ToInt32(m_data, offset)
End Function
Public Sub SetInt32(ByVal offset As Integer, ByVal val As Int32)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4)
End Sub
Public Function GetUInt32(ByVal offset As Integer) As UInt32
Return BitConverter.ToUInt32(m_data, offset)
End Function
Public Sub SetUInt32(ByVal offset As Integer, ByVal val As UInt32)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4)
End Sub
Public Function GetInt16(ByVal offset As Integer) As Int16
Return BitConverter.ToInt16(m_data, offset)
End Function
Public Sub SetInt16(ByVal offset As Integer, ByVal val As Int16)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetUInt16(ByVal offset As Integer) As UInt16
Return BitConverter.ToUInt16(m_data, offset)
End Function
Public Sub SetUInt16(ByVal offset As Integer, ByVal val As UInt16)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetStringUni(ByVal offset As Integer, ByVal len As Integer) As String
Return Encoding.Unicode.GetString(m_data, offset, len).TrimEnd(Chr(0))
End Function
Public Sub SetStringUni(ByVal str As String, ByVal offset As Integer, ByVal len As Integer)
Encoding.Unicode.GetBytes(str, 0, Math.Min(str.Length, len), m_data, offset)
End Sub
Default Public Property Item(ByVal offset As Integer) As Byte
Get
Return m_data(offset)
End Get
Set(ByVal value As Byte)
m_data(offset) = value
End Set
End Property
Public Function [Get](ByVal t As Type, ByVal offset As Integer) As Object
If t.IsPrimitive Then
If t.BaseType.ToString = "Int32" Then
Return GetInt32(offset)
ElseIf t.BaseType.ToString = "Int16" Then
Return GetInt16(offset)
ElseIf t.BaseType.ToString = "UInt32" Then
Return GetUInt32(offset)
ElseIf t.BaseType.ToString = "UInt16" Then
Return GetUInt16(offset)
ElseIf t.BaseType.ToString = "Byte" Then
Return Me(offset)
End If
End If
Return Nothing
End Function
Public Sub [Set](ByVal t As Type, ByVal offset As Integer, ByVal Val As Object)
If t.IsPrimitive Then
If t.BaseType.ToString = "Int32" Then
SetInt32(offset, CInt(Val))
ElseIf t.BaseType.ToString = "Int16" Then
SetInt16(offset, CShort(Val))
ElseIf t.BaseType.ToString = "UInt32" Then
SetUInt32(offset, DirectCast(Val, UInt32))
ElseIf t.BaseType.ToString = "UInt16" Then
SetUInt16(offset, CUShort(Val))
ElseIf t.BaseType.ToString = "Byte" Then
Me(offset) = CByte(Val)
End If
ElseIf t.ToString = "String" Then
SetStringUni(DirectCast(Val, String), offset, DirectCast(Val, String).Length)
End If
End Sub
Protected m_data As Byte()
Public ReadOnly Property Data() As Byte()
Get
Return m_data
End Get
End Property
End Class
Public Class devicemodeW
Inherits SelfMarshalledStruct
Private _DM_Orient As DM_Orient
Private _DM_Fields As DM_Fields
Private _DMD As DMD
Public Sub New()
MyBase.New(192)
dmSize = CUShort(Data.Length)
End Sub
Public Property dmDeviceName() As String
Get
Return GetStringUni(0, 64)
End Get
Set(ByVal value As String)
SetStringUni(value, 0, 64)
End Set
End Property
Public Property dmSpecVersion() As UShort
Get
Return GetUInt16(64)
End Get
Set(ByVal value As UShort)
SetUInt16(64, value)
End Set
End Property
Public Property dmDriverVersion() As UShort
Get
Return GetUInt16(66)
End Get
Set(ByVal value As UShort)
SetUInt16(66, value)
End Set
End Property
Public Property dmSize() As UShort
Get
Return GetUInt16(68)
End Get
Set(ByVal value As UShort)
SetUInt16(68, value)
End Set
End Property
Public Property dmDriverExtra() As UShort
Get
Return GetUInt16(70)
End Get
Set(ByVal value As UShort)
SetUInt16(70, value)
End Set
End Property
Public Property dmFields() As DM_Fields
Get
'Return DirectCast(GetUInt32(72), DM_Fields)
Return _DM_Fields
End Get
Set(ByVal value As DM_Fields)
_DM_Fields = value
SetUInt32(72, CInt(value))
End Set
End Property
Public Property dmOrientation() As DM_Orient
Get
'Return DirectCast(GetInt16(76), DM_Orient)
Return _DM_Orient
End Get
Set(ByVal value As DM_Orient)
SetInt16(76, CShort(value))
End Set
End Property
Public Property dmPaperSize() As Short
Get
Return GetInt16(78)
End Get
Set(ByVal value As Short)
SetInt16(78, value)
End Set
End Property
Public Property dmPaperLength() As Short
Get
Return GetInt16(80)
End Get
Set(ByVal value As Short)
SetInt16(80, value)
End Set
End Property
Public Property dmPaperWidth() As Short
Get
Return GetInt16(82)
End Get
Set(ByVal value As Short)
SetInt16(82, value)
End Set
End Property
Public Property dmScale() As Short
Get
Return GetInt16(84)
End Get
Set(ByVal value As Short)
SetInt16(84, value)
End Set
End Property
Public Property dmCopies() As Short
Get
Return GetInt16(86)
End Get
Set(ByVal value As Short)
SetInt16(86, value)
End Set
End Property
Public Property dmDefaultSource() As Short
Get
Return GetInt16(88)
End Get
Set(ByVal value As Short)
SetInt16(88, value)
End Set
End Property
Public Property dmPrintQuality() As Short
Get
Return GetInt16(90)
End Get
Set(ByVal value As Short)
SetInt16(90, value)
End Set
End Property
Public Property dmColor() As Short
Get
Return GetInt16(92)
End Get
Set(ByVal value As Short)
SetInt16(92, value)
End Set
End Property
Public Property dmDuplex() As Short
Get
Return GetInt16(94)
End Get
Set(ByVal value As Short)
SetInt16(94, value)
End Set
End Property
Public Property dmYResolution() As Short
Get
Return GetInt16(96)
End Get
Set(ByVal value As Short)
SetInt16(96, value)
End Set
End Property
Public Property dmTTOption() As Short
Get
Return GetInt16(98)
End Get
Set(ByVal value As Short)
SetInt16(98, value)
End Set
End Property
Public Property dmCollate() As Short
Get
Return GetInt16(100)
End Get
Set(ByVal value As Short)
SetInt16(100, value)
End Set
End Property
Public Property dmFormName() As String
Get
Return GetStringUni(102, 64)
End Get
Set(ByVal value As String)
SetStringUni(value, 102, 64)
End Set
End Property
Public Property dmLogPixels() As UShort
Get
Return GetUInt16(166)
End Get
Set(ByVal value As UShort)
SetUInt16(166, value)
End Set
End Property
Public Property dmBitsPerPel() As UInteger
Get
Return GetUInt32(168)
End Get
Set(ByVal value As UInteger)
SetUInt32(168, value)
End Set
End Property
Public Property dmPelsWidth() As UInteger
Get
Return GetUInt32(172)
End Get
Set(ByVal value As UInteger)
SetUInt32(172, value)
End Set
End Property
Public Property dmPelsHeight() As UInteger
Get
Return GetUInt32(176)
End Get
Set(ByVal value As UInteger)
SetUInt32(176, value)
End Set
End Property
Public Property dmDisplayFlags() As UInteger
Get
Return GetUInt32(180)
End Get
Set(ByVal value As UInteger)
SetUInt32(180, value)
End Set
End Property
Public Property dmDisplayFrequency() As UInteger
Get
Return GetUInt32(184)
End Get
Set(ByVal value As UInteger)
SetUInt32(184, value)
End Set
End Property
Public Property dmDisplayOrientation() As DMD
Get
'Return DirectCast(GetUInt32(188), DMD)
Return _DMD
End Get
Set(ByVal value As DMD)
_DMD = value
SetUInt32(188, CInt(value))
End Set
End Property
End Class
Public Class ScreenMode
'DEVICEMODE
<DllImport("coredll.dll")> _
Friend Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, ByVal lpDevMode As Byte(), ByVal hwnd As IntPtr, ByVal dwflags As CDSFlags, ByVal lParam As IntPtr) As CDSRet
End Function
Public Function ChangeMode(ByVal degree As Integer) As String
Dim devMode As New devicemodeW()
devMode.dmFields = DM_Fields.DM_DISPLAYORIENTATION
If degree = 0 Then
devMode.dmDisplayOrientation = DMD.DMDO_0
ElseIf degree = 90 Then
devMode.dmDisplayOrientation = DMD.DMDO_90
ElseIf degree = 180 Then
devMode.dmDisplayOrientation = DMD.DMDO_180
ElseIf degree = 270 Then
devMode.dmDisplayOrientation = DMD.DMDO_270
End If
Dim ret As CDSRet = ChangeDisplaySettingsEx(Nothing, devMode.Data, IntPtr.Zero, 0, IntPtr.Zero)
Return (ret.ToString())
End Function
Public Sub SetLandscape()
Try
ChangeMode(90)
Catch ex As Exception
'MsgBox(ex.ToString)
End Try
End Sub
Public Sub SetPortrait()
Try
ChangeMode(0)
Catch ex As Exception
'MsgBox(ex.ToString)
End Try
End Sub
End Class