Click here to Skip to main content
15,867,453 members
Articles / Web Development / HTML
Tip/Trick

How To Change Screen Brightness in C#

Rate me:
Please Sign up or sign in to vote.
3.33/5 (12 votes)
26 Jan 2017CPOL2 min read 44.5K   2.1K   19   10
This is a simple program that demonstrates a method used to adjust the screen brightness on any C# application.

Introduction

With this tool, you can easily adjust the screen brightness of your C# application. This method is supported by all monitors and graphics cards.

Background

While creating a C# application, I wanted to adjust the brightness of the screen. While searching the web, I found two solutions to overcome this problem. One solution involved the use of WMIBrightness class. When I attempted to use it, I was presented with an error saying my computer doesn't support it. Upon reading into the solution, there were many warnings saying that the WMIBrightness does not work on every computer. The next solution used the SetDeviceGammaRamp function. This solution does seem to have more compatibility. The method that I have used will work on any monitor and is simple to apply as demonstrated.

Using the Code

This method requires the use of two forms. One form contains the controls and functionality of the program. The other form is set to the screen size using the windows state maximized and is set to be the top most in the designer. The opacity of the form is then set to 0.5 to make the form transparent.

C#
this.TopMost = true;
this.Opacity = 0.5D;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

The code for the top Brightness form uses the WndProc override so that all the WM_NCHITTEST messages it receives are passed through the transparent form to the form underneath.

C#
protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0084) // WM_NCHITTEST
        m.Result = (IntPtr)(-1); // HTTRANSPARENT
    else
        base.WndProc(ref m);
}

In the form which contains all the controls and functionality, a function has been added to adjust and update the brightness as shown below. This is run on the form load and when the slider is adjusted.

C#
void UpdateBrightness()
{
    float f = trackBarBrightness.Value * 0.01f;
    if (f < 0.5f)
    {
        program.screenForm.Opacity = 1 - 2 * f;
        program.screenForm.BackColor = Color.Black;
    }
    else
    {
        program.screenForm.Opacity = 2 * (f - 0.5f);
        program.screenForm.BackColor = Color.White;
    }
}

When a form is closed, the main program must be closed using the exit thread, otherwise the brightness will still be applied and you will not be able to close the application.

C#
private void ControlForm_FormClosed_1(object sender, FormClosedEventArgs e)
{
    program.ExitThread();
} 

The important aspects of the code have been outlined in this tip. If any more explanation is needed, please leave a comment.

Points of Interest

I learned how to pass the click message from a transparent form to the one underneath using WndProc.

History

  • 20th December, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
I am a computing (embedded systems) graduate from Edge Hill who loves to learn.

Comments and Discussions

 
AnswerHere is the code that actually changes brightness (only for laptop monitor though) Pin
_Maxim_25-Apr-20 0:26
professional_Maxim_25-Apr-20 0:26 
QuestionDoesn't work with more then one monitor Pin
Андрей Корниенко22-Aug-19 4:15
Андрей Корниенко22-Aug-19 4:15 
SuggestionMisleading title PinPopular
dandy7226-Jan-17 7:20
dandy7226-Jan-17 7:20 
GeneralRe: Misleading title Pin
Kunal Chowdhury «IN»26-Jan-17 23:34
professionalKunal Chowdhury «IN»26-Jan-17 23:34 
I do agree with @dandy72.
Vote up or Mark as Answered, if this information helped you.
Kind Regards - Kunal Chowdhury, Windows Development MVP | Windows 10 Champion
Technical blog: http://www.kunal-chowdhury.com

GeneralRe: Misleading title Pin
dandy7227-Jan-17 2:26
dandy7227-Jan-17 2:26 
GeneralRe: Misleading title Pin
Bruce Greene27-Jan-17 11:43
Bruce Greene27-Jan-17 11:43 
GeneralRe: Misleading title Pin
dandy7227-Jan-17 13:07
dandy7227-Jan-17 13:07 
GeneralRe: Misleading title Pin
Kunal Chowdhury «IN»30-Jan-17 2:10
professionalKunal Chowdhury «IN»30-Jan-17 2:10 
QuestionDnld? Pin
Tomee11-Aug-15 8:53
Tomee11-Aug-15 8:53 
AnswerRe: Dnld? Pin
John Melling26-Jan-17 3:22
John Melling26-Jan-17 3:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.