What control is being painted?

B

Blair Bonnett

Hi all,

Run into a problem I can't find a solution for. I'm writing a C# app
which has a custom background for the form (a gradient). I've
successfully overridden the forms OnPaintBackground(PaintEventArgs
pevent) method to paint the background.

However, it seems that some of the controls on the form (such as the
labels) also call this method and hence get painted with the gradient.
Is there some way I can tell what control (or type of control) is being
painted, so only the form itself gets the background?

Pseudo-code for what I mean:
if(ControlBeingPainted == Form){
paint the gradient background
}
else{
base.OnPaintBackground(pevent);
}

Thanks for any help,
Blair
 
S

Sean Hederman

Blair Bonnett said:
Hi all,

Run into a problem I can't find a solution for. I'm writing a C# app which
has a custom background for the form (a gradient). I've successfully
overridden the forms OnPaintBackground(PaintEventArgs pevent) method to
paint the background.

However, it seems that some of the controls on the form (such as the
labels) also call this method and hence get painted with the gradient. Is
there some way I can tell what control (or type of control) is being
painted, so only the form itself gets the background?

This is by design. The assumption with many controls is that you'd want them
to have the same background as the form. The simplest way to get around this
would be to first set the forms background color to something else (say,
White). You'll see that these controls will automatically "inherit" this
background color, Now change these controls background color explicitly to
the background you want them to have (say, Control). This will stop them
from inheriting the form background.

The other way is to move your background painting code into OnPaint, and
call SetStyle(ControlStyles.AllPaintingInWmPaint | SetStyles.UserPaint |
SetSyles.DoubleBuffer) on your form sometime after it's handle is created. I
call this in OnHandleCreated:
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);

if(IsHandleCreated) {
SetStyle(ControlStyles.AllPaintingInWmPaint | SetStyles.UserPaint |
SetSyles.DoubleBuffer);
}
}

However, this might be overkill for your purposes.
 
M

Mick Doherty

Only controls with their BackColor set to Transparent will paint themselves
with the custom background. Other controls will paint themselves with the
color set in their BackColor property, or if not explicitly set, with the
color set in their parents BackColor property.

If this is not the behaviour you are seeing, then you have encountered a bug
or, more likely, have missed something in your code or control property
settings, e.g. you have placed labels on a Transparent Panel.
 
B

Blair Bonnett

Thanks for pointing that out. I was under the impression that
Transparent meant they would not paint a background colour, but just
write the text (or whatever) on top of what was already there. At least
this behavior makes sense now...
 

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