Problem in drawing my user controls

O

osmarjunior

Hello there...

I have a custom user control, in which i put a panel on the top.
In the Paint event of the panel, i have the following code:

VisualStyleRenderer x = new
VisualStyleRenderer(VisualStyleElement.ExplorerBar.NormalGroupHead.Normal);
x.DrawBackground(e.Graphics, e.ClipRectangle);

VisualStyleRenderer y = new
VisualStyleRenderer(VisualStyleElement.ExplorerBar.NormalGroupExpand.Normal);
y.DrawBackground(e.Graphics, new
Rectangle(e.ClipRectangle.Width-30, e.ClipRectangle.Y, 30,
e.ClipRectangle.Height));

But when I run the application, and resize the form, or move another
program window above my application window, the user control drawing
becomes "noised"... a kind of "broken"...
I don't know what's happening...
Should I use another event instead of Paint??? But if I use another
event, how am I supposed to draw my component???

Regards.

Junior.
 
S

sb

The problem is that you are trying to paint only _within_ the
cliprect...which is not what I think you want in this case. The
ClipRectangle is basically the portion of the window (Control) that Windows
thinks should be repainted...for whatever reason (a portion just became
visible, a window was restored/maximized, etc.) You can either use this
information and paint only within the cliprect...which can significantly
speed things up...or you can safely ignore it and code the method to repaint
everything. It's completely up to you.

Anyway, I'd try something like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
// Because the gradient being rendered is based on the entire control's
size, you need to code it like you're repainting it completely.
// Note that the framework will safely ignore painting that falls
outside of the ClipRectangle.
VisualStyleRenderer x = new
VisualStyleRenderer(VisualStyleElement.ExplorerBar.NormalGroupHead.Normal);
x.DrawBackground(e.Graphics, panel1.ClientRectangle);

// Since we're only painting an arrow which is not based on the entire
control's size, we can get away with a quick check to see if it even needs
to be repainted.
// These kinds of quick checks can save a lot of time if you're doing a
lot of painting.
Rectangle r = new Rectangle(panel1.ClientRectangle.Width - 30,
panel1.ClientRectangle.Y, 30, panel1.ClientRectangle.Height);
if (r.IntersectsWith(e.ClipRectangle))
{
VisualStyleRenderer y = new
VisualStyleRenderer(VisualStyleElement.ExplorerBar.NormalGroupExpand.Normal);
y.DrawBackground(e.Graphics, r);
}
}

-sb
 

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