Virtual Developer Workshop: Containerized Development with Docker


I have always had a fascination with screen display settings. Well, I like to play with a computer's display and all the options in code. It is wonderful what .NET and Windows enable you to do. If you revisit some of my older articles, you would see articles that includes the taskbar settings, desktop icons, screen savers, and display settings. Today, you will learn how to create a .NET application that enables you to rotate any one of your display screens and to reset them back.


You will create a .NET Windows Forms application in C# or in Visual Basic with four buttons. Three buttons will be used for the three different displays. The fourth button will be used to Reset the displays back to their original values. Let us jump straight to the code!

Add a class to your project, and name it APIStuff. This class will be responsible for all the Windows APIs aiding you in setting display settings. Because you will be dealing with the system API, you need to import the correct Namespaces. Do this now.


using System;
using System.Runtime.InteropServices;


Imports System.Runtime.InteropServices

Now, add the following API methods.


   internal static extern DISP_CHANGE ChangeDisplaySettingsEx
      (string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd,
      DisplaySettingsFlags dwflags, IntPtr lParam);

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


   Friend Shared Function ChangeDisplaySettingsEx(ByVal _
      lpszDeviceName As String, ByRef lpDevMode As DEVMODE, _
      ByVal hwnd As IntPtr, ByVal dwflags As DisplaySettingsFlags, _
      ByVal lParam As IntPtr) As DISP_CHANGE

   End Function
   Friend Shared Function EnumDisplayDevices(ByVal lpDevice As _
      String, ByVal iDevNum As UInteger, ByRef lpDisplayDevice _
      As DISPLAY_DEVICE, ByVal dwFlags As UInteger) As Boolean

   End Function
   <DllImport("user32.dll", CharSet:=CharSet.Ansi)>
   Friend Shared Function EnumDisplaySettings(ByVal lpszDeviceName _
      As String, ByVal iModeNum As Integer, ByRef lpDevMode _
      As DEVMODE) As Integer

   End Function

ChangeDisplaySettingsEx changes the settings of the specified display to the specified graphics mode. EnumDisplayDevices obtains information about the display devices. EnumDisplaySettings retrieves information of the graphics modes of a display device.

Add the Constants and Structures that work with the preceding API functions.


   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 =
      public string dmDeviceName;
      public Int16 dmSpecVersion;
      public Int16 dmDriverVersion;
      public Int16 dmSize;
      public Int16 dmDriverExtra;
      public DM dmFields;

      Int16 dmOrientation;
      Int16 dmPaperSize;
      Int16 dmPaperLength;
      Int16 dmPaperWidth;
      Int16 dmScale;
      Int16 dmCopies;
      Int16 dmDefaultSource;
      Int16 dmPrintQuality;

      public POINTL dmPosition;
      public Int32 dmDisplayOrientation;
      public Int32 dmDisplayFixedOutput;

      public short dmColor;
      public short dmDuplex;
      public short dmYResolution;
      public short dmTTOption;
      public short dmCollate;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
      public string dmFormName;
      public Int16 dmLogPixels;
      public Int32 dmBitsPerPel;
      public Int32 dmPelsWidth;
      public Int32 dmPelsHeight;
      public Int32 dmDisplayFlags;
      public Int32 dmNup;
      public Int32 dmDisplayFrequency;

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

   internal struct POINTL
      long x;
      long y;


   Public Const DMDO_DEFAULT As Integer = 0
   Public Const DMDO_90 As Integer = 1
   Public Const DMDO_180 As Integer = 2
   Public Const DMDO_270 As Integer = 3
   Public Const ENUM_CURRENT_SETTINGS As Integer = -1

<StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Ansi)>
Friend Structure DEVMODE

   Public Const CCHDEVICENAME As Integer = 32
   Public Const CCHFORMNAME As Integer = 32
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHDEVICENAME)>
   Public dmDeviceName As String
   Public dmSpecVersion As Int16
   Public dmDriverVersion As Int16
   Public dmSize As Int16
   Public dmDriverExtra As Int16
   Public dmFields As DM
   Private dmOrientation As Int16
   Private dmPaperSize As Int16
   Private dmPaperLength As Int16
   Private dmPaperWidth As Int16
   Private dmScale As Int16
   Private dmCopies As Int16
   Private dmDefaultSource As Int16
   Private dmPrintQuality As Int16
   Public dmPosition As POINTL
   Public dmDisplayOrientation As Int32
   Public dmDisplayFixedOutput As Int32
   Public dmColor As Short
   Public dmDuplex As Short
   Public dmYResolution As Short
   Public dmTTOption As Short
   Public dmCollate As Short
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHFORMNAME)>
   Public dmFormName As String
   Public dmLogPixels As Int16
   Public dmBitsPerPel As Int32
   Public dmPelsWidth As Int32
   Public dmPelsHeight As Int32
   Public dmDisplayFlags As Int32
   Public dmNup As Int32
   Public dmDisplayFrequency As Int32

End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Friend Structure DISPLAY_DEVICE

   Public cb As Integer
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)>
   Public DeviceName As String
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)>
   Public DeviceString As String
   Public StateFlags As DisplayDeviceStateFlags
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)>
   Public DeviceID As String
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)>
   Public DeviceKey As String

End Structure

Friend Structure POINTL

   Private x As Long
   Private y As Long

End Structure

The DEVMODE structure contains information about the environment of a display device. DISPLAY_DEVICE receives information about the given display device. POINTL simply contains the coordinates of a point. Add the next few Enumerations.


   internal enum DISP_CHANGE : int
      Successful = 0,
      Restart = 1,
      Failed = -1,
      BadMode = -2,
      NotUpdated = -3,
      BadFlags = -4,
      BadParam = -5,
      BadDualView = -6

   internal enum DisplayDeviceStateFlags : int
      AttachedToDesktop = 0x1,
      MultiDriver = 0x2,
      PrimaryDevice = 0x4,
      MirroringDriver = 0x8,
      VGACompatible = 0x10,
      Removable = 0x20,
      ModesPruned = 0x8000000,
      Remote = 0x4000000,
      Disconnect = 0x2000000

   internal enum DisplaySettingsFlags : int
      CDS_NONE = 0,
      CDS_UPDATEREGISTRY = 0x00000001,
      CDS_TEST = 0x00000002,
      CDS_FULLSCREEN = 0x00000004,
      CDS_GLOBAL = 0x00000008,
      CDS_SET_PRIMARY = 0x00000010,
      CDS_VIDEOPARAMETERS = 0x00000020,
      CDS_ENABLE_UNSAFE_MODES = 0x00000100,
      CDS_DISABLE_UNSAFE_MODES = 0x00000200,
      CDS_RESET = 0x40000000,
      CDS_RESET_EX = 0x20000000,
      CDS_NORESET = 0x10000000

   internal enum DM : int
      Orientation = 0x00000001,
      PaperSize = 0x00000002,
      PaperLength = 0x00000004,
      PaperWidth = 0x00000008,
      Scale = 0x00000010,
      Position = 0x00000020,
      NUP = 0x00000040,
      DisplayOrientation = 0x00000080,
      Copies = 0x00000100,
      DefaultSource = 0x00000200,
      PrintQuality = 0x00000400,
      Color = 0x00000800,
      Duplex = 0x00001000,
      YResolution = 0x00002000,
      TTOption = 0x00004000,
      Collate = 0x00008000,
      FormName = 0x00010000,
      LogPixels = 0x00020000,
      BitsPerPixel = 0x00040000,
      PelsWidth = 0x00080000,
      PelsHeight = 0x00100000,
      DisplayFlags = 0x00200000,
      DisplayFrequency = 0x00400000,
      ICMMethod = 0x00800000,
      ICMIntent = 0x01000000,
      MediaType = 0x02000000,
      DitherType = 0x04000000,
      PanningWidth = 0x08000000,
      PanningHeight = 0x10000000,
      DisplayFixedOutput = 0x20000000



   Successful = 0
   Restart = 1
   Failed = -1
   BadMode = -2
   NotUpdated = -3
   BadFlags = -4
   BadParam = -5
   BadDualView = -6

End Enum

Friend Enum DisplayDeviceStateFlags

   AttachedToDesktop = &H1
   MultiDriver = &H2
   PrimaryDevice = &H4
   MirroringDriver = &H8
   VGACompatible = &H10
   Removable = &H20
   ModesPruned = &H8000000
   Remote = &H4000000
   Disconnect = &H2000000

End Enum

Friend Enum DisplaySettingsFlags

   CDS_NONE = 0
   CDS_TEST = &H2
   CDS_RESET = &H40000000
   CDS_RESET_EX = &H20000000
   CDS_NORESET = &H10000000

End Enum

Friend Enum DM

   Orientation = &H1
   PaperSize = &H2
   PaperLength = &H4
   PaperWidth = &H8
   Scale = &H10
   Position = &H20
   NUP = &H40
   DisplayOrientation = &H80
   Copies = &H100
   DefaultSource = &H200
   PrintQuality = &H400
   Color = &H800
   Duplex = &H1000
   YResolution = &H2000
   TTOption = &H4000
   Collate = &H8000
   FormName = &H10000
   LogPixels = &H20000
   BitsPerPixel = &H40000
   PelsWidth = &H80000
   PelsHeight = &H100000
   DisplayFlags = &H200000
   DisplayFrequency = &H400000
   ICMMethod = &H800000
   ICMIntent = &H1000000
   MediaType = &H2000000
   DitherType = &H4000000
   PanningWidth = &H8000000
   PanningHeight = &H10000000
   DisplayFixedOutput = &H20000000

End Enum

DISP_CHANGE supplies the methods with results after the Display has been changed. These include Successful or Failed. DisplayDeviceStateFlags indicates how the device is attached to the computer. For example: Am I connected to this device through a remote connection, or is the screen connected to the current PC requesting a change of screen settings? DisplaySettingsFlags enables you to specify how the computer must store the change made to the display device. For example: If you were to supply the UPDATE_REGISTRY value, the PC will note this change in the computer's Registry. The next time you start your PC, the changes will still be the same as you set them. Or temporary. DM specifies which screen setting you are changing. For example: Orientation.

Add a class to your project and name it clsDisplaySettings. This class will be responsible for all the Rotating and Resetting methods. Add the following Enumeration.


   public enum Orientation
      DEGREES_CW_270 = 1,
      DEGREES_CW_180 = 2,
      DEGREES_CW_90 = 3,
      DEGREES_CW_0 = 0


Public Class clsDisplaySettings
   Public Enum Orientation

      DEGREES_CW_270 = 1
      DEGREES_CW_180 = 2
      DEGREES_CW_90 = 3
      DEGREES_CW_0 = 0

   End Enum

This enumeration simply indicates how many degrees the screen most rotate. Add the Rotate method.


   public static bool Rotate(uint uDisplay,
      Orientation oOrientation)

      bool bResult = false;
      DEVMODE dmMode = new DEVMODE();

      dDevice.cb = Marshal.SizeOf(dDevice);

      if (!APIStuff.EnumDisplayDevices(null, uDisplay - 1,
            ref dDevice, 0))
         return false;

      if (0 != APIStuff.EnumDisplaySettings(
         dDevice.DeviceName, APIStuff.ENUM_CURRENT_SETTINGS,
            ref dmMode))

         if ((dmMode.dmDisplayOrientation + (int)oOrientation)
            % 2 == 1) 

            int tmp = dmMode.dmPelsHeight;

            dmMode.dmPelsHeight = dmMode.dmPelsWidth;
            dmMode.dmPelsWidth = tmp;


         switch (oOrientation)

            case Orientation.DEGREES_CW_90:

               dmMode.dmDisplayOrientation = APIStuff.DMDO_270;

            case Orientation.DEGREES_CW_180:

               dmMode.dmDisplayOrientation = APIStuff.DMDO_180;

            case Orientation.DEGREES_CW_270:

               dmMode.dmDisplayOrientation = APIStuff.DMDO_90;

            case Orientation.DEGREES_CW_0:

               dmMode.dmDisplayOrientation = APIStuff.DMDO_DEFAULT;



         DISP_CHANGE ret = APIStuff.ChangeDisplaySettingsEx
            (dDevice.DeviceName, ref dmMode, IntPtr.Zero,
            DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);

         bResult = ret == 0;


      return bResult;



   Public Shared Function Rotate(ByVal uDisplay As UInteger, _
         ByVal oOrientation As Orientation) As Boolean

      Dim bResult As Boolean = False
      Dim dmMode As DEVMODE = New DEVMODE()

      dDevice.cb = Marshal.SizeOf(dDevice)

      If Not APIStuff.EnumDisplayDevices(Nothing, uDisplay - 1, _
         dDevice, 0) Then Return False

      If 0 <> APIStuff.EnumDisplaySettings _
         (dDevice.DeviceName, APIStuff.ENUM_CURRENT_SETTINGS, _
         dmMode) Then

         If (dmMode.dmDisplayOrientation + CInt(oOrientation)) _
            Mod 2 = 1 Then

            Dim tmp As Integer = dmMode.dmPelsHeight
            dmMode.dmPelsHeight = dmMode.dmPelsWidth
            dmMode.dmPelsWidth = tmp

         End If

         Select Case oOrientation

            Case Orientation.DEGREES_CW_90
               dmMode.dmDisplayOrientation = APIStuff.DMDO_270

            Case Orientation.DEGREES_CW_180
               dmMode.dmDisplayOrientation = APIStuff.DMDO_180

            Case Orientation.DEGREES_CW_270
               dmMode.dmDisplayOrientation = APIStuff.DMDO_90

            Case Orientation.DEGREES_CW_0
               dmMode.dmDisplayOrientation = APIStuff.DMDO_DEFAULT

            Case Else

         End Select

         Dim ret As DISP_CHANGE = APIStuff.ChangeDisplaySettingsEx _
            (dDevice.DeviceName, dmMode, IntPtr.Zero, _
            DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero)
            bResult = ret = 0

      End If

      Return bResult

   End Function

The preceding code rotates your specified screen. Add the Reset method, which resets all screens back to their default values.


   public static void Reset()


         uint i = 0;

         while (++i <= 64)

            Rotate(i, Orientation.DEGREES_CW_0);



      catch (ArgumentOutOfRangeException ex)




   Public Shared Sub Reset()


         Dim i As Integer = 0

         While System.Threading.Interlocked.Increment(i) <= 64

            Rotate(i, Orientation.DEGREES_CW_0)

         End While

      Catch ex As ArgumentOutOfRangeException

      End Try

   End Sub

On your form, add the following code for your buttons.


   private void button1_Click(object sender, EventArgs e)



   private void button2_Click(object sender, EventArgs e)



   private void button3_Click(object sender, EventArgs e)



   private void button4_Click(object sender, EventArgs e)




   Private Sub button1_Click(ByVal sender As Object, ByVal e _
         As EventArgs) Handles button1.Click

      clsDisplaySettings.Rotate(1, clsDisplaySettings.Orientation _

   End Sub

   Private Sub button2_Click(ByVal sender As Object, ByVal e _
         As EventArgs) Handles button2.Click

      clsDisplaySettings.Rotate(2, clsDisplaySettings _

   End Sub

   Private Sub button3_Click(ByVal sender As Object, ByVal e _
         As EventArgs) Handles button3.Click

      clsDisplaySettings.Rotate(3, clsDisplaySettings.Orientation _

   End Sub

   Private Sub button4_Click(ByVal sender As Object, ByVal e _
         As EventArgs) Handles button4.Click


   End Sub


It is quite easy to "play" with your desktop resolution, as you saw. I use it to prank my co-workers; I hope you put this knowledge to better use than I.

About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date