Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Faded/Dimmed Button Images

5.00/5 (1 vote)
15 Aug 2019CPOL 8.2K  
Dim Windows Forms Buttons when disabled

Introduction

I noticed that the images on my buttons aren't faded/dimmed when they're disabled. Text/back color goes darker....but not the image. Of course, I Googled a solution....and it appears that many people have the problem and there were many suggestions....but all far too complex.

Using the Code

The code is very simple.

C#
//Define a general paint event
//This can be in a static class, a form, anywhere.
private void Ctrl_Paint(object sender, PaintEventArgs e)
{
    Control b = (Control)sender;
    //Paint a semi-transparent black rectangle over the button if disabled
    if (!b.Enabled)
    {
        using (SolidBrush brdim = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
        {
            e.Graphics.FillRectangle(brdim, e.ClipRectangle);
        }
    }
}

//Then just tie up the above to your control in a form load event:
ControlName.Paint += Ctrl_Paint;

A really simple solution - just paint a semi-transparent rectangle over the control....the amount of dimming can be altered by adjusting the alpha value (128 in the code above).

Points of Interest

You can have more than one event handler for each event. The above works by executing the Ctrl_Paint event after a control's 'normal' paint event...so it even works on overridden events in classes.

History

  • 15th August, 2019: First version

License

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