question

DiDra-0165 avatar image
0 Votes"
DiDra-0165 asked ·

Managing windows settings in any way using C# code

Is there ant library, method to get/set some of windows setting (like changing monitor brightness, changing power supply mode (from high performance to eco and back))?

dotnet-csharpwindows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

You can use the SetMonitorBrightness API to modify the screen brightness:

    public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
             
         }
         private void button_Click(object sender, RoutedEventArgs e)
         {
             IntPtr windowHandle = new WindowInteropHelper(this).Handle;
             using (BrightnessController brightnessController = new BrightnessController(windowHandle))
             {
                 brightnessController.SetBrightness(100);
             } 
         }
     }
     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
     public struct PHYSICAL_MONITOR
     {
         public IntPtr hPhysicalMonitor;
    
         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
         public string szPhysicalMonitorDescription;
     }
    
     public class BrightnessController : IDisposable
     {
         [DllImport("user32.dll", EntryPoint = "MonitorFromWindow")]
         public static extern IntPtr MonitorFromWindow([In] IntPtr hwnd, uint dwFlags);
    
         [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR[] pPhysicalMonitorArray);
    
         [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
    
         [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
    
         [DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);
    
         [DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);
    
         private uint _physicalMonitorsCount = 0;
         private PHYSICAL_MONITOR[] _physicalMonitorArray;
    
         private IntPtr _firstMonitorHandle;
    
         private uint _minValue = 0;
         private uint _maxValue = 0;
         private uint _currentValue = 0;
    
         public BrightnessController(IntPtr windowHandle)
         {
             uint dwFlags = 0u;
             IntPtr ptr = MonitorFromWindow(windowHandle, dwFlags);
             if (!GetNumberOfPhysicalMonitorsFromHMONITOR(ptr, ref _physicalMonitorsCount))
             {
                 throw new Exception("Cannot get monitor count!");
             }
             _physicalMonitorArray = new PHYSICAL_MONITOR[_physicalMonitorsCount];
    
             if (!GetPhysicalMonitorsFromHMONITOR(ptr, _physicalMonitorsCount, _physicalMonitorArray))
             {
                 throw new Exception("Cannot get phisical monitor handle!");
             }
             _firstMonitorHandle = _physicalMonitorArray[0].hPhysicalMonitor;
    
             if (!GetMonitorBrightness(_firstMonitorHandle, ref _minValue, ref _currentValue, ref _maxValue))
             {
                 throw new Exception("Cannot get monitor brightness!");
             }
         }
    
         public void SetBrightness(int newValue) // 0 ~ 100
         {
             newValue = Math.Min(newValue, Math.Max(0, newValue));
             _currentValue = (_maxValue - _minValue) * (uint)newValue / 100u + _minValue;
             SetMonitorBrightness(_firstMonitorHandle, _currentValue);
         }
    
         public void Dispose()
         {
             Dispose(true);
             GC.SuppressFinalize(this);
         }
    
         protected virtual void Dispose(bool disposing)
         {
             if (disposing)
             {
                 if (_physicalMonitorsCount > 0)
                 {
                     DestroyPhysicalMonitors(_physicalMonitorsCount, ref _physicalMonitorArray);
                 }
             }
         }
     }

Most of this code is quoted from this post: Adjust screen brightness using C#

Windows provides a tool to modify the power plan, we can use Process to call this tool:

 Process.Start("powercfg", "-setactive  GUID");

GUID example:

Balanced: 381b4222-f694-41f0-9685-ff5bb260df2e
High performance: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
Power saver: a1841308-3541-4fab-bc81-f71556f20b4a

You can also use this method to get more power options. If you want to learn more, you can open Command Prompt and type powercfg -aliases to get more available GUID values.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.