首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用WinAPI/C#获取监视器/显示设备名称

要使用WinAPI/C#获取监视器/显示设备名称,您可以使用以下方法:

  1. 使用EnumDisplayDevices函数枚举所有显示设备。
  2. 使用GetMonitorInfo函数获取显示设备的详细信息。

以下是一个示例代码:

代码语言:csharp
复制
using System;
using System.Runtime.InteropServices;

public class DisplayDevice
{
    public string DeviceName { get; set; }
    public string DeviceString { get; set; }
    public bool IsPrimary { get; set; }

    public DisplayDevice(string deviceName, string deviceString, bool isPrimary)
    {
        DeviceName = deviceName;
        DeviceString = deviceString;
        IsPrimary = isPrimary;
    }
}

public class MonitorInfo
{
    public const int CCHDEVICENAME = 32;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct MONITORINFOEX
    {
        public int cbSize;
        public RECT rcMonitor;
        public RECT rcWork;
        public int dwFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
        public string szDevice;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi);

    public static DisplayDevice[] GetDisplayDevices()
    {
        var devices = new System.Collections.Generic.List<DisplayDevice>();
        uint deviceIndex = 0;
        DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
        displayDevice.cb = Marshal.SizeOf(displayDevice);

        while (EnumDisplayDevices(null, deviceIndex, ref displayDevice, 0))
        {
            devices.Add(new DisplayDevice(displayDevice.DeviceName, displayDevice.DeviceString, displayDevice.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice)));
            deviceIndex++;
            displayDevice.cb = Marshal.SizeOf(displayDevice);
        }

        return devices.ToArray();
    }

    public static string GetMonitorName(IntPtr hMonitor)
    {
        var monitorInfo = new MONITORINFOEX();
        monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
        GetMonitorInfo(hMonitor, ref monitorInfo);
        return monitorInfo.szDevice;
    }
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public 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;
}

[Flags()]
public enum DisplayDeviceStateFlags : int
{
    None = 0x0,
    AttachedToDesktop = 0x1,
    MultiDriver = 0x2,
    PrimaryDevice = 0x4,
    MirroringDriver = 0x8,
    VGACompatible = 0x10,
    Removable = 0x20,
    Disconnect = 0x2000000,
    Remote = 0x4000000,
    ModeChanged = 0x8000000
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

使用示例:

代码语言:csharp
复制
var displayDevices = MonitorInfo.GetDisplayDevices();
foreach (var displayDevice in displayDevices)
{
    Console.WriteLine($"Device Name: {displayDevice.DeviceName}");
    Console.WriteLine($"Device String: {displayDevice.DeviceString}");
    Console.WriteLine($"Is Primary: {displayDevice.IsPrimary}");
}

这将输出所有显示设备的名称、字符串和是否为主显示设备。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券