private static Devmode GetDevmode(int devNum, int modeNum)
        {
            var devMode = new Devmode();
            var devName = GetDeviceName(devNum);

            EnumDisplaySettings(devName, modeNum, ref devMode);
            return(devMode);
        }
        private void AddRefresh(Devmode devMode)
        {
            var set = devMode.dmDisplayFrequency + " Hz";

            if (_listRefreshRate.IndexOf(set) == -1)
            {
                _listRefreshRate.Add(set);
            }
        }
        private void AddResolution(Devmode devMode)
        {
            var set = devMode.dmPelsWidth + " x " + devMode.dmPelsHeight;

            if (_listResolution.IndexOf(set) == -1)
            {
                _listResolution.Add(set);
            }
        }
Beispiel #4
0
        public static IEnumerable <Devmode> GetDisplayModes(string device = null)
        {
            var index = 0;
            var mode  = new Devmode();

            while (EnumDisplaySettings(device, index++, ref mode))
            {
                yield return(mode);
            }
        }
Beispiel #5
0
        private static Devmode GetDm()
        {
            const int enumCurrentSettings = -1;

            Devmode devMode = default;

            devMode.dmSize = (short)Marshal.SizeOf(devMode);
            EnumDisplaySettings(null, enumCurrentSettings, ref devMode);
            return(devMode);
        }
Beispiel #6
0
        public virtual Devmode GetCurrentDisplaySettings()
        {
            var result = new Devmode();

            result.dmDeviceName = new String(new char[32]);
            result.dmSize       = (short)Marshal.SizeOf(result);
            result.dmDeviceName = Screen.PrimaryScreen.DeviceName;

            if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref result) == 0)
            {
                result = InvalidDevmode;
            }
            return(result);
        }
Beispiel #7
0
        public static string SetPrimary(string deviceName)
        {
            var id           = int.Parse(Regex.Match(deviceName, @"\d+").Value) - 1;
            var originalMode = new Devmode();

            originalMode.dmSize = (short)Marshal.SizeOf(originalMode);
            EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);
            var offsetx = originalMode.dmPositionX;
            var offsety = originalMode.dmPositionY;

            originalMode.dmPositionX = 0;
            originalMode.dmPositionY = 0;

            ChangeDisplaySettingsEx(deviceName, ref originalMode, (IntPtr)null,
                                    ChangeDisplaySettingsFlags.CDS_SET_PRIMARY | ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY |
                                    ChangeDisplaySettingsFlags.CDS_NORESET, IntPtr.Zero);
            var device = new DisplayDevice();

            device.cb = Marshal.SizeOf(device);

            // Update remaining devices
            for (uint otherid = 0; EnumDisplayDevices(null, otherid, ref device, 0); otherid++)
            {
                if (device.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop) && otherid != id)
                {
                    device.cb = Marshal.SizeOf(device);
                    var otherDeviceMode = new Devmode();

                    EnumDisplaySettings(device.DeviceName, -1, ref otherDeviceMode);

                    otherDeviceMode.dmPositionX -= offsetx;
                    otherDeviceMode.dmPositionY -= offsety;

                    ChangeDisplaySettingsEx(
                        device.DeviceName,
                        ref otherDeviceMode,
                        (IntPtr)null,
                        ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY | ChangeDisplaySettingsFlags.CDS_NORESET,
                        IntPtr.Zero);
                }

                device.cb = Marshal.SizeOf(device);
            }

            // Apply settings
            return
                (GetMessageForCode(ChangeDisplaySettingsEx(null, IntPtr.Zero, (IntPtr)null,
                                                           ChangeDisplaySettingsFlags.CDS_NONE, (IntPtr)null)));
        }
Beispiel #8
0
        public static string ChangeResolution(string deviceName, int width, int height, int bbp, int freq)
        {
            var originalMode = new Devmode();

            originalMode.dmSize = (short)Marshal.SizeOf(originalMode);
            EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);
            var newMode = originalMode;

            newMode.dmDeviceName       = deviceName;
            newMode.dmPelsWidth        = width;
            newMode.dmPelsHeight       = height;
            newMode.dmBitsPerPel       = bbp;
            newMode.dmDisplayFrequency = freq;
            return(GetMessageForCode(ChangeDisplaySettingsEx(deviceName, ref newMode, IntPtr.Zero,
                                                             ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero)));
        }
Beispiel #9
0
        private IEnumerable <ScreenSize> GetSupportedScreenResolutions()
        {
            var devmode = new Devmode();

            devmode.dmDeviceName = new String(new char[32]);
            devmode.dmSize       = (short)Marshal.SizeOf(devmode);
            devmode.dmDeviceName = Screen.PrimaryScreen.DeviceName;

            int i = 0;

            while (EnumDisplaySettings(null, i, ref devmode) == 1)
            {
                yield return(new ScreenSize {
                    Width = (uint)devmode.dmPelsWidth, Height = (uint)devmode.dmPelsHeight
                });

                i++;
            }
        }
        private void EnumModes(int devNum)
        {
            _listRefreshRate.Clear();

            var  devName = GetDeviceName(devNum);
            var  devMode = new Devmode();
            var  modeNum = 0;
            bool result;

            do
            {
                result = EnumDisplaySettings(devName,
                                             modeNum, ref devMode);

                if (result)
                {
                    AddRefresh(devMode);
                    AddResolution(devMode);
                    AddDisplay(devMode);
                }
                modeNum++;
            } while (result);
        }
Beispiel #11
0
        public static string Rotate(int angle, int width, int height, string deviceName)
        {
            var originalMode = new Devmode();

            originalMode.dmSize = (short)Marshal.SizeOf(originalMode);
            EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);

            // swap height and width
            var temp = originalMode.dmPelsHeight;

            originalMode.dmPelsHeight = originalMode.dmPelsWidth;
            originalMode.dmPelsWidth  = temp;

            originalMode.dmPelsWidth  = width;
            originalMode.dmPelsHeight = height;
            switch (angle)
            {
            case 0:
                originalMode.dmDisplayOrientation = ScreenOrientation.Angle0;
                break;

            case 90:
                originalMode.dmDisplayOrientation = ScreenOrientation.Angle90;
                break;

            case 180:
                originalMode.dmDisplayOrientation = ScreenOrientation.Angle180;
                break;

            case 270:
                originalMode.dmDisplayOrientation = ScreenOrientation.Angle270;
                break;
            }
            return(GetMessageForCode(ChangeDisplaySettingsEx(deviceName, ref originalMode, IntPtr.Zero,
                                                             ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero)));
        }
Beispiel #12
0
        public static string Rotate(int angle, int width, int height, string deviceName)
        {
            var originalMode = new Devmode();
            originalMode.dmSize = (short) Marshal.SizeOf(originalMode);
            EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);

            // swap height and width
            var temp = originalMode.dmPelsHeight;
            originalMode.dmPelsHeight = originalMode.dmPelsWidth;
            originalMode.dmPelsWidth = temp;

            originalMode.dmPelsWidth = width;
            originalMode.dmPelsHeight = height;
            switch (angle)
            {
                case 0:
                    originalMode.dmDisplayOrientation = ScreenOrientation.Angle0;
                    break;
                case 90:
                    originalMode.dmDisplayOrientation = ScreenOrientation.Angle90;
                    break;
                case 180:
                    originalMode.dmDisplayOrientation = ScreenOrientation.Angle180;
                    break;
                case 270:
                    originalMode.dmDisplayOrientation = ScreenOrientation.Angle270;
                    break;
            }
            return GetMessageForCode(ChangeDisplaySettingsEx(deviceName, ref originalMode, IntPtr.Zero,
                ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero));
        }
        private void AddResolution(Devmode devMode)
        {
            var set = devMode.dmPelsWidth + " x " + devMode.dmPelsHeight;

            if (_listResolution.IndexOf(set) == -1)
                _listResolution.Add(set);
        }
 private static extern bool EnumDisplaySettings(string devName, int modeNum, ref Devmode devMode);
Beispiel #15
0
 public static string ChangeResolution(string deviceName, int width, int height, int bbp, int freq)
 {
     var originalMode = new Devmode();
     originalMode.dmSize = (short) Marshal.SizeOf(originalMode);
     EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);
     var newMode = originalMode;
     newMode.dmDeviceName = deviceName;
     newMode.dmPelsWidth = width;
     newMode.dmPelsHeight = height;
     newMode.dmBitsPerPel = bbp;
     newMode.dmDisplayFrequency = freq;
     return GetMessageForCode(ChangeDisplaySettingsEx(deviceName, ref newMode, IntPtr.Zero,
         ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero));
 }
        private void EnumModes(int devNum)
        {
            _listRefreshRate.Clear();

            var devName = GetDeviceName(devNum);
            var devMode = new Devmode();
            var modeNum = 0;
            bool result;
            do
            {
                result = EnumDisplaySettings(devName,
                    modeNum, ref devMode);

                if (result)
                {
                    AddRefresh(devMode);
                    AddResolution(devMode);
                    AddDisplay(devMode);
                }
                modeNum++;
            } while (result);
        }
 private void AddRefresh(Devmode devMode)
 {
     var set = devMode.dmDisplayFrequency + " Hz";
     if (_listRefreshRate.IndexOf(set) == -1)
         _listRefreshRate.Add(set);
 }
Beispiel #18
0
 internal static extern int EnumDisplaySettings(string deviceName, int modeNum, ref Devmode devMode);
 public static extern int ChangeDisplaySettings(ref Devmode devMode, int flags);
        private void AddDisplay(Devmode devMode)
        {
            var set = devMode.dmPelsWidth + " x " + devMode.dmPelsHeight + " - " + devMode.dmDisplayFrequency + " Hz";

            _listDisplay.Add(set);
        }
Beispiel #21
0
        public static List<DisplayInformation> DisplayInformation()
        {
            var monitors = new List<DisplayInformation>();
            var d = new DisplayDevice();
            d.cb = Marshal.SizeOf(d);
            try
            {
                for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
                {
                    if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))
                    {
                        var device = d.DeviceName;

                        var vDevMode = new Devmode();
                        var i = 0;
                        var supportedResolutions = new Dictionary<string, List<ResolutionInformation>>();
                        while (EnumDisplaySettings(device, i, ref vDevMode))
                        {
                            var width = vDevMode.dmPelsWidth;
                            var height = vDevMode.dmPelsHeight;
                            var bpp = vDevMode.dmBitsPerPel;
                            var orientation = vDevMode.dmDisplayOrientation.ToString();
                            var freq = vDevMode.dmDisplayFrequency;
                            var resolutionKey = $"{width}x{height}";
                            var resolution = new ResolutionInformation
                            {
                                BitsPerPixel = bpp,
                                Frequency = freq,
                                Height = height,
                                Width = width,
                                Orientation = orientation
                            };
                            if (supportedResolutions.ContainsKey(resolutionKey))
                            {
                                supportedResolutions[resolutionKey].Add(resolution);
                            }
                            else
                            {
                                supportedResolutions.Add(resolutionKey, new List<ResolutionInformation>());
                                supportedResolutions[resolutionKey].Add(resolution);
                            }
                            i++;
                        }
                        var cDevMode = new Devmode();
                        EnumDisplaySettings(device, ENUM_CURRENT_SETTINGS, ref cDevMode);

                        var currentResolution = new ResolutionInformation
                        {
                            BitsPerPixel = cDevMode.dmBitsPerPel,
                            Frequency = cDevMode.dmDisplayFrequency,
                            Height = cDevMode.dmPelsHeight,
                            Width = cDevMode.dmPelsWidth,
                            Orientation = cDevMode.dmDisplayOrientation.ToString()
                        };
                        var monitor = new DisplayInformation
                        {
                            Primary = d.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice),
                            Attached = d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop),
                            Removable = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Removable),
                            VgaCompatible = d.StateFlags.HasFlag(DisplayDeviceStateFlags.VgaCompatible),
                            MirroringDriver = d.StateFlags.HasFlag(DisplayDeviceStateFlags.MirroringDriver),
                            MultiDriver = d.StateFlags.HasFlag(DisplayDeviceStateFlags.MultiDriver),
                            ModesPruned = d.StateFlags.HasFlag(DisplayDeviceStateFlags.ModesPruned),
                            Remote = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Remote),
                            Disconnect = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Disconnect),
                            FriendlyName = $"{GetAllMonitorsFriendlyNames().ElementAt((int) id)} on {d.DeviceString}",
                            SupportedResolutions = supportedResolutions,
                            CurrentResolution = currentResolution,
                            DeviceName = device
                        };
                        monitors.Add(monitor);
                        d.cb = Marshal.SizeOf(d);
                        EnumDisplayDevices(d.DeviceName, 0, ref d, 0);
                    }
                    d.cb = Marshal.SizeOf(d);
                }
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Console.WriteLine($"{ex}");
                }
            }
            return monitors;
        }
Beispiel #22
0
		public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref Devmode devMode);
Beispiel #23
0
 public static extern bool EnumDisplaySettings(
     string deviceName, int modeNum, ref Devmode devMode);
Beispiel #24
0
 public static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref Devmode lpDevMode,
                                                          IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
 private static extern bool EnumDisplaySettings(string devName, int modeNum, ref Devmode devMode);
Beispiel #26
0
        public static string SetPrimary(string deviceName)
        {
            var id = int.Parse(Regex.Match(deviceName, @"\d+").Value) - 1;
            var originalMode = new Devmode();
            originalMode.dmSize = (short) Marshal.SizeOf(originalMode);
            EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref originalMode);
            var offsetx = originalMode.dmPositionX;
            var offsety = originalMode.dmPositionY;
            originalMode.dmPositionX = 0;
            originalMode.dmPositionY = 0;

            ChangeDisplaySettingsEx(deviceName, ref originalMode, (IntPtr) null,
                ChangeDisplaySettingsFlags.CDS_SET_PRIMARY | ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY |
                ChangeDisplaySettingsFlags.CDS_NORESET, IntPtr.Zero);
            var device = new DisplayDevice();
            device.cb = Marshal.SizeOf(device);

            // Update remaining devices
            for (uint otherid = 0; EnumDisplayDevices(null, otherid, ref device, 0); otherid++)
            {
                if (device.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop) && otherid != id)
                {
                    device.cb = Marshal.SizeOf(device);
                    var otherDeviceMode = new Devmode();

                    EnumDisplaySettings(device.DeviceName, -1, ref otherDeviceMode);

                    otherDeviceMode.dmPositionX -= offsetx;
                    otherDeviceMode.dmPositionY -= offsety;

                    ChangeDisplaySettingsEx(
                        device.DeviceName,
                        ref otherDeviceMode,
                        (IntPtr) null,
                        ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY | ChangeDisplaySettingsFlags.CDS_NORESET,
                        IntPtr.Zero);
                }

                device.cb = Marshal.SizeOf(device);
            }

            // Apply settings
            return
                GetMessageForCode(ChangeDisplaySettingsEx(null, IntPtr.Zero, (IntPtr) null,
                    ChangeDisplaySettingsFlags.CDS_NONE, (IntPtr) null));
        }
 private static Devmode GetDevmode(int devNum, int modeNum)
 {
     var devMode = new Devmode();
     var devName = GetDeviceName(devNum);
     EnumDisplaySettings(devName, modeNum, ref devMode);
     return devMode;
 }
Beispiel #28
0
        private static List <DisplayInformation> UpdateDisplays()
        {
            var monitors = new List <DisplayInformation>();
            var d        = new DisplayDevice();

            d.cb = Marshal.SizeOf(d);
            try
            {
                for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
                {
                    if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))
                    {
                        var device = d.DeviceName;

                        var vDevMode             = new Devmode();
                        var i                    = 0;
                        var supportedResolutions = new Dictionary <string, List <ResolutionInformation> >();
                        while (EnumDisplaySettings(device, i, ref vDevMode))
                        {
                            var width         = vDevMode.dmPelsWidth;
                            var height        = vDevMode.dmPelsHeight;
                            var bpp           = vDevMode.dmBitsPerPel;
                            var orientation   = vDevMode.dmDisplayOrientation.ToString();
                            var freq          = vDevMode.dmDisplayFrequency;
                            var resolutionKey = $"{width}x{height}";
                            var resolution    = new ResolutionInformation
                            {
                                BitsPerPixel = bpp,
                                Frequency    = freq,
                                Height       = height,
                                Width        = width,
                                Orientation  = orientation
                            };
                            if (supportedResolutions.ContainsKey(resolutionKey))
                            {
                                supportedResolutions[resolutionKey].Add(resolution);
                            }
                            else
                            {
                                supportedResolutions.Add(resolutionKey, new List <ResolutionInformation>());
                                supportedResolutions[resolutionKey].Add(resolution);
                            }
                            i++;
                        }
                        var cDevMode = new Devmode();
                        EnumDisplaySettings(device, ENUM_CURRENT_SETTINGS, ref cDevMode);

                        var currentResolution = new ResolutionInformation
                        {
                            BitsPerPixel = cDevMode.dmBitsPerPel,
                            Frequency    = cDevMode.dmDisplayFrequency,
                            Height       = cDevMode.dmPelsHeight,
                            Width        = cDevMode.dmPelsWidth,
                            Orientation  = cDevMode.dmDisplayOrientation.ToString(),
                            X            = cDevMode.dmPositionX,
                            Y            = cDevMode.dmPositionY
                        };
                        var monitor = new DisplayInformation
                        {
                            Primary              = d.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice),
                            Attached             = d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop),
                            Removable            = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Removable),
                            VgaCompatible        = d.StateFlags.HasFlag(DisplayDeviceStateFlags.VgaCompatible),
                            MirroringDriver      = d.StateFlags.HasFlag(DisplayDeviceStateFlags.MirroringDriver),
                            MultiDriver          = d.StateFlags.HasFlag(DisplayDeviceStateFlags.MultiDriver),
                            ModesPruned          = d.StateFlags.HasFlag(DisplayDeviceStateFlags.ModesPruned),
                            Remote               = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Remote),
                            Disconnect           = d.StateFlags.HasFlag(DisplayDeviceStateFlags.Disconnect),
                            FriendlyName         = $"{GetAllMonitorsFriendlyNames().ElementAt((int)id)} on {d.DeviceString}",
                            SupportedResolutions = supportedResolutions,
                            CurrentResolution    = currentResolution,
                            DeviceName           = device
                        };
                        monitors.Add(monitor);
                        d.cb = Marshal.SizeOf(d);
                        EnumDisplayDevices(d.DeviceName, 0, ref d, 0);
                    }
                    d.cb = Marshal.SizeOf(d);
                }
                return(monitors);
            }
            catch
            {
            }
            string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;

            Console.WriteLine(errorMessage);
            return(monitors);
        }
 private void AddDisplay(Devmode devMode)
 {
     var set = devMode.dmPelsWidth + " x " + devMode.dmPelsHeight + " - " + devMode.dmDisplayFrequency + " Hz";
     _listDisplay.Add(set);
 }
Beispiel #30
0
		public static extern int ChangeDisplaySettings(ref Devmode devMode, int flags);
Beispiel #31
0
 public static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref Devmode lpDevMode,
     IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);