Custom OnPaint and disabled control

G

Guest

Please, help.

I created my contol, ButtonX, which subclasses System.Forms.Windows.Button
class. I am doing my own paiting, by overriding OnPaint and
OnPaintBackground (without calling base class's OnPaint &
OnPaintBackground). My button has a shape of rectangle with rounded corners
and is filled with gradient brush, where user specifies the gradient colors.

The dilema I have now is how to paint the button when it's disabled (Enabled
= false). I know how to detect when this happens, but I am not sure how to
give my button the "disabled" look, so it would be somewhat similar to how
native windows controls look.

The idea I had was to BitBlt my button to an image, convert the image to a
gray scale image, and maybe to increase the brightness of RGB in gray scale
(to make it look different if the button is already gray). But it seems like
a hack to me.

Is there a built-in .NET functionality to do something like this? Or maybe a
better way than the one I described above?

There are a lot of other custom controls that I will have to build, so I
guess I am looking for a more or less generic solution.

Thanks for all the help and suggestions.

VR
 
B

Bob Powell [MVP]

In your paint handler you can detect if the component is disabled and drw
the appropriate pixels.

protected override void OnPaint(PaintEventArgs e)
{
if(Enabled)
{
// draw the enabled appearance
}
else
{
// draw the disabled appearance.
}
base OnPaint(e);
}

I would advise calling the OnPaint base class implementation at the end
because this enables the Paint event to fire.

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
G

Guest

Bob,

Thanks for your post. As I said in my post, I do know how to detect whether
the control is enabled or disabled. What I am having problem with, is coming
up with a generic implementation that could generate the "disabled" look of
the control from its enabled look.

Thanks,
VR
 
R

Rhy Mednick

For your enabled state you're creating a non-standard visual. I'm sure that
it's consistent across your controls, but it's hard for me (us) to suggest a
"standard" disabled state when the enabled state isn't standard to start
with. :)

From an implementation perspective I'd create a class that models the
ControlPaint class which contains methods that you use to do generic drawing
of your controls. I can't really say what the "best" way to draw a greyed
version of your control is. If I were to do it myself I would just draw the
control two different ways depending on the state (like Bob suggested). You
could do something like your idea (applying a filter over the control to
remove the color) but my feeling is that a disabled control should be
"lighter" than an enabled control and not take more processor power to
paint. Perhaps you could create an implementiation of ControlPaint that
takes the colors to be painted as a parameter to a method and if the control
is disabled it converts those colors to shades of grey before painting them.

If you're looking for a routine to convert the colors to shades of grey
there's not one in .NET that I'm aware of.

I know that there's really any hard content here in my response, but I hope
it helps.

- Rhy
 
G

Guest

Rhy,

Thanks for your suggestions. It helps a lot, now that I know that I am not
re-inventing the wheel. My worry was that I would come up with this
eloborate way of doing something that's been already done and works much
better...

Thanks again.
VR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top