2011-09-20 82 views
1

在編寫WPF應用程序時,我想要獲取在標準窗口顯示設置對話框中找到的顯示名稱。我試過WMI查詢Win32_DesktopMonitor和System.Windows.Forms.Screen.AllScreens。獲取與在顯示設置中找到的顯示名稱相匹配的名稱

,顯示在標準的Windows顯示設置列表中的名稱是:

Mobile PC Display 
DELL 2407WFP 

Win32_DesktopMonitor提供了以下(剝離不相關的信息):

DISPLAY 1 
Caption = Generic PnP Monitor 
CreationClassName = Win32_DesktopMonitor 
Description = Generic PnP Monitor 
DeviceID = DesktopMonitor1 
MonitorManufacturer = (Standard monitor types) 
MonitorType = Generic PnP Monitor 
Name = Generic PnP Monitor 
PNPDeviceID = DISPLAY\DELA017\5&2F0149CC&0&UID1078064 

DISPLAY 2 
Caption = Generic PnP Monitor 
CreationClassName = Win32_DesktopMonitor 
Description = Generic PnP Monitor 
DeviceID = DesktopMonitor2 
MonitorManufacturer = (Standard monitor types) 
MonitorType = Generic PnP Monitor 
Name = Generic PnP Monitor 
PNPDeviceID = DISPLAY\CMO1720\4&164FD10C&0&UID67568640 

System.Windows.Forms.Screen。 AllScreens提供設備清單(無關信息除外):

DISPLAY 1 
DeviceName = \\.\DISPLAY1 

DISPLAY 2 
DeviceName = \\.\DISPLAY3 

很明顯,我應該能夠交叉引用設備名稱,設備ID或PNPDeviceID與其他地方的名單來獲取名稱,不是嗎?

請不要因爲這個而憋悶我,我搜索了所有我能想到的東西,我發現的所有信息都是關於AllScreens和Win32_DesktopMonitor的信息,但沒有找到我們在標準窗口顯示設置對話框中看到的顯示名稱。

非常感謝。

回答

1

它看起來像有人問這個在MSDN Forums完全相同的問題。

中有兩個相關的答案,我會在這裏重複:

我不知道究竟你通過監視器名稱的意思,唯一 監測的名字嗎?如果您撥打EnumDisplayDevices來枚舉 監視器,則可以指定以下標誌: EDD_GET_DEVICE_INTERFACE_NAME然後在 DISPLAY_DEVICE結構的DeviceID字段中,您將看到唯一的監視器名稱。

依次爲:

謝謝你,你的解決方案几乎是100%存在。我需要添加的唯一東西 是第二次調用EnumDisplayDevices並傳遞從第一次調用返回的 DeviceName。然後,設備名是 填入顯示器的名稱,而不是視頻卡。完善!

我自己並沒有這樣做,所以我無法驗證它是否有效,但似乎基於MSDN線程的OP說它對他有效。

0

對於completedness的緣故,這裏的實際代碼類似於MSDN答案:

DISPLAY_DEVICE DisplayDevice = new DISPLAY_DEVICE(); 
const int EDD_GET_DEVICE_INTERFACE_NAME = 0x1; 
int NumberOfMonitor = 0 /* You can either iterate over EnumDisplayDevices until 
          it returns false for the number of attached monitors, 
          or use 0 for the primary monitor.*/ 

EnumDisplayDevices(null, NumberOfMonitor, ref DisplayDevice, 0)) { 
EnumDisplayDevices(DisplayDevice.DeviceName, NumberOfMonitor, ref DisplayDevice, EDD_GET_DEVICE_INTERFACE_NAME); 

DisplayDevice對象是一個結構稱爲DISPLAY_DEVICE

[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; 
} 
+0

DISPLAY_DEVICE是不是在C#中已知的任何類雖然。這是從哪裏來的,如何在C#中訪問?我得到的只是(相當有限的)「屏幕」類。 – Nyerguds

+0

這不是一個類,它是我的文章中的結構。 – Lennart