【问题标题】:Windows application very slow when using transparency使用透明度时 Windows 应用程序非常慢
【发布时间】:2010-11-24 20:58:18
【问题描述】:

我正在 Visual Studio .net 2005 中使用 C# 编写一个 Windows 应用程序。

在表单中,有一些带有透明背景的控件;表单打开时最大化并具有全屏背景。

应用程序运行速度很慢,CPU 使用率很高。

这是为什么?

【问题讨论】:

    标签: c# transparency performance


    【解决方案1】:

    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

    【讨论】:

    【解决方案2】:

    那是因为在 .NET 2 中实现的 GDI+ 透明度并没有得到理想的实现,如 Bob Powell explains

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2012-10-12
      • 2020-04-09
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多