Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How To Change Screen Brightness in C#

0.00/5 (No votes)
26 Jan 2017 2  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here