自定义控件中的绘制和无效机制
我正在创建的自定义控件需要在其客户端空间中绘制许多“项目”。对 Invalidate() 的调用将触发一个新的绘制周期,其中所有项目都将被重新绘制。
现在,当控件内有很多项目并且发生大量导航时,就需要进行优化;所以我需要触发一个绘制周期,其中只绘制一个或两个项目。我存储对这些项目的引用,以便绘制方法 (OnPaint) 知道它是“快速”的。
困难在于,当执行 OnPaint 时,很难知道是否同时进行了其他 Invalidate() 调用。在这种情况下,它应该进行“正常”的完整油漆。
我确实使用了剪辑矩形。当然,我可以检查 OnPaint 中的剪辑矩形是否已成为整个客户端矩形,这是调用 Invalidate() 的标志,但这并不是 100% 安全。我想到了其他类似的解决方案,但它们看起来很古怪。
这个问题通常或最好的解决方法是什么?
A custom control I am creating needs to draw many "items" in its client space. A call to Invalidate() would trigger a new paint cycle wherein all items would be redrawn.
Now when there are many items and a lot of navigation happens within the control, things need to be optimized; so I need to trigger a paint cycle where only one or two items are drawn. I store references to these items so that the paint method (OnPaint) knows it's a "quicky".
The difficulty is that when OnPaint is executed, it is hard to know if other Invalidate() calls have been made in the meantime. In that case it should do a "normal", complete paint.
I do make use of the clip rectangle. Of course I could check if the clip rectangle in OnPaint has become the whole of the client rectangle, a sign that Invalidate() was called, but this is not 100% safe. I thought of other similar solutions but they seem hacky.
What is the way this problem is usually, or best, solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的解决方案是使用 BufferedGraphics 类的双缓冲方法。这样,您的 OnPaint 中就不会出现太多棘手的事情,并且您可以随时随地进行绘画。
MSDN:双缓冲图形(在“手动管理缓冲图形”下)
如下一个有用的例子:
C# 中的自定义绘图控件 – 手动双缓冲
The solution here would be to employ a double buffering approach with the BufferedGraphics class. This way you won't have so much tricky stuff going on in your OnPaint and you'll be able to paint whenever, whatever.
MSDN: Double Buffered Graphics (under "Manually Managing Buffered Graphics")
Here's a useful example:
Custom Drawing Controls in C# – Manual Double Buffering