获取屏幕的实际尺寸

Hic*_*ham 3 c# windows

是否有可能获得以厘米为单位而不是以像素为单位的实际屏幕尺寸?即我需要知道屏幕的大小而不是它的分辨率。

如果在Windows应用程序中有可能,我需要这个。

Xar*_*uth 5

有关屏幕的所有信息(来自制造商)在注册表中HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY。屏幕的大小是经过编码的,很难找到,但是有可能。

有关更多信息,请在网上搜索:EDID(“扩展的显示标识数据”)(http://en.wikipedia.org/wiki/Extended_display_identification_data,其大小的字节为#21和#22)

在这里,我使用的代码具有大小(以及更多信息,但我清除了代码的大小):

// Open the Display Reg-Key
RegistryKey displayRegistry = Registry.LocalMachine;
Boolean isFailed = false;
try
{
    displayRegistry = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\DISPLAY");
}
catch
{
    isFailed = true;
}

if (!isFailed & (displayRegistry != null))
{
    // Get all MonitorIDss
    foreach (String monitorID in displayRegistry.GetSubKeyNames())
    {
        if (monitorID == name)
        {
            RegistryKey monitorIDRegistry = displayRegistry.OpenSubKey(monitorID);

            if (monitorIDRegistry != null)
            {
                // Get all Plug&Play ID's
                foreach (String subname in monitorIDRegistry.GetSubKeyNames())
                {
                    RegistryKey pnpID = monitorIDRegistry.OpenSubKey(subname);
                    if (pnpID != null)
                    {
                        String[] subkeys = pnpID.GetSubKeyNames();

                        // Check if Monitor is active
                        if (subkeys.Contains("Control"))
                        {
                            if (subkeys.Contains("Device Parameters"))
                            {
                                RegistryKey devParam = pnpID.OpenSubKey("Device Parameters");

                                Int16 sizeH = 0;
                                Int16 sizeV = 0;

                                // Get the EDID code
                                byte[] edidObj = devParam.GetValue("EDID", null) as byte[];
                                if (edidObj != null)
                                {
                                    sizeH = Convert.ToInt16(Encoding.Default.GetString(edidObj, 0x15, 1)[0]);
                                    sizeV = Convert.ToInt16(Encoding.Default.GetString(edidObj, 0x16, 1)[0]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

尺寸以厘米为单位(仅整数)。

您可以使用此物理比例和对角线:

private static String GetRatio(Double MaxSizeH, Double MaxSizeV)
{
    if (MaxSizeV == 0)
    {
        return "undefined";
    }

    Double ratio = MaxSizeH / MaxSizeV;

    String strRatio = "4/3";
    Double ecartRatio = Math.Abs(ratio - (4 / (Double)3));

    if (Math.Abs(ratio - (16 / (Double)10)) < ecartRatio)
    {
        ecartRatio = Math.Abs(ratio - (16 / (Double)10));
        strRatio = "16/10";
    }

    if (Math.Abs(ratio - (16 / (Double)9)) < ecartRatio)
    {
        ecartRatio = Math.Abs(ratio - (16 / (Double)9));
        strRatio = "16/9";
    }

    return strRatio;
}

// diagonal in inch
private static Double GetDiagonale(Double MaxSizeH, Double MaxSizeV)
{
    return 0.3937 * Math.Sqrt(MaxSizeH * MaxSizeH + MaxSizeV * MaxSizeV);
}
Run Code Online (Sandbox Code Playgroud)

编辑:如何具有监视器名称(所有连接的监视器):

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DISPLAY_DEVICE
{
    [MarshalAs(UnmanagedType.U4)]
    public int cb;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string DeviceName;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string DeviceString;

    [MarshalAs(UnmanagedType.U4)]
    public DisplayDeviceStateFlags StateFlags;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string DeviceID;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string DeviceKey;
}

[Flags]
public enum DisplayDeviceStateFlags : int
{
    /// <summary>The device is part of the desktop.</summary>
    AttachedToDesktop = 0x1,

    MultiDriver = 0x2,

    /// <summary>The device is part of the desktop.</summary>
    PrimaryDevice = 0x4,

    /// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
    MirroringDriver = 0x8,

    /// <summary>The device is VGA compatible.</summary>
    VGACompatible = 0x10,

    /// <summary>The device is removable; it cannot be the primary display.</summary>
    Removable = 0x20,

    /// <summary>The device has more display modes than its output devices support.</summary>
    ModesPruned = 0x8000000,

    Remote = 0x4000000,
    Disconnect = 0x2000000
}

[DllImport("User32.dll")]
public static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);


private static List<NativeMethods.DISPLAY_DEVICE> GetAllDevice()
{
    List<NativeMethods.DISPLAY_DEVICE> devices = new List<NativeMethods.DISPLAY_DEVICE>();
    bool error = false;
    for (int devId = 0; !error; devId++)
    {
        try
        {
            NativeMethods.DISPLAY_DEVICE device = new NativeMethods.DISPLAY_DEVICE();
            device.cb = Marshal.SizeOf(typeof(NativeMethods.DISPLAY_DEVICE));
            error = NativeMethods.EnumDisplayDevices(null, devId, ref device, 0) == 0;
            if (String.IsNullOrWhiteSpace(device.DeviceID) == false)
            {
                devices.Add(device);
            }
        }
        catch (Exception)
        {
            error = true;
        }
    }

    return devices;
}
Run Code Online (Sandbox Code Playgroud)

并完成:

List<NativeMethods.DISPLAY_DEVICE> devices = GetAllDevice();

foreach (NativeMethods.DISPLAY_DEVICE device in devices)
{
    NativeMethods.DISPLAY_DEVICE monitor = new NativeMethods.DISPLAY_DEVICE();
    monitor.cb = Marshal.SizeOf(typeof(NativeMethods.DISPLAY_DEVICE));
    NativeMethods.EnumDisplayDevices(device.DeviceName, 0, ref monitor, 0);
    String monitorname = monitor.DeviceID.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault();

    GetMonitorDetail(monitorname);
}
Run Code Online (Sandbox Code Playgroud)