Emulating Disabled Colors

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
This post is not quite like the usual "I need to changed my textbox's disabled color" posts - Im having trouble going the other way.

I want to simulate a disabled control in one of my overridden paint methods - is there a method/formula for calculating the forecolor of a disabled control based on it's backcolor?

Eg, a LinkLabel with a Cornsilk backcolor, looks like this when Enabled = False:

linklabel.gif


You can see the ForeColor of the text has been automagically set to a shade based on the BackColor.

Any ideas?

Thanks
mafro
 
I havent looked into how it works specifically but i would guess that it translates the backcolor using a fairly simple function. You would just pass in the backcolor and do something like this...

VB.NET:
Expand Collapse Copy
Function GetDisabledTextColor(byval BackColor as color) as color
 
dim R as integer = CInt(backcolor.r)
dim G as byte = Cint(backcolor.g)
dim b as byte  = Cint(backcolor.b)
 
Dim median as integer = 127
 
if r > median then 
R+=20
if r >= 255 then
r = 255
end if
else
R-=20
if r <= 0 then 
r = 0
end if
end if
 
If G > median then 
G+= 20
if g>= 255 then 
g = 255
end if
else
G-=20
if g < = 0 then 
g = 0
end if
end if
 
If B > median then
B+=20
if b >= 255 then 
b = 255
end if
else
g-=20
if g<= 0 then
g = 0
end if
end if
 
return color.fromargb(CByte(r),CByte(g),Cbyte(b))
 
end function

Apolgoies if the code is a bit of but i just made that up. I think it stands a chance of working. Dunno, see how it goes.
 
Thanks vis, that's basically the conclusion I had come to - I was wondering if anyone actually knew where in the framework this conversion is made, and then I could perhaps steal the formula using Reflector..

For now ill suffice with a dummy version which works like you describe. Shame its not available to the normal user!

mafro
 
I would hazard a guess and say that it is done in the controlpaint class using one of the following..

VB.NET:
Expand Collapse Copy
ControlPaint.Dark
ControlPaint.DarkDark
ControlPaint.Light
ControlPaint.LightLight
 
Thanks vis, with that pointer I got the solution - you can emulate a disabled text string with this code (comes from part of an overridden PaintText method in a control):

VB.NET:
Expand Collapse Copy
If disabled = True Then
    Dim c As Color = ControlPaint.Light(DirectCast(backBrush, SolidBrush).Color)
    ControlPaint.DrawStringDisabled(g, text, f, c, rDraw, format)
Else
    g.DrawString(text, f, foreBrush, rDraw, format)
End If
By using ControlPaint.Light() on my backcolor, I get the correct forecolor, and then ControlPaint.DrawStringDisabled() does the greyed dropshadow effect.

mafro
 
Back
Top