Contrôle de la luminosité de l'écran

Description

Suite au message posté au http://www.csharpfr.com/forum.v2.aspx?ID=552660
J'ai décidé de faire une enquête afin de trouver le moyen de contrôler la luminosité de l'écran.
Après documentation, j'ai fini par réaliser ce petit programme.

Source / Exemple :


using System;
using System.Runtime.InteropServices;

namespace ScreenBrightness
{

	public class screenBrightness
	{
		//on importe les fonctions necessaires
		[DllImport("gdi32.dll",CharSet = CharSet.Auto)]
			public static extern bool SetDeviceGammaRamp(IntPtr hDc,
			[MarshalAs(UnmanagedType.LPArray)] ushort[,] lpRamp);
		[DllImport("user32.dll",CharSet = CharSet.Auto)]
			public static extern IntPtr GetDC(IntPtr hWnd);
		
		IntPtr screenDC;

		public screenBrightness(IntPtr hDC)
		{
			screenDC = hDC;
		}

		public bool setBrightness(int b)
		{
			IntPtr gammaDC;
			if(screenDC==IntPtr.Zero)
			{
				gammaDC = GetDC(IntPtr.Zero);
			} else gammaDC = screenDC;

			if(gammaDC == IntPtr.Zero) return false;

			ushort[,] gammaArray = new ushort[3,256];

			for (int i = 0; i < 256; i++)
			{
				int arrayValue = i * (b + 128);

				if (arrayValue > 65535)
					arrayValue = 65535;

				gammaArray[0, i] = 
					gammaArray[1, i] = 
					gammaArray[2, i] = (ushort)arrayValue;
			
			}

			return SetDeviceGammaRamp(gammaDC, gammaArray);
		}
	}
}

Conclusion :


Je tiens a dire merci aux auteurs de cet page dont je me suis beaucoup inspiré:
http://www.nirsoft.net/vc/change_screen_brightness.html

Codes Sources

A voir également