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.