473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Custom OnPaint and disabled control

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
4 5639
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
3
by: Vern | last post by:
I have a custom button control that compares the user security level to the buttons security level property. If the user level is greater than the button level, then the button is enabled, else it...
1
by: Brian Henry | last post by:
I am trying to make a custom user control that gets a list of users from our database and populates the list, its an owner drawn control also, the problem is, I placed the item onto a form and...
4
by: RSH | last post by:
How do I go about overriding a Control's OnPaint Method? I would like to prevent a control's color from changing when it is disabled. I have overridden the Form's OnPaint Method but I need to...
1
by: Kenneth Siewers Møller | last post by:
Hi there I have a custom label control (GradientLabel) which enherits from Label and basically just paints the background of a label in a gradient. In my code I have the following for the...
4
by: cashdeskmac | last post by:
I have created a control and added a few properties. One of these is an array of DateTime objects, looking like this: public DateTime MyDates { get { return theDate;} set {theDate =...
4
by: H-S | last post by:
Please help. This is a real puzzler! Originally posted on microsoft.public.dotnet.framework.windowsforms but no answer found! I have a read-only textBox which shows the results of a selection...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
3
by: rarobbi | last post by:
I created a custom label so I can write characters in apex and also in this way: ex.: Ac. Now I have a problem: when my CustomLabel is in a disabled groupBox , the label does not appear...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.