Пример #1
0
        public static void SwapMonitors(Monitor monitor0, Monitor monitor1)
        {
            Monitor primary   = monitor0.IsPrimary ? monitor0 : monitor1;
            Monitor secondary = monitor0.IsPrimary ? monitor1 : monitor0;

            Win32.DEVMODE primarySettings   = primary.GetCurrentDisplaySettingsStructure();
            Win32.DEVMODE secondarySettings = secondary.GetCurrentDisplaySettingsStructure();

            const uint dwFlags = Win32.CDS_UPDATEREGISTRY | Win32.CDS_NORESET;

            if (PreferenceSettings.PrimaryMonitor == PreferenceSettings.PrimaryMonitorEnum.LeftSide)
            {
                primarySettings.dmPositionX   = secondary.CurrentDisplaySettings.dmPelsWidth;
                secondarySettings.dmPositionX = 0;

                Win32.ChangeDisplaySettingsEx(primary.Device.DeviceName, ref primarySettings, (IntPtr)null,
                                              dwFlags, (IntPtr)null);
                Win32.ChangeDisplaySettingsEx(secondary.Device.DeviceName, ref secondarySettings, (IntPtr)null,
                                              dwFlags | Win32.CDS_SET_PRIMARY, (IntPtr)null);
            }
            else
            {
                primarySettings.dmPositionX   = -primary.CurrentDisplaySettings.dmPelsWidth;
                secondarySettings.dmPositionX = 0;
                Win32.ChangeDisplaySettingsEx(secondary.Device.DeviceName, ref secondarySettings, (IntPtr)null,
                                              dwFlags, (IntPtr)null);
                Win32.ChangeDisplaySettingsEx(primary.Device.DeviceName, ref primarySettings, (IntPtr)null,
                                              dwFlags | Win32.CDS_SET_PRIMARY, (IntPtr)null);
            }

            Win32.ChangeDisplaySettingsEx(null, null, (IntPtr)null, 0, (IntPtr)null);
        }
Пример #2
0
 public Monitor(Win32.DISPLAY_DEVICE device, Win32.DEVMODE currentDisplaySettings, List <Win32.DEVMODE> availableDisplaySettings)
 {
     Device = device;
     AvailableDisplaySettings = availableDisplaySettings;
     CurrentDisplaySettings   = currentDisplaySettings;
     IsPrimary = CurrentDisplaySettings.dmPositionX == 0 && CurrentDisplaySettings.dmPositionY == 0;
 }
Пример #3
0
            public Win32.DEVMODE GetCurrentDisplaySettingsStructure()
            {
                var displaySettings = new Win32.DEVMODE();

                displaySettings.dmSize = (short) Marshal.SizeOf(displaySettings);
                //displaySettings.dmPelsWidth = CurrentDisplaySettings.dmPelsWidth;
                //displaySettings.dmPelsHeight = CurrentDisplaySettings.dmPelsHeight;
                //displaySettings.dmDisplayFlags = CurrentDisplaySettings.dmDisplayFlags;
                //displaySettings.dmDisplayFrequency = CurrentDisplaySettings.dmDisplayFrequency;
                displaySettings.dmPositionX = CurrentDisplaySettings.dmPositionX;
                displaySettings.dmPositionY = CurrentDisplaySettings.dmPositionY;
                displaySettings.dmFields = (int)Win32.DM_POSITION;
                return displaySettings;
            }
Пример #4
0
            public Win32.DEVMODE GetCurrentDisplaySettingsStructure()
            {
                var displaySettings = new Win32.DEVMODE();

                displaySettings.dmSize = (short)Marshal.SizeOf(displaySettings);
                //displaySettings.dmPelsWidth = CurrentDisplaySettings.dmPelsWidth;
                //displaySettings.dmPelsHeight = CurrentDisplaySettings.dmPelsHeight;
                //displaySettings.dmDisplayFlags = CurrentDisplaySettings.dmDisplayFlags;
                //displaySettings.dmDisplayFrequency = CurrentDisplaySettings.dmDisplayFrequency;
                displaySettings.dmPositionX = CurrentDisplaySettings.dmPositionX;
                displaySettings.dmPositionY = CurrentDisplaySettings.dmPositionY;
                displaySettings.dmFields    = (int)Win32.DM_POSITION;
                return(displaySettings);
            }
Пример #5
0
        private static Win32.DEVMODE EnumerateCurrentDisplaySettings(string deviceName)
        {
            Win32.DEVMODE deviceMode = new Win32.DEVMODE();
            deviceMode.dmSize = (short)Marshal.SizeOf(deviceMode);

            bool success = Win32.EnumDisplaySettingsEx(deviceName, Win32.ENUM_CURRENT_SETTINGS, ref deviceMode, 0);

            if (!success)
            {
                throw new Exception("Unable to retrieve the current display settings.");
            }

            return(deviceMode);
        }
Пример #6
0
        private void DisplayDebugInfo()
        {
            StringBuilder debug = new StringBuilder();

            foreach (Display.Monitor monitor in _Monitors)
            {
                Win32.DISPLAY_DEVICE device = monitor.Device;

                debug.AppendFormat("DeviceName:   {0}{1}", device.DeviceName, Environment.NewLine);
                debug.AppendFormat("DeviceString: {0}{1}", device.DeviceString, Environment.NewLine);
                debug.AppendFormat("DeviceString: {0}{1}", device.StateFlags, Environment.NewLine);
                debug.AppendFormat("DeviceID:     {0}{1}", device.DeviceID, Environment.NewLine);
                debug.AppendFormat("DeviceKey:    {0}{1}", device.DeviceKey, Environment.NewLine);

                Win32.DEVMODE currMode = monitor.CurrentDisplaySettings;
                debug.AppendFormat("Resolution:   {0}x{1}x{2}{3}", currMode.dmPelsWidth, currMode.dmPelsHeight,
                                   currMode.dmBitsPerPel, Environment.NewLine);
                debug.AppendFormat("Refresh Rate: {0}hz{1}", currMode.dmDisplayFrequency, Environment.NewLine);
                debug.AppendFormat("Position:{0}", Environment.NewLine);
                debug.AppendFormat("  X: {0}{1}", currMode.dmPositionX, Environment.NewLine);
                debug.AppendFormat("  Y: {0}{1}", currMode.dmPositionY, Environment.NewLine);

                debug.AppendFormat("Available Modes: {0}", Environment.NewLine);

                foreach (Win32.DEVMODE mode in monitor.AvailableDisplaySettings)
                {
                    Win32.DEVMODE curr     = monitor.CurrentDisplaySettings;
                    bool          selected = false;

                    if (mode.dmPelsWidth == curr.dmPelsWidth &&
                        mode.dmPelsHeight == curr.dmPelsHeight &&
                        mode.dmPelsWidth == curr.dmPelsWidth &&
                        mode.dmBitsPerPel == curr.dmBitsPerPel &&
                        mode.dmDisplayFrequency == curr.dmDisplayFrequency)
                    {
                        selected = true;
                    }

                    debug.AppendFormat("    {0}x{1}x{2} {3}hz {4}:{5} {6}{7}", mode.dmPelsWidth,
                                       mode.dmPelsHeight, mode.dmBitsPerPel, mode.dmDisplayFrequency, mode.dmPositionX,
                                       mode.dmPositionY, selected ? "<--" : "", Environment.NewLine);
                }

                debug.Append(Environment.NewLine);
            }
            DebugText.Text = debug.ToString();
        }
Пример #7
0
        private static List <Win32.DEVMODE> EnumerateAllDisplaySettings(string deviceName)
        {
            var displaySettings = new List <Win32.DEVMODE>();

            for (uint i = 0; ; i++)
            {
                Win32.DEVMODE deviceMode = new Win32.DEVMODE();
                deviceMode.dmSize = (short)Marshal.SizeOf(deviceMode);

                bool success = Win32.EnumDisplaySettingsEx(deviceName, i, ref deviceMode, 0);

                if (!success)
                {
                    break;
                }

                displaySettings.Add(deviceMode);
            }

            return(displaySettings);
        }
Пример #8
0
        private static Win32.DEVMODE EnumerateCurrentDisplaySettings(string deviceName)
        {
            Win32.DEVMODE deviceMode = new Win32.DEVMODE();
            deviceMode.dmSize = (short) Marshal.SizeOf(deviceMode);

            bool success = Win32.EnumDisplaySettingsEx(deviceName, Win32.ENUM_CURRENT_SETTINGS, ref deviceMode, 0);

            if (!success) throw new Exception("Unable to retrieve the current display settings.");
            
            return deviceMode;
        }
Пример #9
0
        private static List<Win32.DEVMODE> EnumerateAllDisplaySettings(string deviceName)
        {
            var displaySettings = new List<Win32.DEVMODE>();

            for (uint i = 0; ; i++)
            {
                Win32.DEVMODE deviceMode = new Win32.DEVMODE();
                deviceMode.dmSize = (short)Marshal.SizeOf(deviceMode);

                bool success = Win32.EnumDisplaySettingsEx(deviceName, i, ref deviceMode, 0);

                if (!success) break;

                displaySettings.Add(deviceMode);
            }

            return displaySettings;
        }