简体   繁体   中英

Monitor (Display) Names and Bounds

My app has the ability to use a dual monitor configuration. In the app's settings I list the available displays where the user will choose his secondary screen.

I'm getting the (real) monitor device names using:

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())
                {
                       ...
                }
             }

But I also need the display bounds (top, left, etc) which this doesn't give me. But System.Windows.Forms.Screen does give me bounds but not real device names. I could use these together if I am certain that they return the devices in the same order every time. Will both of these always return "Device 1", "Device 2", etc in chronological order every time? Or is there a way that contains all my needed information?

[edit] Hmm. Win32_DesptopMonitor isnt giving me my secondary monitors name. Its just calling it Default Monitor. And it's listing it before my primary.

[edit2] Ok it's got the names and their resolutions messed up..... Anyone know whats going on here?

I have done this before using Win API calls. I pasted bits of the code below, which might be helpful to you...

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

And this is what gets called behind 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM