using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ConsoleApplication1
{
partial class Program
{
public const int ENUM_CURRENT_SETTINGS = -1;
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
public DISPLAY_DEVICE(int flags)
{
cb = 0;
StateFlags = flags;
DeviceName = new string((char)32, 32);
DeviceString = new string((char)32, 128);
DeviceID = new string((char)32, 128);
DeviceKey = new string((char)32, 128);
cb = Marshal.SizeOf(this);
}
}
[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;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
static void EnumMonitors()
{
foreach (Screen screen in Screen.AllScreens)
{
Console.WriteLine("*** Managed output");
Console.WriteLine("Display Device: {0}\t\n" +
"\tBPP={1}\n\tWidth={2}\n\tHeight={3}\n\n",
screen.DeviceName, screen.BitsPerPixel,
screen.Bounds.Width, screen.Bounds.Height);
DISPLAY_DEVICE monitor =
new DISPLAY_DEVICE(0);
EnumDisplayDevices(screen.DeviceName, 0,
ref monitor, 0);
DEVMODE dm = new DEVMODE();
EnumDisplaySettings(screen.DeviceName,
ENUM_CURRENT_SETTINGS, ref dm);
Console.WriteLine("*** Unmanaged output");
Console.WriteLine("\tMonitor: {0}\n\t\t{1}\n\t\t{2}",
monitor.DeviceName,
monitor.DeviceString, monitor.DeviceID);
Console.WriteLine(
"\t\t\tFreq.={0}\n" +
"\t\t\tBPP={1}\n" +
"\t\t\tWidth={2}\n" +
"\t\t\tHeight={3}\n",
dm.dmDisplayFrequency, dm.dmBitsPerPel,
dm.dmPelsWidth, dm.dmPelsHeight);
}
Console.WriteLine("Number of monitors: {0}",
Screen.AllScreens.Length);
}
[DllImport("User32.dll")]
private static extern bool EnumDisplayDevices(
string lpDevice, int iDevNum,
ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
[DllImport("User32.dll")]
private static extern bool EnumDisplaySettings(
string devName, int modeNum, ref DEVMODE devMode);
}
}
|