问题
I'm trying to override the OnPaint events of the windows form in .NET Compact Framework.
So far, I'm able to draw the controls with gradients, rounded rectangles etc that would not normally be possible with regular controls.
However, I'm having problems when I want to make a control invisible. Right now, what I do is re-draw all the controls except those invisible ones. Since it takes a bit of time, it causes some flicker.
Does anyone know how windows handles painting the "invisible" controls? Or can you suggest any algorithm or method on how to achieve this.
回答1:
There should be several things you're doing.
- Don't paint right to the Graphics object handed to you in
OnPaint
. Draw to a back buffer, and paint that to the Graphics at the end. - Override
OnPaintBackground
and have it do nothing. - Invalidate only areas that change, not the entire screen, whenever possible to keep the clipping region small
If you're changing a single control to "not visible", then you'd change it and invalidate just it's bounding rect. The in OnPaint
you'd do your drawing, omitting the "invisible" control based on the Visibility property. Only the clipping region will get updated. You could even go one further by checking the incoming clipping region on OnPaint
and only bothering to do draw logic draw that region. That's a bit more complex though, and often isn't required.
回答2:
As an alternative, would it be possible to call any overload of the Invalidate() method on the appropriate controls to reduce total number of controls that need to be repainted?
来源:https://stackoverflow.com/questions/13584912/how-are-the-painting-of-invisible-controls-handled-in-winforms