回答
有關屏幕(製造商)的所有信息都在註冊表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]);
}
}
}
}
}
}
}
}
}
大小是釐米(只有它的整數)。
你可以有這個身體比例和對角線:
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);
}
編輯:如何擁有監視器名稱(所有連接的顯示器):
[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;
}
,並完成:
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);
}
,我發現這個
public class NativeMethods
{
[DllImport("gdi32.dll", EntryPoint = "CreateDC", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CreateDC(string lpszDriver, string lpszDeviceName, string lpszOutput, IntPtr devMode);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
private const int LOGPIXELSX = 88;
private static int _dpi = -1;
public static int DPI
{
get
{
if (_dpi != -1)
return _dpi;
_dpi = 96;
try
{
IntPtr hdc = CreateDC("DISPLAY", null, null, IntPtr.Zero);
if (hdc != IntPtr.Zero)
{
_dpi = GetDeviceCaps(hdc, LOGPIXELSX);
if (_dpi == 0)
_dpi = 96;
DeleteDC(hdc);
}
}
catch (Exception)
{
}
return _dpi;
}
}
}
使用的PInvoke得到DPI在有關這個問題的其他問題,我張貼在這裏,因爲我只看到了鏈接。
我會提及這不是一個普遍的事情,我不知道你爲什麼需要這個。這聽起來像A-B問題,所以我會說你需要絕對確定你需要使用前的DPI
請看看Lasse V. Karlsen的評論。這可能不準確,所以請小心謹慎
看起來很有前途,但不確定這是多麼正確/準確。我的BENQ BL2400屏幕報道了96的DPI,並且是全高清,但這相當於22.94英寸,而不是24英寸,因爲它應該是。 – 2014-11-05 13:47:36
@ LasseV.Karlsen我使用這段代碼尋找ppl,有人說它運行良好,有些人說它根本不起作用。沒有看到「不準確」的評論,但我會在答案中發佈 – 2014-11-05 13:49:19
- 1. 在屏幕上獲取像素大小,而不是實際大小?
- 2. 獲取屏幕大小XCode
- 3. 定義的大小不會屏幕上的實際大小
- 4. 在GTK中,如何在屏幕上獲取小部件的實際大小?
- 5. 獲取實際屏幕高度(android)
- 6. android獲取其他屏幕方向的屏幕大小
- 7. JFrame:獲取實際內容大小
- 8. Android屏幕取向大小
- 9. 如何獲得實際屏幕高度?
- 10. 獲取實際文件大小VS表觀大小在GO
- 11. Firefox:獲取視口的實際屏幕位置
- 12. 大屏幕或hdpi的主屏幕小部件大小?
- 13. 如何在Android的畫布中獲取屏幕大小的大小?
- 14. Java中的屏幕大小
- 15. 屏幕大小的Javascript
- 16. 無法取回屏幕大小android
- 17. 我如何獲得比屏幕大小更大的可視屏幕布局
- 18. 獲取屏幕的大小不使用getMetrics
- 19. 在Galaxy S8上獲取正確的屏幕顯示大小
- 20. 獲取不同大小屏幕的右角Radius問題
- 21. 的iPad - 獲取屏幕大小在縱向和橫向
- 22. 如何獲取屏幕的總像素大小
- 23. 使用xamarin獲取android片段的屏幕大小
- 24. 使用PHP獲取iframe或屏幕的大小
- 25. 如何獲取getaddrinfo返回的ai_addr-> sa_data的實際大小()
- 26. 多屏幕支持大屏幕和小屏幕
- 27. 如何在ComponentOne中獲取C1TrueDBGrid控件的實際大小?
- 28. 在呈現之前獲取用戶控件的實際大小
- 29. 我無法獲取UIElement的實際大小
- 30. Oracle - 如何獲取特定行的實際大小?
是的,這是可能的;) – Xaruth 2014-11-05 13:38:33
@Xaruth我想你的'wro ng:http://stackoverflow.com/questions/6363752/acquire-monitor-physical-dimension – 2014-11-05 13:39:10
請參閱http://ofekshilon.com/2011/11/13/reading-monitor-physical-dimensions-or-getting-the -edid-the-right-way /不確定這是否準確或不正確,因爲@NoIdeaForName發佈了一個問題的鏈接,如果你按照鏈接重複它指的是回答說不,無法完成。 – 2014-11-05 13:44:22