Ejemplo n.º 1
0
        /// <summary>
        /// A private helper method used to retrieve current display settings as a DEVMODE object.
        /// </summary>
        /// <remarks>
        /// Internally calls EnumDisplaySettings() native function with the value ENUM_CURRENT_SETTINGS (-1) to retrieve the current settings.
        /// </remarks>
        private static SafeNativeMethods.DEVMODE GetDeviceMode(DisplayDevice dev, bool def)
        {
            SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE();

            mode.Initialize();

            if (def)
            {
                if (SafeNativeMethods.EnumDisplaySettings(null, SafeNativeMethods.ENUM_CURRENT_SETTINGS, ref mode))
                    return mode;
                else
                    //throw new InvalidOperationException(GetLastError());
                    throw new InvalidOperationException("Hello");
            }
            else
            {

                //if (SafeNativeMethods.EnumDisplaySettings(dev.DeviceName, 0, ref mode))
                  if(SafeNativeMethods.EnumDisplaySettingsEx(dev.DeviceName, -2, ref mode, (uint)CDSFlags.CDS_RESET) )
                    return mode;
                else
                    //throw new InvalidOperationException(GetLastError());
                    throw new InvalidOperationException("Hello1");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Changes the current display settings with the new settings provided. May throw InvalidOperationException if failed. Check the exception message for more details.
        /// </summary>
        /// <param name="set">The new settings.</param>
        /// <remarks>
        /// Internally calls ChangeDisplaySettings() native function.
        /// </remarks>
        public static string SetDisplaySettings(DisplaySettings set, DisplayDevice dev, bool primary)
        {
            SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE();

            mode.dmPelsWidth = (uint)set.Width;
            mode.dmPelsHeight = (uint)set.Height;
            mode.dmDisplayOrientation = (uint)set.Orientation;
            mode.dmBitsPerPel = (uint)set.BitCount;
            mode.dmDisplayFrequency = (uint)set.Frequency;

            if (primary)
            {
                mode.dmPosition.x = 0;
                mode.dmPosition.y = 0;
            }
            else
            {
                mode.dmPosition.x = -set.Width -1;
                mode.dmPosition.y = 0;
            }

            uint flags;

            if (primary)

                flags = (uint)(CDSFlags.CDS_RESET | CDSFlags.CDS_UPDATEREGISTRY | CDSFlags.CDS_SET_PRIMARY );

            else
                flags = (uint)(CDSFlags.CDS_RESET | CDSFlags.CDS_UPDATEREGISTRY);

            mode.dmFields = (uint)(SafeNativeMethods.DM.Position | SafeNativeMethods.DM.PelsHeight | SafeNativeMethods.DM.PelsWidth|
                                    SafeNativeMethods.DM.DisplayFrequency | SafeNativeMethods.DM.DisplayOrientation|
                                    SafeNativeMethods.DM.BitsPerPixel);

            mode.dmSize = (ushort)Marshal.SizeOf(mode);

            DisplayChangeResult result = (DisplayChangeResult)
            SafeNativeMethods.ChangeDisplaySettingsEx(dev.DeviceName, ref mode, IntPtr.Zero, flags, IntPtr.Zero);

            string msg = "Successfully changed the settings!";
            switch (result)
            {
                case DisplayChangeResult.BadDualView:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadDualView;
                    break;
                case DisplayChangeResult.BadParam:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadParam;
                    break;
                case DisplayChangeResult.BadFlags:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadFlags;
                    break;
                case DisplayChangeResult.NotUpdated:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_NotUpdated;
                    break;
                case DisplayChangeResult.BadMode:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadMode;
                    break;
                case DisplayChangeResult.Failed:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Failed;
                    break;
                case DisplayChangeResult.Restart:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Restart;
                    break;
            }

            return msg;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Enumerates all supported display modes.
        /// </summary>
        /// <remarks>
        /// Internally calls EnumDisplaySettings() native function.
        /// Because of the nature of EnumDisplaySettings() it calls it many times and uses the <code>yield return</code> statement to simulate an enumerator.
        /// </remarks>
        public static IEnumerator<DisplaySettings> GetModesEnumerator(string devName)
        {
            SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE();

            mode.Initialize();
               // mode.dmDeviceName = devName;

            int idx = 0;

            while (SafeNativeMethods.EnumDisplaySettingsEx(devName, idx, ref mode, 0))
            {

                yield return CreateDisplaySettingsObject(idx++, mode);

            }
        }
Ejemplo n.º 4
0
        public static string enableDevice(DisplayDevice dev)
        {
            SafeNativeMethods.DEVMODE dm = new SafeNativeMethods.DEVMODE();

            SafeNativeMethods.POINTL enabledPosition = new SafeNativeMethods.POINTL();
            enabledPosition.x = -1280;
            enabledPosition.y = 0;

            dm.dmPosition = enabledPosition;
            dm.dmFields = (uint)SafeNativeMethods.DM.Position;

            dm.dmSize = (ushort)Marshal.SizeOf(dm);

            DisplayChangeResult result = (DisplayChangeResult)
            SafeNativeMethods.ChangeDisplaySettingsEx(dev.DeviceName, ref dm, IntPtr.Zero, (uint)CDSFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);

            string msg = "Successfully enabled the device!";
            switch (result)
            {
                case DisplayChangeResult.BadDualView:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadDualView;
                    break;
                case DisplayChangeResult.BadParam:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadParam;
                    break;
                case DisplayChangeResult.BadFlags:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadFlags;
                    break;
                case DisplayChangeResult.NotUpdated:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_NotUpdated;
                    break;
                case DisplayChangeResult.BadMode:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadMode;
                    break;
                case DisplayChangeResult.Failed:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Failed;
                    break;
                case DisplayChangeResult.Restart:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Restart;
                    break;
            }

            return msg;
        }
Ejemplo n.º 5
0
        public static string disableDevice(DisplayDevice dev)
        {
            SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE();
            /*
            mode.dmPelsWidth = 0;
            mode.dmPelsHeight = 0;
            mode.dmDisplayOrientation = 0;

            uint flags;

            flags = (uint)(CDSFlags.CDS_RESET | CDSFlags.CDS_UPDATEREGISTRY);

            mode.dmFields = (uint)(SafeNativeMethods.DM.PelsHeight | SafeNativeMethods.DM.PelsWidth);
            */
            mode.dmSize = (ushort)Marshal.SizeOf(mode);

            DisplayChangeResult result = (DisplayChangeResult)
            SafeNativeMethods.ChangeDisplaySettingsEx(dev.DeviceName, 0,0, 0, 0);

            string msg = "Successfully disabled the device!";
            switch (result)
            {
                case DisplayChangeResult.BadDualView:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadDualView;
                    break;
                case DisplayChangeResult.BadParam:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadParam;
                    break;
                case DisplayChangeResult.BadFlags:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadFlags;
                    break;
                case DisplayChangeResult.NotUpdated:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_NotUpdated;
                    break;
                case DisplayChangeResult.BadMode:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_BadMode;
                    break;
                case DisplayChangeResult.Failed:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Failed;
                    break;
                case DisplayChangeResult.Restart:
                    msg = Properties.Resources.InvalidOperation_Disp_Change_Restart;
                    break;
            }

            return msg;
        }
Ejemplo n.º 6
0
        public static void AttachSecondMonitor(DisplayDevice dev)
        {
            SafeNativeMethods.DEVMODE mode = new SafeNativeMethods.DEVMODE();

            //ZeroMemory(&mode, sizeof(DEVMODE));

            mode.dmBitsPerPel = 32;
            mode.dmPelsWidth = 1920;
            mode.dmPelsHeight = 1200;
            mode.dmPosition.x = -1920;
            mode.dmPosition.y = 0;
            mode.dmDisplayFrequency = 60;
            mode.dmDisplayOrientation = 0;

            mode.dmFields = (uint)(SafeNativeMethods.DM.PelsWidth | SafeNativeMethods.DM.PelsHeight | SafeNativeMethods.DM.BitsPerPixel |
                                   SafeNativeMethods.DM.Position | SafeNativeMethods.DM.DisplayFrequency |
                                   SafeNativeMethods.DM.DisplayFlags | SafeNativeMethods.DM.Orientation);

            mode.dmSize = (ushort)Marshal.SizeOf(mode);
            DisplayChangeResult result = (DisplayChangeResult)SafeNativeMethods.ChangeDisplaySettingsEx(dev.DeviceName, ref mode, IntPtr.Zero, (uint)(CDSFlags.CDS_UPDATEREGISTRY | CDSFlags.CDS_NORESET), IntPtr.Zero);

            if (result == 0)
            {
                Console.WriteLine("Success");
                //ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
            }
            else
            {
                Console.WriteLine(result.ToString());
            }
        }