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.
private void Ctrl_Paint(object sender, PaintEventArgs e)
{
Control b = (Control)sender;
if (!b.Enabled)
{
using (SolidBrush brdim = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
{
e.Graphics.FillRectangle(brdim, e.ClipRectangle);
}
}
}
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