NETCF - 优化重绘 (onPaint)

发布于 2024-09-04 20:20:45 字数 997 浏览 4 评论 0原文

我想询问有关如何优化 Compact Framework 中的重绘的建议? GetHashCode() 没有帮助,因为它总是返回不同的哈希码。

不管怎样,我有一个程序,您可以在运行时拖动对象并调整对象的大小。该对象是一个透明对象,它具有一个 PNG 图像,该图像还可以相对于对象客户端大小动态调整大小。

虽然我注意到,(例如,我有 4 个透明对象,并且我正在拖动其中一个或调整其大小),即使 3 个对象没有移动,所有 4 个对象也会触发 OnPaintBackground。另一个当我只是点击一个对象时..它仍然会触发 onPaintBacground()。无论如何,当这个事件被触发时我没有问题。

我喜欢做的是优化,这意味着我只需在必要时重新绘制对象。

大家能给点建议吗?

这是我的伪 C# 代码,

Bitmap _backBuff;
onResize() {
   if(_backBuff != null) _backBuff.Dispose();
   _backBuff = new Bitmap(ClientSize.Width, ClientSize.Height);
   Invalidate();
}

onPaintBackground(e) /*have to use onPaintBackground because MSDN said it's faster*/ {
   using(Graphics g = Graphics.FromImage(_backBuff)) {
      g.Clear(Color.Black);

      // draw background
      ....some interface calling here
      ....and paint the background

      // draw alpha PNG
      .. get hDc
      .. paint PNG
      .. release hDc
   }

   e.Graphics.DrawImage(_backBuff,0,0);
}

提前致谢。

I want to ask for suggestions on how to optimize a repaint in Compact Framework? GetHashCode() didn't help because it always return a different hash code.

Anyway, I have a program which you can drag and resize an object in run time. This object is a transparent object and it has a PNG image which also dynamically resize relative to object client size.

Though I noticed, (e.g. I have 4 transparent object and I'm dragging or resizing one) all 4 of them triggers OnPaintBackground even if the 3 are not moving. Another one when am just tapping on the one object .. it sill triggers onPaintBacground(). Anyway, I don't have a problem when this events get triggered.

What I like to do is optimization and that means I only have to repaint the object when it's necessary.

Can you guys please give a suggestions?

here's my pseudo C# code

Bitmap _backBuff;
onResize() {
   if(_backBuff != null) _backBuff.Dispose();
   _backBuff = new Bitmap(ClientSize.Width, ClientSize.Height);
   Invalidate();
}

onPaintBackground(e) /*have to use onPaintBackground because MSDN said it's faster*/ {
   using(Graphics g = Graphics.FromImage(_backBuff)) {
      g.Clear(Color.Black);

      // draw background
      ....some interface calling here
      ....and paint the background

      // draw alpha PNG
      .. get hDc
      .. paint PNG
      .. release hDc
   }

   e.Graphics.DrawImage(_backBuff,0,0);
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-09-11 20:20:45

有一个想法,

我必须检查新位置或旧位置之间的新尺寸差异。如果其中之一是新的,则重新绘制..否则..绘制_backBuff(充当缓存图像)。

我实现了它,到目前为止,重新绘制或绘制缓存看起来不错。

Got an idea

I have to check for new location or new size differences between the old ones. If one of them are new, then do a repaint .. else .. paint the _backBuff (which acts as a cache image).

I implemented it and it looks good so far about repainting or painting the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文