Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows application very slow when using transparency

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?

like image 506
Modir Avatar asked Oct 29 '25 16:10

Modir


2 Answers

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

like image 140
Modir Avatar answered Oct 31 '25 07:10

Modir


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

like image 29
Martin Avatar answered Oct 31 '25 06:10

Martin