I am building a .NET 3.5 application with a form that draws a partially transparent black background. I override OnPaintBackground to accomplish this:
protected override void OnPaintBackground( PaintEventArgs e ) { using ( Brush brush = new SolidBrush( Color.FromArgb( 155, Color.Black ) ) ) { e.Graphics.FillRectangle( brush, e.ClipRectangle ); } }
This works, but sometimes the shape is drawn by itself, without clearing the screen, making the transparency darker than it should be. I tried playing with Graphics.Flush()
and Graphics.Clear()
, but this either does not help or completely removes transparency. Any suggestions?
Edit: Here's what it looks like, after starting the application on the left, and after the form redraws itself several times (in response to a tab from one control to another) on the right:
Transparency Error http://www.quicksnapper.com/files/5085/17725729384A10347269148_m.png
Edit 2: I tried a few things this morning and noticed that when the desktop behind the transparent parts changes, it doesn't actually redraw. For example, if I open the task manager and put it outside the window, you will not see it updating. This makes sense with what I saw with transparency levels. Is there a feature that allows Windows to redraw the area outside the window?
Edit 3: I tried to change several properties in the form, but all of them lead to the fact that the trait of the form is opaque black:
this.AllowTransparency = true; this.DoubleBuffered = true; this.Opacity = .99;
I will try to create a separate window for the transparent part, as mentioned above, but any other ideas are still welcome.