3

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

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

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 没有给我我的辅助监视器名称。它只是将其称为默认监视器。它在我的初选之前列出。

[edit2] 好的,名字和他们的决议搞砸了.....有人知道这里发生了什么吗?

4

1 回答 1

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;
}
于 2012-07-04T05:50:12.227 回答