Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MonitorHelper {
- #region consts
- const UInt32 SDC_TOPOLOGY_INTERNAL = 0x00000001;
- const UInt32 SDC_TOPOLOGY_CLONE = 0x00000002;
- const UInt32 SDC_TOPOLOGY_EXTEND = 0x00000004;
- const UInt32 SDC_TOPOLOGY_EXTERNAL = 0x00000008;
- const UInt32 SDC_USE_SUPPLIED_DISPLAY_CONFIG = 0x00000020;
- const UInt32 SDC_VALIDATE = 0x00000040;
- const UInt32 SDC_APPLY = 0x00000080;
- const UInt32 SDC_NO_OPTIMIZATION = 0x00000100;
- const UInt32 SDC_SAVE_TO_DATABASE = 0x00000200;
- const UInt32 SDC_ALLOW_CHANGES = 0x00000400;
- const UInt32 SDC_PATH_PERSIST_IF_REQUIRED = 0x00000800;
- const UInt32 SDC_FORCE_MODE_ENUMERATION = 0x00001000;
- const UInt32 SDC_ALLOW_PATH_ORDER_CHANGES = 0x00002000;
- const UInt32 QDC_ALL_PATHS = 0x00000001;
- const UInt32 QDC_ONLY_ACTIVE_PATHS = 0x00000002;
- const UInt32 QDC_DATABASE_CURRENT = 0x00000004;
- #endregion
- [Serializable]
- public class TVSettings {
- public DISPLAYCONFIG_PATH_INFO[] Path;
- public DISPLAYCONFIG_MODE_INFO[] Mode;
- }
- public static TVSettings Enabled;
- public static TVSettings Disabled;
- [DllImport("User32.dll")]
- public static extern int GetDisplayConfigBufferSizes(uint flags, ref uint numPathArrayElements, ref uint numModeInfoArrayElements);
- [DllImport("User32.dll")]
- public static extern int QueryDisplayConfig(
- uint flags,
- ref uint numPathArray, [Out] DISPLAYCONFIG_PATH_INFO[] pathArray,
- ref uint numModeArray, [Out] DISPLAYCONFIG_MODE_INFO[] modeArray,
- IntPtr currentTopologyId
- );
- [DllImport("User32.dll")]
- public static extern int SetDisplayConfig(
- uint numPathArray, [In] DISPLAYCONFIG_PATH_INFO[] pathArray,
- uint numModeArray, [In] DISPLAYCONFIG_MODE_INFO[] modeArray,
- uint flags
- );
- public static void ChangeTVState(bool Update = false, bool Save = false) {
- uint numPathArrayElements = 0;
- uint numModeInfoArrayElements = 0;
- uint id = QDC_ALL_PATHS;
- int bufferError = GetDisplayConfigBufferSizes(id, ref numPathArrayElements, ref numModeInfoArrayElements);
- DISPLAYCONFIG_PATH_INFO[] pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
- DISPLAYCONFIG_MODE_INFO[] modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
- QueryDisplayConfig(id, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, IntPtr.Zero);
- var active_modeArray = modeArray.Where(x => x.targetMode.targetVideoSignalInfo.activeSize.cx != 0).ToArray();
- var active_pathArray = pathArray.Where(x => x.flags != 0).ToArray();
- bool ThirdScreenIsConnected = active_pathArray.Length >= 3 && active_modeArray.Length >= 3;
- if (Save) {
- if (ThirdScreenIsConnected) {
- Disabled = new TVSettings() {
- Path = pathArray,
- Mode = modeArray
- };
- }
- else {
- Enabled = new TVSettings() {
- Path = pathArray,
- Mode = modeArray
- };
- }
- }
- if (Update) {
- if (Enabled == null || Disabled == null) {
- Console.WriteLine("Enabled & Disabled Settings are not configured properly.");
- Console.WriteLine("Please save both and try again.");
- return;
- }
- var Settings = ThirdScreenIsConnected ? Enabled : Disabled;
- pathArray = Settings.Path;
- modeArray = Settings.Mode;
- int f = SetDisplayConfig((uint)pathArray.Length, pathArray, (uint)modeArray.Length, modeArray, (SDC_APPLY | SDC_SAVE_TO_DATABASE | SDC_ALLOW_CHANGES | SDC_USE_SUPPLIED_DISPLAY_CONFIG));
- if (f == 0) { Console.WriteLine("Successfully updated Screen setup!"); }
- else { Console.WriteLine("SetDisplayConfig ERROR: " + f); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement