Beispiel #1
0
            public static Display_Device CreateWithDefaults()
            {
                var dd = new Display_Device();

                dd.Size = Marshal.SizeOf(dd);
                return(dd);
            }
Beispiel #2
0
 static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref Display_Device lpDisplayDevice, uint dwFlags);
Beispiel #3
0
        public static List <SystemDisplay> GetAllDisplays()
        {
            List <SystemDisplay> allDisplays = new List <SystemDisplay>();
            IntPtr hMainWindowMonitor        = IntPtr.Zero;
            IntPtr mainWindowHandle          = IntPtr.Zero;

            EWFDebugging.LogLine("Getting all displays.", 0, 4);
            try
            {
                mainWindowHandle = GetProcessMainWindow();
                if (mainWindowHandle != IntPtr.Zero)
                {
                    var mainWindowMonitorInfoEx = MonitorInfoEx.CreateWithDefaults();
                    hMainWindowMonitor = MonitorFromWindow(mainWindowHandle, MONITOR_DEFAULTTONEAREST);
                    LogWin32Error("Error finding main window monitor");
                    if (hMainWindowMonitor != IntPtr.Zero)
                    {
                        GetMonitorInfo(hMainWindowMonitor, ref mainWindowMonitorInfoEx);
                        LogWin32Error("Error getting main window monitor info");
                    }
                }
                else
                {
                    EWFDebugging.LogError("Could not find the process main window handle.");
                }
            }
            catch (Exception e)
            {
                string err = "Error finding the main window monitor. " + e.ToString();
                Debug.LogError(err);
                EWFDebugging.LogError(err);
            }

            var deviceDisplayMonitorCount = new Dictionary <string, uint>();

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
            {
                try
                {
                    //Get the monitor info
                    var monitorInfoEx = MonitorInfoEx.CreateWithDefaults();
                    GetMonitorInfo(hMonitor, ref monitorInfoEx);
                    LogWin32Error();

                    //Get the associated display device
                    bool mirroringDriver   = false;
                    bool attachedToDesktop = false;
                    string deviceName      = monitorInfoEx.DeviceName;

                    if (!deviceDisplayMonitorCount.ContainsKey(deviceName))
                    {
                        deviceDisplayMonitorCount.Add(deviceName, 0);
                    }
                    deviceDisplayMonitorCount[deviceName] += 1;

                    var displayDevice  = Display_Device.CreateWithDefaults();
                    int displayMonitor = 0;
                    for (uint id = 0; EnumDisplayDevices(deviceName, id, ref displayDevice, 0); id++)
                    {
                        attachedToDesktop = ((displayDevice.StateFlags & DisplayDeviceStateFlags.AttachedToDesktop) == DisplayDeviceStateFlags.AttachedToDesktop);

                        if (attachedToDesktop)
                        {
                            displayMonitor++;
                            if (displayMonitor == deviceDisplayMonitorCount[deviceName])
                            {
                                mirroringDriver = ((displayDevice.StateFlags & DisplayDeviceStateFlags.MirroringDriver) == DisplayDeviceStateFlags.MirroringDriver);
                                break;     //Found the display device which matches the monitor
                            }
                        }

                        displayDevice.Size = Marshal.SizeOf(displayDevice);
                    }

                    //Skip the monitor if it's a pseudo monitor
                    if (mirroringDriver)
                    {
                        return(true);
                    }

                    //Store the monitor info in a SystemDisplay object
                    var display  = new SystemDisplay();
                    display.Name = displayDevice.DeviceString;
                    display.AttachedToDesktop = attachedToDesktop;     //Should always be true within EnumDisplayMonitors
                    display.IsPrimary         = monitorInfoEx.Flags == (uint)1;
                    display.HasMainWindow     = (hMonitor == hMainWindowMonitor);
                    display.Bounds            = RectFromRectStruct(lprcMonitor);
                    display.WorkArea          = RectFromRectStruct(monitorInfoEx.WorkAreaBounds);

                    var devMode = new DEVMODE();
                    EnumDisplaySettings(monitorInfoEx.DeviceName, ENUM_CURRENT_SETTINGS, ref devMode);
                    display.PixelWidth  = devMode.dmPelsWidth;
                    display.PixelHeight = devMode.dmPelsHeight;

                    //Add the SystemDisplay to allDisplays
                    allDisplays.Add(display);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    EWFDebugging.LogError(e.ToString());
                }
                LogWin32Error();

                return(true);    //Continue the enumeration
            }, IntPtr.Zero);
            LogWin32Error();

            //Calculate physical bounds
            foreach (var display in allDisplays)
            {
                Rect physicalBounds = display.Bounds;
                physicalBounds.width  = display.PixelWidth;
                physicalBounds.height = display.PixelHeight;
                Vector2 displayTopLeft = new Vector2(display.Bounds.xMin, display.Bounds.yMin);

                var displayTopLeftPhysical = GetPhysicalPoint(mainWindowHandle, displayTopLeft);
                physicalBounds.x       = displayTopLeftPhysical.x;
                physicalBounds.y       = displayTopLeftPhysical.y;
                display.PhysicalBounds = physicalBounds;
            }

            return(allDisplays);
        }