【发布时间】:2010-11-24 20:58:18
【问题描述】:
我正在 Visual Studio .net 2005 中使用 C# 编写一个 Windows 应用程序。
在表单中,有一些带有透明背景的控件;表单打开时最大化并具有全屏背景。
应用程序运行速度很慢,CPU 使用率很高。
这是为什么?
【问题讨论】:
标签: c# transparency performance
我正在 Visual Studio .net 2005 中使用 C# 编写一个 Windows 应用程序。
在表单中,有一些带有透明背景的控件;表单打开时最大化并具有全屏背景。
应用程序运行速度很慢,CPU 使用率很高。
这是为什么?
【问题讨论】:
标签: c# transparency performance
1.使用属性 DoubleBuffered 的解决方案
旁注:仅当您有权访问控件时才有效,因为DoubleBuffered 是控件的受保护属性。与解决方案 2 类似(参见后面的代码)。
// from within the control class
this.DoubleBuffered = true;
// from the designer, in case you are utilizing images
control.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
System.Windows.Forms.Control.DoubleBuffered System.Windows.Forms.Control.BackgroundImageLayout
2。使用 SetStyle + OptimizedDoubleBuffer 的替代解决方案:
旁注:控件自己绘制,窗口消息WM_ERASEBKGND 被忽略以减少闪烁,控件首先绘制到缓冲区而不是直接绘制到屏幕上。
control.SetStyle(UserPaint | AllPaintingInWmPaint | OptimizedDoubleBuffer, true);
System.Windows.Forms.Control.SetStyle(ControlStyles, Boolean) System.Windows.Forms.Control.ControlStyles
3.使用 SetStyle + DoubleBuffer 的替代解决方案:
旁注:类似于OptimizedDoubleBuffer,由于遗留原因,它仍保留在代码库中。
control.SetStyle(UserPaint | AllPaintingInWmPaint | OptimizedDoubleBuffer, true);
System.Windows.Forms.Control.SetStyle(ControlStyles, Boolean) System.Windows.Forms.Control.ControlStyles
【讨论】:
那是因为在 .NET 2 中实现的 GDI+ 透明度并没有得到理想的实现,如 Bob Powell explains。
【讨论】: