Advertisement
Guest User

Untitled

a guest
May 19th, 2017
3,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4.  
  5. /*
  6.     Initialize a field/property of the type BrightnessController in a form or wherever you see fit
  7.     BrightnessController brightnessController = new BrightnessController();
  8.  
  9.     // Change the brightness with this method with a monitor index and brightness value 0-100
  10.     BrightnessController.MonitorArray[MONITOR_INDEX].SetBrightness(BRIGHTNESS);
  11.  
  12.     This was made in a very short time and not meant to be impressive or anything like that, i just needed a quick fix to dim my monitors
  13.     when I'm up late nights and made this for my own use, but if anyone is interested here it is.
  14. */
  15.  
  16. namespace MonitorDimmer
  17. {
  18.     /// <summary>
  19.     /// Rectangle
  20.     /// </summary>
  21.     [StructLayout(LayoutKind.Sequential)]
  22.     public struct RECT
  23.     {
  24.         public int left;
  25.         public int top;
  26.         public int right;
  27.         public int bottom;
  28.     }
  29.  
  30.     /// <summary>
  31.     /// Monitor information.
  32.     /// </summary>
  33.     [StructLayout(LayoutKind.Sequential)]
  34.     public struct MONITORINFO
  35.     {
  36.         public uint size;
  37.         public RECT monitor;
  38.         public RECT work;
  39.         public uint flags;
  40.     }
  41.  
  42.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  43.     public struct PHYSICAL_MONITOR
  44.     {
  45.         public IntPtr hPhysicalMonitor;
  46.  
  47.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  48.         public string szPhysicalMonitorDescription;
  49.     }
  50.  
  51.     public struct BrightnessValues
  52.     {
  53.         public uint Minimum;
  54.         public uint Current;
  55.         public uint Maximum;
  56.     }
  57.  
  58.     /// <summary>
  59.     /// Monitor information with handle.
  60.     /// </summary>
  61.     public class Monitor
  62.     {
  63.         /// <summary>
  64.         /// Gets the monitor handle.
  65.         /// </summary>
  66.         /// <value>
  67.         /// The monitor handle.
  68.         /// </value>
  69.         public IntPtr MonitorHandle { get; private set; }
  70.  
  71.         public uint PhysicalMonitorCount { get; private set; } = 1;
  72.  
  73.         public PHYSICAL_MONITOR[] PhysicalMonitorArray;
  74.  
  75.         public BrightnessValues[] BrightnessValues { get; private set; }
  76.  
  77.         #region Native Methods
  78.  
  79.         [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
  80.         public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
  81.  
  82.         [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
  83.         public static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
  84.  
  85.         [DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
  86.         public static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);
  87.  
  88.         [DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
  89.         public static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);
  90.  
  91.         #endregion
  92.  
  93.         public Monitor(IntPtr monitorHandle)
  94.         {
  95.             this.MonitorHandle = monitorHandle;
  96.  
  97.             //if (!GetNumberOfPhysicalMonitorsFromHMONITOR(this.MonitorHandle, ref this.PhysicalMonitorCount))
  98.             //{
  99.             //    throw new Exception("Cannot get monitor count!");
  100.             //}
  101.             //this.PhysicalMonitorArray = new PHYSICAL_MONITOR[this.PhysicalMonitorCount];
  102.  
  103.             // Todo: Implement multi-physical display
  104.  
  105.             // This array will hold the IntPtr(s) we need to change brightness
  106.             this.PhysicalMonitorArray = new PHYSICAL_MONITOR[this.PhysicalMonitorCount];
  107.             this.BrightnessValues = new BrightnessValues[this.PhysicalMonitorCount];
  108.  
  109.             // Validate all physical monitors
  110.             if (!GetPhysicalMonitorsFromHMONITOR(this.MonitorHandle, this.PhysicalMonitorCount, this.PhysicalMonitorArray))
  111.             {
  112.                 throw new Exception("Cannot get physical monitor handle!");
  113.             }
  114.  
  115.             // Get brightness values
  116.             for (int i = 0; i < this.PhysicalMonitorCount; i++)
  117.             {
  118.                 BrightnessValues brightness = new BrightnessValues();
  119.  
  120.                 GetMonitorBrightness(
  121.                     this.PhysicalMonitorArray[i].hPhysicalMonitor,
  122.                     ref brightness.Minimum,
  123.                     ref brightness.Current,
  124.                     ref brightness.Maximum);
  125.  
  126.                 this.BrightnessValues[i] = brightness;
  127.  
  128.                 Console.WriteLine(
  129.                     string.Format(
  130.                         "hwId: {0}, Min: {1}, Max: {2}, Current: {3}",
  131.                         this.PhysicalMonitorArray[i].hPhysicalMonitor,
  132.                         brightness.Minimum,
  133.                         brightness.Maximum,
  134.                         brightness.Current));
  135.             }
  136.         }
  137.  
  138.         public void SetBrightness(int newValue)
  139.         {
  140.             newValue = Math.Min(newValue, Math.Max(0, newValue));
  141.  
  142.             for (int i = 0; i < this.PhysicalMonitorCount; i++)
  143.             {
  144.                 var _maxValue = this.BrightnessValues[i].Maximum;
  145.                 var _minValue = this.BrightnessValues[i].Minimum;
  146.                 this.BrightnessValues[i].Current = (_maxValue - _minValue) * (uint)newValue / 100u + _minValue;
  147.  
  148.                 SetMonitorBrightness(this.PhysicalMonitorArray[i].hPhysicalMonitor, this.BrightnessValues[i].Current);
  149.             }
  150.         }
  151.     }
  152.  
  153.     public class BrightnessController : IDisposable
  154.     {
  155.         public static List<Monitor> MonitorArray { get; set; }
  156.  
  157.         public BrightnessController()
  158.         {
  159.             var monitors = GetMonitors();
  160.         }
  161.  
  162.         public static Monitor[] GetMonitors()
  163.         {
  164.             // New List
  165.             MonitorArray = new List<Monitor>();
  166.  
  167.             // Enumerate monitors
  168.             EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnum, IntPtr.Zero);
  169.  
  170.             // Return list
  171.             return MonitorArray.ToArray();
  172.         }
  173.  
  174.         #region Native Methods
  175.  
  176.         /// <summary>
  177.         /// Monitor Enum Delegate
  178.         /// </summary>
  179.         /// <param name="hMonitor">A handle to the display monitor.</param>
  180.         /// <param name="hdcMonitor">A handle to a device context.</param>
  181.         /// <param name="lprcMonitor">A pointer to a RECT structure.</param>
  182.         /// <param name="dwData">Application-defined data that EnumDisplayMonitors passes directly to the enumeration function.</param>
  183.         /// <returns></returns>
  184.         public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
  185.  
  186.         /// <summary>
  187.         /// Enumerates through the display monitors.
  188.         /// </summary>
  189.         /// <param name="hdc">A handle to a display device context that defines the visible region of interest.</param>
  190.         /// <param name="lprcClip">A pointer to a RECT structure that specifies a clipping rectangle.</param>
  191.         /// <param name="lpfnEnum">A pointer to a MonitorEnumProc application-defined callback function.</param>
  192.         /// <param name="dwData">Application-defined data that EnumDisplayMonitors passes directly to the MonitorEnumProc function.</param>
  193.         /// <returns></returns>
  194.         [DllImport("user32.dll")]
  195.         public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
  196.  
  197.         /// <summary>
  198.         /// Monitor Enum Delegate
  199.         /// </summary>
  200.         /// <param name="hMonitor">A handle to the display monitor.</param>
  201.         /// <param name="hdcMonitor">A handle to a device context.</param>
  202.         /// <param name="lprcMonitor">A pointer to a RECT structure.</param>
  203.         /// <param name="dwData">Application-defined data that EnumDisplayMonitors passes directly to the enumeration function.</param>
  204.         /// <returns></returns>
  205.         public static bool MonitorEnum(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
  206.         {
  207.             MonitorArray.Add(new Monitor(hMonitor));
  208.  
  209.             return true;
  210.         }
  211.  
  212.         [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
  213.         public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR[] pPhysicalMonitorArray);
  214.  
  215.         #endregion
  216.  
  217.         public void Dispose()
  218.         {
  219.             this.Dispose(true);
  220.             GC.SuppressFinalize(this);
  221.         }
  222.  
  223.         protected virtual void Dispose(bool disposing)
  224.         {
  225.             if (disposing)
  226.             {
  227.                 if (MonitorArray.Count > 0)
  228.                 {
  229.                     foreach (var monitor in MonitorArray)
  230.                     {
  231.                         DestroyPhysicalMonitors(monitor.PhysicalMonitorCount, ref monitor.PhysicalMonitorArray);
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement