How are the painting of invisible controls handled in WinForms?

眉间皱痕 提交于 2019-12-11 00:57:01

问题


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.

  1. 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.
  2. Override OnPaintBackground and have it do nothing.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!