Click here to Skip to main content
14,773,906 members
Articles » Languages » C# » How To
Tip/Trick
Posted 20 Dec 2014

Stats

33.3K views
1.7K downloads
19 bookmarked

How To Change Screen Brightness in C#

Rate me:
Please Sign up or sign in to vote.
3.33/5 (12 votes)
26 Jan 2017CPOL
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.

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.

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.

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.

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)

Share

About the Author

John Melling
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 1:26
professional_Maxim_25-Apr-20 1:26 
QuestionDoesn't work with more then one monitor Pin
Андрей Корниенко22-Aug-19 5:15
MemberАндрей Корниенко22-Aug-19 5:15 
SuggestionMisleading title PinPopular
dandy7226-Jan-17 8:20
Memberdandy7226-Jan-17 8:20 
GeneralRe: Misleading title Pin
Kunal Chowdhury «IN»27-Jan-17 0:34
professionalKunal Chowdhury «IN»27-Jan-17 0:34 
GeneralRe: Misleading title Pin
dandy7227-Jan-17 3:26
Memberdandy7227-Jan-17 3:26 
GeneralRe: Misleading title Pin
Bruce Greene27-Jan-17 12:43
MemberBruce Greene27-Jan-17 12:43 
GeneralRe: Misleading title Pin
dandy7227-Jan-17 14:07
Memberdandy7227-Jan-17 14:07 
GeneralRe: Misleading title Pin
Kunal Chowdhury «IN»30-Jan-17 3:10
professionalKunal Chowdhury «IN»30-Jan-17 3:10 
QuestionDnld? Pin
Tomee11-Aug-15 9:53
MemberTomee11-Aug-15 9:53 
AnswerRe: Dnld? Pin
John Melling26-Jan-17 4:22
MemberJohn Melling26-Jan-17 4: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.