简体   繁体   English

监视(显示)名称和界限

[英]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 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. System.Windows.Forms.Screen确实给了我边界但不是真正的设备名称。 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? 这些都会每次按时间顺序返回“设备1”,“设备2”等吗? Or is there a way that contains all my needed information? 或者有没有一种方法可以包含我所需要的所有信息?

[edit] Hmm. [编辑]嗯。 Win32_DesptopMonitor isnt giving me my secondary monitors name. Win32_DesptopMonitor不给我我的辅助监视器名称。 Its just calling it Default Monitor. 它只是称它为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? [edit2]好吧,它的名字和他们的决议搞砸了.....任何人都知道这里发生了什么?

I have done this before using Win API calls. 我在使用Win API调用之前已经这样做了。 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 : 这就是new ScreenInfo背后的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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