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

When to redraw control?

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


P: n/a
The OnPaintBackground and OnPaint overrides should be called automatically
when a control needs to be repainted after it has been obscured. Just as an
option, you may also use the protected SetStyle() method and the
ControlStyles.ResizeRedraw enum value to have your control repainted when it
is resized instead of using the OnResize override.
http://msdn.microsoft.com/library/de...styletopic.asp

--
Tim Wilson
..Net Compact Framework MVP

"CroDude" <di***********@zg.htnet.hr> wrote in message
news:d0**********@news1.xnet.hr...
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.

Nov 16 '05 #2

P: n/a
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);
}
}
Nov 16 '05 #3

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

--
Tim Wilson
..Net Compact Framework MVP

"CroDude" <di***********@zg.htnet.hr> wrote in message
news:d0**********@news1.xnet.hr...
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);
}
}

Nov 16 '05 #4

P: n/a
Dam' it, that was under my nose! Heh ... thanks Tim! That helped a lot!
Nov 16 '05 #5

P: n/a
Glad to help.

--
Tim Wilson
..Net Compact Framework MVP

"CroDude" <di***********@zg.htnet.hr> wrote in message
news:d0**********@news1.xnet.hr...
Dam' it, that was under my nose! Heh ... thanks Tim! That helped a lot!

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.