Get Default Display Settings stored in registry

Dave Cotton 41 Reputation points
2025-02-04T19:05:09.29+00:00

Documentation for ChangeDisplaySettings says passing null DevMode and 0 flag will revert the monitor to the default setting stored in the registry settings. How to check what the current default setting stored in the registry is before the call?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,086 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,268 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,861 Reputation points Microsoft Vendor
    2025-02-05T02:34:47.96+00:00

    Hi @Dave Cotton , Welcome to Microsoft Q&A,

    The EnumDisplaySettings function gets the settings of the current display device by passing lpszDeviceName as null, and iModeNum uses ENUM_REGISTRY_SETTINGS (that is, -2) to specify getting the default settings in the registry.

    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        // Define the DEVMODE structure
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct DEVMODE
        {
            private const int CCHDEVICENAME = 32;
            private const int CCHFORMNAME = 32;
            
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
            public string dmDeviceName;
            public short  dmSpecVersion;
            public short  dmDriverVersion;
            public short  dmSize;
            public short  dmDriverExtra;
            public int    dmFields;
    
            public int    dmPositionX;
            public int    dmPositionY;
            public int    dmDisplayOrientation;
            public int    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 short  dmLogPixels;
            public int    dmBitsPerPel;
            public int    dmPelsWidth;
            public int    dmPelsHeight;
            public int    dmDisplayFlags;
            public int    dmDisplayFrequency;
    
            public int    dmICMMethod;
            public int    dmICMIntent;
            public int    dmMediaType;
            public int    dmDitherType;
            public int    dmReserved1;
            public int    dmReserved2;
            public int    dmPanningWidth;
            public int    dmPanningHeight;
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern bool EnumDisplaySettings(
            string lpszDeviceName,
            int iModeNum,
            ref DEVMODE lpDevMode);
    
        // Define enumeration values ​​to get the default settings in the registry
        const int ENUM_REGISTRY_SETTINGS = -2;
    
        static void Main()
        {
            // Initialize the DEVMODE structure and set the dmSize field
            DEVMODE dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
    
            // Get the default display settings (settings in the registry)
            bool success = EnumDisplaySettings(null, ENUM_REGISTRY_SETTINGS, ref dm);
            if (success)
            {
                Console.WriteLine("Default display settings in the registry: ");
                Console.WriteLine("Resolution: {0} x {1}", dm.dmPelsWidth, dm.dmPelsHeight);
                Console.WriteLine("Color bit depth: {0} bits", dm.dmBitsPerPel);
                Console.WriteLine("Refresh rate: {0} Hz", dm.dmDisplayFrequency);
                // Other field information can be output as needed
            }
            else
            {
                Console.WriteLine("The call to EnumDisplaySettings failed.");
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.