3

I am writing a windows application with C# in visual studio .net 2005.

In the form , there are some control with transparent Background; the form opens maximised and with full screen background.

The application runs very slow with high CPU usage.

Why is this?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Modir
  • 171
  • 3
  • 9

2 Answers2

7

1. Solution using property DoubleBuffered

Sidenote: Only works if you have access to the control as DoubleBuffered is a protected property of Control. Similar as solution 2 (see code behind).

// 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. Alternative solution using SetStyle + OptimizedDoubleBuffer:

Sidenote: The control paints itself, window message WM_ERASEBKGND is ignored to reduce flicker, and the control is first drawn to a buffer rather than directly to the screen.

control.SetStyle(UserPaint | AllPaintingInWmPaint | OptimizedDoubleBuffer, true);

System.Windows.Forms.Control.SetStyle(ControlStyles, Boolean) System.Windows.Forms.Control.ControlStyles

3. Alternative solution using SetStyle + DoubleBuffer:

Sidenote: Similar as OptimizedDoubleBuffer, due to legacy reasons it remained in the codebase.

control.SetStyle(UserPaint | AllPaintingInWmPaint | OptimizedDoubleBuffer, true);

System.Windows.Forms.Control.SetStyle(ControlStyles, Boolean) System.Windows.Forms.Control.ControlStyles

Terence
  • 10,533
  • 1
  • 14
  • 20
Modir
  • 171
  • 3
  • 9
  • Beware that DoubleBuffering requires more memory, see https://stackoverflow.com/questions/6314197/advantages-and-disadvantages-when-using-doublebuffer-on-control – Terence Nov 22 '21 at 20:52
  • More info on SetStyles: DoubleBuffer vs OptimizedDoubleBuffer, see https://stackoverflow.com/questions/1967228/controlstyles-doublebuffer-vs-controlstyles-optimizeddoublebuffer – Terence Nov 22 '21 at 21:00
5

Thats because the GDI+ transparency implemented in .NET 2 is not ideally implemented, as Bob Powell explains.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Martin
  • 2,442
  • 14
  • 15