要使用WinAPI/C#获取监视器/显示设备名称,您可以使用以下方法:
EnumDisplayDevices
函数枚举所有显示设备。GetMonitorInfo
函数获取显示设备的详细信息。以下是一个示例代码:
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;
}
使用示例:
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}");
}
这将输出所有显示设备的名称、字符串和是否为主显示设备。
领取专属 10元无门槛券
手把手带您无忧上云