Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

ChangeDisplaySettingsEx (coredll)
 

coredll is for smart devices, not desktop Windows. Therefore, this information only applies to code using the .NET Compact Framework. To see if information for ChangeDisplaySettingsEx in other DLLs exists, click on Find References to the right.

.
Summary
Get/change the display rotation (landscape/portrait)

C# Signature:

[DllImport("coredll.dll")]
static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);

User-Defined Types:

[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;
     public short dmOrientation;
     public short dmPaperSize;
     public short dmPaperLength;
     public short dmPaperWidth;
     public short dmScale;
     public short dmCopies;
     public short dmDefaultSource;
     public short dmPrintQuality;
     public short dmColor;
     public short dmDuplex;
     public short dmYResolution;
     public short dmTTOption;
     public short dmCollate;
     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;
}

VB Signature:

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

User-Defined Types:

    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

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Additional information is needed.

As for example how to rotate screen and stay in fullscreen mode.

Additional notes please add some!

Tips & Tricks:

Please add some!

Sample Code:

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));

        DEVMODE DevM2 = 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: " + devmode.dmDisplayOrientation.ToString());

        devmode.dmFields = 0x00800000; // query/set actual rotation modes
        devmode.dmDisplayOrientation = (devmode.dmDisplayOrientation == 0) ? 1 : 0; //switch between 0, 90 rotation
        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

Documentation

CompactFramework 2 has a setting for ScreenOrientation http://msdn.microsoft.com/en-us/library/microsoft.windowsce.forms.systemsettings.screenorientation%28v=vs.90%29.aspx

    using System;

    using System.Collections.Generic;
    using System.Text;
    using Microsoft.WindowsCE.Forms;

    namespace rotateW
    {
        public static class ScreenOrientationCF
        {
            public static bool SetScreenOrientation(Microsoft.WindowsCE.Forms.ScreenOrientation so)
            {
                try
                {
                    SystemSettings.ScreenOrientation = so;
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            public static Microsoft.WindowsCE.Forms.ScreenOrientation GetScreenOrientation()
            {
                return SystemSettings.ScreenOrientation;
            }
        }
    }

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions