Exemplo n.º 1
0
        static public void CaptureCurrentConfigAndSaveAsPreset()
        {
            bool uniqueNameEntered = false;

            string presetName;

            DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection();

            do
            {
                Console.Write("Enter name for new preset: ");
                presetName = Console.ReadLine();

                DisplayPreset existingDisplayPreset = displayPresetCollection.GetPreset(presetName);

                uniqueNameEntered = existingDisplayPreset == null;

                if (!uniqueNameEntered)
                {
                    Console.WriteLine("Preset with name '" + presetName + "' already exists. Please choose a different name.");
                }
            } while (!uniqueNameEntered);

            DisplayPreset newPreset = DisplayPresetRecorderAndApplier.RecordCurrentConfiguration(presetName);

            if (!displayPresetCollection.TryAddDisplayPreset(newPreset))
            {
                throw new Exception("Failed to add new preset to saved presets collection.");   // This is really unexpected because we've checked for existence already.
            }

            Console.WriteLine("Added new preset!");
            ConsoleOutputUtilities.WriteDisplayPresetToConsole(newPreset);
        }
Exemplo n.º 2
0
        public static void ListPresetDetail()
        {
            DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection();

            List <DisplayPreset> displayPresets = displayPresetCollection.GetPresets();

            Console.WriteLine("Available presets: ");

            IEnumerator <DisplayPreset> displayPresetsEnumerator = displayPresets.GetEnumerator();

            for (int i = 0; i < displayPresets.Count; i++)
            {
                displayPresetsEnumerator.MoveNext();
                Console.WriteLine(String.Format("[{0}] {1}", i, displayPresetsEnumerator.Current.Name));
            }

            Console.Write("Select preset: ");
            string selection = Console.ReadLine();

            int    selectedPresetIndex = -1;
            string selectedPresetName  = String.Empty;

            if (!Int32.TryParse(selection, out selectedPresetIndex))
            {
                // If it's not a number, assume the user typed in a name
                selectedPresetName = selection;
            }
            else if (selectedPresetIndex >= displayPresets.Count)
            {
                Console.WriteLine("Invalid selection!");
                return;
            }
            else
            {
                selectedPresetName = displayPresets[selectedPresetIndex].Name;
            }

            DisplayPreset selectedPreset = displayPresetCollection.GetPreset(selectedPresetName);

            if (selectedPreset == null)
            {
                Console.WriteLine("Invalid preset name: " + selectedPresetName);
                return;
            }

            ConsoleOutputUtilities.WriteDisplayPresetToConsole(selectedPreset);
        }
Exemplo n.º 3
0
        public static void TestCCDExampleCode(CCD.QueryDisplayFlags queryDisplayFlags)
        {
            int resultCode;

            int numPathArrayElements;
            int numModeInfoArrayElements;

            // Get buffer size required to enumerate all valid paths.
            resultCode = CCD.GetDisplayConfigBufferSizes(queryDisplayFlags, out numPathArrayElements, out numModeInfoArrayElements);

            Win32Utilities.ThrowIfResultCodeNotSuccess(resultCode);

            CCD.DisplayConfigPathInfo[] pathInfoArray = new CCD.DisplayConfigPathInfo[numPathArrayElements];
            CCD.DisplayConfigModeInfo[] modeInfoArray = new CCD.DisplayConfigModeInfo[numModeInfoArrayElements];

            resultCode = CCD.QueryDisplayConfig(
                queryDisplayFlags,
                ref numPathArrayElements,
                pathInfoArray,
                ref numModeInfoArrayElements,
                modeInfoArray,
                IntPtr.Zero);

            Win32Utilities.ThrowIfResultCodeNotSuccess(resultCode);

            foreach (CCD.DisplayConfigPathInfo configPathInfo in pathInfoArray)
            {
                ConsoleOutputUtilities.WriteDisplayConfigPathInfoToConsole(configPathInfo);
                Console.WriteLine();
            }

            foreach (CCD.DisplayConfigModeInfo configModeInfo in modeInfoArray)
            {
                ConsoleOutputUtilities.WriteDisplayConfigModeInfoToConsole(configModeInfo);
                Console.WriteLine();
            }

            // Find and store the primary path based by looking for an active path that is located at desktop position
            // 0,0
            CCD.DisplayConfigPathInfo primaryPath = new CCD.DisplayConfigPathInfo();
            bool primaryPathFound = false;

            foreach (CCD.DisplayConfigPathInfo configPathInfo in pathInfoArray)
            {
                if ((configPathInfo.flags & (uint)CCD.DisplayConfigFlags.PathActive) > 0)
                {
                    if (configPathInfo.sourceInfo.modeInfoIdx > modeInfoArray.Length - 1)
                    {
                        throw new Exception("Config Path Info Source Mode Info Index is out of range.");
                    }

                    CCD.DisplayConfigModeInfo modeInfo = modeInfoArray[configPathInfo.sourceInfo.modeInfoIdx];

                    if (modeInfo.infoType == CCD.DisplayConfigModeInfoType.Source &&
                        modeInfo.sourceMode.position.x == 0 &&
                        modeInfo.sourceMode.position.y == 0)
                    {
                        // Bingo
                        primaryPath      = configPathInfo;
                        primaryPathFound = true;
                        break;
                    }
                }
            }

            if (!primaryPathFound)
            {
                throw new Exception("Failed to find primary display path!");
            }

            CCD.DisplayConfigTargetDeviceName dcTargetDeviceName = new CCD.DisplayConfigTargetDeviceName();
            dcTargetDeviceName.header.type      = CCD.DisplayConfigDeviceInfoType.GetTargetName;
            dcTargetDeviceName.header.size      = (uint)Marshal.SizeOf(dcTargetDeviceName);
            dcTargetDeviceName.header.adapterId = primaryPath.targetInfo.adapterId;
            dcTargetDeviceName.header.id        = primaryPath.targetInfo.id;

            resultCode = CCD.DisplayConfigGetDeviceInfo(ref dcTargetDeviceName);

            Win32Utilities.ThrowIfResultCodeNotSuccess(resultCode);

            ConsoleOutputUtilities.WriteDisplayConfigTargetDeviceNameToConsole(dcTargetDeviceName);
            Console.WriteLine();

            CCD.DisplayConfigSourceDeviceName dcSourceDeviceName = new CCD.DisplayConfigSourceDeviceName();
            dcSourceDeviceName.header.type      = CCD.DisplayConfigDeviceInfoType.GetSourceName;
            dcSourceDeviceName.header.size      = (uint)Marshal.SizeOf(dcSourceDeviceName);
            dcSourceDeviceName.header.adapterId = primaryPath.sourceInfo.adapterId;
            dcSourceDeviceName.header.id        = primaryPath.sourceInfo.id;

            resultCode = CCD.DisplayConfigGetDeviceInfo(ref dcSourceDeviceName);

            Win32Utilities.ThrowIfResultCodeNotSuccess(resultCode);

            ConsoleOutputUtilities.WriteDisplayConfigSourceDeviceNameToConsole(dcSourceDeviceName);

            CCD.DisplayConfigAdapterName dcAdapterName = new CCD.DisplayConfigAdapterName();
            dcAdapterName.header.type      = CCD.DisplayConfigDeviceInfoType.GetAdapterName;
            dcAdapterName.header.size      = (uint)Marshal.SizeOf(dcAdapterName);
            dcAdapterName.header.adapterId = primaryPath.sourceInfo.adapterId;

            Win32Utilities.ThrowIfResultCodeNotSuccess(
                CCD.DisplayConfigGetDeviceInfo(ref dcAdapterName));

            ConsoleOutputUtilities.WriteDisplayConfigAdapterNameToConsole(dcAdapterName);

            CCD.DisplayConfigTargetPreferredMode dcTargetPreferredMode = new CCD.DisplayConfigTargetPreferredMode();
            dcTargetPreferredMode.header.type      = CCD.DisplayConfigDeviceInfoType.GetTargetPreferredMode;
            dcTargetPreferredMode.header.size      = (uint)Marshal.SizeOf(dcTargetPreferredMode);
            dcTargetPreferredMode.header.adapterId = primaryPath.targetInfo.adapterId;
            dcTargetPreferredMode.header.id        = primaryPath.targetInfo.id;

            Win32Utilities.ThrowIfResultCodeNotSuccess(CCD.DisplayConfigGetDeviceInfo(ref dcTargetPreferredMode));
        }