监视(显示)名称和界限

时间:2012-07-04 00:48:08

标签: c# multiple-monitors

我的应用程序可以使用双显示器配置。在应用程序的设置中,我列出了用户将选择其辅助屏幕的可用显示。

我使用以下方式获取(真实)监视器设备名称:

http://msdn.microsoft.com/en-us/library/aa394122%28VS.85%29.aspx

    SelectQuery q = new SelectQuery("SELECT Name, DeviceID, ScreenHeight, ScreenWidth FROM Win32_DesktopMonitor");
 using (ManagementObjectSearcher mos = new ManagementObjectSearcher(q))
            {
                foreach (ManagementObject mo in mos.Get())
                {
                       ...
                }
             }

但是我还需要显示范围(顶部,左边等),这不会给我。但是System.Windows.Forms.Screen确实给了我界限但不是真正的设备名称。如果我确定每次都以相同的顺序返回设备,我可以一起使用它们。这些都会每次按时间顺序返回“设备1”,“设备2”等吗?或者有没有一种方法可以包含我所需要的所有信息?

[编辑] 嗯。 Win32_DesptopMonitor不给我我的辅助监视器名称。它只是称它为Default Monitor。它在我的主要版本之前列出它。

[EDIT2] 好吧,这些名字和他们的决议搞砸了.....任何人都知道这里发生了什么?

1 个答案:

答案 0 :(得分:1)

我在使用Win API调用之前已经这样做了。我粘贴了下面的代码,这可能对你有所帮助......

public void Store()
{
  Screens.Clear();
  uint iAdaptorNum = 0;
  Win.User32.DISPLAY_DEVICE adaptor = new Win.User32.DISPLAY_DEVICE();
        adaptor.cb = (short)Marshal.SizeOf(adaptor);
  Win.User32.DISPLAY_DEVICE dd = new Win.User32.DISPLAY_DEVICE();
  dd.cb = (short)Marshal.SizeOf(dd);

  while (Win.User32.EnumDisplayDevices(null, iAdaptorNum, ref adaptor, Win.User32.EDD_GET_DEVICE_INTERFACE_NAME))
  {
    uint iDevNum = 0;
    while (Win.User32.EnumDisplayDevices(adaptor.DeviceName, iDevNum, ref dd, Win.User32.EDD_GET_DEVICE_INTERFACE_NAME))
    {
      log.WriteFormat(LogLevel.Debug, "Adaptor {0}:{1} {2}='{3}', Device {4}='{5}', State flags = {4}",
        iAdaptorNum, iDevNum, adaptor.DeviceName, adaptor.DeviceString, dd.DeviceName, dd.DeviceString, dd.StateFlags);
      if ((dd.StateFlags & Win.User32.DisplayDeviceStateFlags.AttachedToDesktop) > 0)
        Screens.Add(new ScreenInfo(adaptor, dd));
      iDevNum++;
    }
    iAdaptorNum++;
  }
}

这就是在new ScreenInfo后面调用的内容:

public void StoreScreen(Win.User32.DISPLAY_DEVICE Adaptor, Win.User32.DISPLAY_DEVICE Device)
    {
  adaptor = Adaptor.DeviceName;
  device = Device.DeviceName;
  name = string.Format("{0} on {1}", Device.DeviceString, Adaptor.DeviceString);
  Win.User32.DEVMODE dm = newDevMode();
  if (Win.User32.EnumDisplaySettings(Adaptor.DeviceName, Win.User32.ENUM_CURRENT_SETTINGS, ref dm) != 0)
  {
    isAttached = (Adaptor.StateFlags & Win.User32.DisplayDeviceStateFlags.AttachedToDesktop) > 0;
    isPrimary = (Adaptor.StateFlags & Win.User32.DisplayDeviceStateFlags.PrimaryDevice) > 0;
    mode = findMode(Adaptor.DeviceName, dm);
    if ((dm.dmFields & Win.User32.DM.PelsWidth) > 0) width = dm.dmPelsWidth;
    if ((dm.dmFields & Win.User32.DM.PelsHeight) > 0) height = dm.dmPelsHeight;
    if ((dm.dmFields & Win.User32.DM.BitsPerPixel) > 0) bpp = dm.dmBitsPerPel;
    if ((dm.dmFields & Win.User32.DM.Orientation) > 0) orientation = dm.dmOrientation;
    if ((dm.dmFields & Win.User32.DM.DisplayFrequency) > 0) frequency = dm.dmDisplayFrequency;
    if ((dm.dmFields & Win.User32.DM.DisplayFlags) > 0) flags = dm.dmDisplayFlags;
    if ((dm.dmFields & Win.User32.DM.Position) > 0)
    {
      posX = dm.dmPosition.x;
      posY = dm.dmPosition.y;
    }
  }
}

private static Win.User32.DEVMODE newDevMode()
{
  Win.User32.DEVMODE dm = new Win.User32.DEVMODE();
  dm.dmDeviceName = new String(new char[31]);
  dm.dmFormName = new String(new char[31]);
  dm.dmSize = (short)Marshal.SizeOf(dm);
  return dm;
}