Click here to Skip to main content
13,557,915 members
Click here to Skip to main content
Add your own
alternative version

Stats

20.9K views
860 downloads
19 bookmarked
Posted 20 Dec 2014
Licenced CPOL

How To Change Screen Brightness in C#

, 26 Jan 2017
Rate this:
Please Sign up or sign in to vote.
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.

You may also be interested in...

Pro

Comments and Discussions

 
SuggestionMisleading title Pin
dandy7226-Jan-17 7:20
memberdandy7226-Jan-17 7:20 
GeneralRe: Misleading title Pin
Kunal Chowdhury « IN »26-Jan-17 23:34
mentorKunal Chowdhury « IN »26-Jan-17 23:34 
GeneralRe: Misleading title Pin
dandy7227-Jan-17 2:26
memberdandy7227-Jan-17 2:26 
GeneralRe: Misleading title Pin
Bruce Greene27-Jan-17 11:43
memberBruce Greene27-Jan-17 11:43 
GeneralRe: Misleading title Pin
dandy7227-Jan-17 13:07
memberdandy7227-Jan-17 13:07 
GeneralRe: Misleading title Pin
Kunal Chowdhury «IN»30-Jan-17 2:10
mentorKunal Chowdhury «IN»30-Jan-17 2:10 
QuestionDnld? Pin
Tomee11-Aug-15 8:53
memberTomee11-Aug-15 8:53 
AnswerRe: Dnld? Pin
John Melling26-Jan-17 3:22
memberJohn 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 26 Jan 2017
Article Copyright 2014 by John Melling
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid