By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Custom OnPaint and disabled control

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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


<VR@MSDN.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Ok**************@TK2MSFTNGP11.phx.gbl...
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


<VR@MSDN.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #3

P: n/a
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

<VR@MSDN.COM> wrote in message news:O9**************@TK2MSFTNGP11.phx.gbl...
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Ok**************@TK2MSFTNGP11.phx.gbl...
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


<VR@MSDN.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> 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
>
>



Nov 16 '05 #4

P: n/a
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

"Rhy Mednick" <rh*@rhy.com> wrote in message
news:u9**************@TK2MSFTNGP10.phx.gbl...
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

<VR@MSDN.COM> wrote in message

news:O9**************@TK2MSFTNGP11.phx.gbl...
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Ok**************@TK2MSFTNGP11.phx.gbl...
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


<VR@MSDN.COM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> 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
>
>



Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.