When to redraw control?

  • Thread starter Thread starter CroDude
  • Start date Start date
C

CroDude

Hi all!

I've made a simple control derived from a panel class which draws gradient
in a overrided OnPaintBackground event.
Well, I know that I need to redraw control when it's resized by overriding
OnResize event, but
how would I know when to redraw if, let's say, user drags some form over it
or some other action that messes display of this control.

I'm stuck here ... what to do?

Thanks in advance! Take care!

D.
 
Thanks for help, but unfortunately that didn't do it.
Every time when I drag some other form above that panel or if I hide child
control there are glitches in the drawing of the panel control.

Here is my drawing code:

protected override void OnPaint(PaintEventArgs e)
{
// If drawOutline is true, draw outline around toolbar
if(this.drawOutline)
{
// Set rectangle for drawing
Rectangle drawRectangle = e.ClipRectangle;
// Create pen with outlineColor applied
Pen outlinePen = new Pen(this.outlineColor, 2);
// Draw outline
e.Graphics.DrawRectangle(outlinePen, drawRectangle.X ,
drawRectangle.Y , drawRectangle.Width , drawRectangle.Height );
}
else
{
base.OnPaint(e);
}
}

and:

protected override void OnPaintBackground(PaintEventArgs e)
{
if(this.drawGradientBackground)
{
// Set gradient brush
LinearGradientBrush brush = new LinearGradientBrush(e.ClipRectangle,
this.gradientColorA, this.gradientColorB, this.gradientMode);
// Draw the gradient background.
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
else
{
base.OnPaintBackground (e);
}
}
 
Don't use e.ClipRectangle, use this.ClientRectangle instead. ClipRectangle
will return the exact area to paint. In other words, it will return only the
portion of the control that is invalidated and needs to be redrawn. However,
you were painting the entire control into this invalidated region. See the
modified code below.

protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if(this.drawGradientBackground)
{
// Set gradient brush.
LinearGradientBrush brush = new
LinearGradientBrush(this.ClientRectangle, this.gradientColorA,
this.gradientColorB, this.gradientMode);
// Draw the gradient background.
e.Graphics.FillRectangle(brush, this.ClientRectangle);
// Dispose.
brush.Dispose();
}
}

protected override void OnPaint(PaintEventArgs e)
{
// If drawOutline is true, draw outline around toolbar.
if(this.drawOutline)
{
// Create pen with outlineColor applied.
Pen outlinePen = new Pen(this.outlineColor, 2);
// Draw outline.
e.Graphics.DrawRectangle(outlinePen, this.ClientRectangle);
// Dispose.
outlinePen.Dispose();
}
// Fires the Paint event.
base.OnPaint(e);
}
 
Back
Top