Advertisement
Guest User

MonitorHelper

a guest
Jul 18th, 2019
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. public class MonitorHelper {
  2.  
  3.     #region consts
  4.         const UInt32 SDC_TOPOLOGY_INTERNAL = 0x00000001;
  5.         const UInt32 SDC_TOPOLOGY_CLONE = 0x00000002;
  6.         const UInt32 SDC_TOPOLOGY_EXTEND = 0x00000004;
  7.         const UInt32 SDC_TOPOLOGY_EXTERNAL = 0x00000008;
  8.  
  9.         const UInt32 SDC_USE_SUPPLIED_DISPLAY_CONFIG = 0x00000020;
  10.         const UInt32 SDC_VALIDATE = 0x00000040;
  11.         const UInt32 SDC_APPLY = 0x00000080;
  12.         const UInt32 SDC_NO_OPTIMIZATION = 0x00000100;
  13.         const UInt32 SDC_SAVE_TO_DATABASE = 0x00000200;
  14.         const UInt32 SDC_ALLOW_CHANGES = 0x00000400;
  15.         const UInt32 SDC_PATH_PERSIST_IF_REQUIRED = 0x00000800;
  16.         const UInt32 SDC_FORCE_MODE_ENUMERATION = 0x00001000;
  17.         const UInt32 SDC_ALLOW_PATH_ORDER_CHANGES = 0x00002000;
  18.  
  19.         const UInt32 QDC_ALL_PATHS = 0x00000001;
  20.         const UInt32 QDC_ONLY_ACTIVE_PATHS = 0x00000002;
  21.         const UInt32 QDC_DATABASE_CURRENT = 0x00000004;
  22.     #endregion
  23.  
  24.     [Serializable]
  25.     public class TVSettings {
  26.         public DISPLAYCONFIG_PATH_INFO[] Path;
  27.         public DISPLAYCONFIG_MODE_INFO[] Mode;
  28.     }
  29.  
  30.     public static TVSettings Enabled;
  31.     public static TVSettings Disabled;
  32.  
  33.     [DllImport("User32.dll")]
  34.     public static extern int GetDisplayConfigBufferSizes(uint flags, ref uint numPathArrayElements, ref uint numModeInfoArrayElements);
  35.     [DllImport("User32.dll")]
  36.     public static extern int QueryDisplayConfig(
  37.         uint flags,
  38.         ref uint numPathArray, [Out] DISPLAYCONFIG_PATH_INFO[] pathArray,
  39.         ref uint numModeArray, [Out] DISPLAYCONFIG_MODE_INFO[] modeArray,
  40.         IntPtr currentTopologyId
  41.     );
  42.     [DllImport("User32.dll")]
  43.     public static extern int SetDisplayConfig(
  44.         uint numPathArray,  [In] DISPLAYCONFIG_PATH_INFO[] pathArray,
  45.         uint numModeArray,  [In] DISPLAYCONFIG_MODE_INFO[] modeArray,
  46.         uint flags
  47.     );
  48.  
  49.     public static void ChangeTVState(bool Update = false, bool Save = false) {
  50.         uint numPathArrayElements = 0;
  51.         uint numModeInfoArrayElements = 0;
  52.         uint id = QDC_ALL_PATHS;
  53.  
  54.         int bufferError = GetDisplayConfigBufferSizes(id, ref numPathArrayElements, ref numModeInfoArrayElements);
  55.         DISPLAYCONFIG_PATH_INFO[] pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
  56.         DISPLAYCONFIG_MODE_INFO[] modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
  57.  
  58.         QueryDisplayConfig(id, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, IntPtr.Zero);
  59.  
  60.         var active_modeArray = modeArray.Where(x => x.targetMode.targetVideoSignalInfo.activeSize.cx != 0).ToArray();
  61.         var active_pathArray = pathArray.Where(x => x.flags != 0).ToArray();
  62.  
  63.         bool ThirdScreenIsConnected = active_pathArray.Length >= 3 && active_modeArray.Length >= 3;
  64.  
  65.         if (Save) {
  66.             if (ThirdScreenIsConnected) {
  67.                 Disabled = new TVSettings() {
  68.                     Path = pathArray,
  69.                     Mode = modeArray
  70.                 };
  71.             }
  72.             else {
  73.                 Enabled = new TVSettings() {
  74.                     Path = pathArray,
  75.                     Mode = modeArray
  76.                 };
  77.             }
  78.         }
  79.  
  80.         if (Update) {
  81.             if (Enabled == null || Disabled == null) {
  82.                 Console.WriteLine("Enabled & Disabled Settings are not configured properly.");
  83.                 Console.WriteLine("Please save both and try again.");
  84.                 return;
  85.             }
  86.  
  87.             var Settings = ThirdScreenIsConnected ? Enabled : Disabled;
  88.             pathArray = Settings.Path;
  89.             modeArray = Settings.Mode;
  90.  
  91.             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));
  92.  
  93.             if (f == 0) { Console.WriteLine("Successfully updated Screen setup!"); }
  94.             else { Console.WriteLine("SetDisplayConfig ERROR: " + f); }
  95.         }
  96.  
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement